【问题标题】:Drawer navigation header button does not work after click单击后抽屉导航标题按钮不起作用
【发布时间】:2019-08-21 02:16:20
【问题描述】:

我在导航标题栏中创建了一个登录按钮。我可以看到按钮,但点击它时没有反应。

我尝试在 MainActivity > OnCreate() 中添加按钮单击事件,但它不起作用。

override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_main)

       //FragNav
       //list of fragments
       val fragments = ArrayList<Fragment>(3)

       //add fragments to list
       fragments.add(BottomHome.newInstance(0))
       fragments.add(BottomMap.newInstance(0))
       fragments.add(BottomCategory.newInstance(0))

       //link fragments to container
       fragNavController = FragNavController(supportFragmentManager, R.id.container, fragments)
       //End of FragNav

       //BottomBar menu
       mBottomBar = BottomBar.attach(this, savedInstanceState)
       mBottomBar!!.setItems(R.menu.bottombar_menu)
       mBottomBar!!.setOnMenuTabClickListener(object : OnMenuTabClickListener {
           override fun onMenuTabSelected(@IdRes menuItemId: Int) {
               //switch between tabs
               when (menuItemId) {
                   R.id.bottomBarItemOne -> fragNavController!!.switchTab(TAB_FIRST)
                   R.id.bottomBarItemSecond -> fragNavController!!.switchTab(TAB_SECOND)
                   R.id.bottomBarItemThird -> fragNavController!!.switchTab(TAB_THIRD)
               }
           }

           override fun onMenuTabReSelected(@IdRes menuItemId: Int) {
               if (menuItemId == R.id.bottomBarItemOne) {
                   fragNavController!!.clearStack()
               }
           }
       })
       //End of BottomBar menu

       //Navigation drawer
       DrawerBuilder().withActivity(this).build()



       //primary items
       val menuHome = PrimaryDrawerItem()
               .withIdentifier(1)
               .withName(R.string.menu_home)
               .withIcon(R.drawable.menu_home)

       val menuEvent = PrimaryDrawerItem()
               .withIdentifier(3)
               .withName(R.string.menu_event)
               .withIcon(R.drawable.menu_event)



       //Toolbar
       val toolbar = findViewById<View>(R.id.toolbar) as Toolbar
       setSupportActionBar(toolbar)
       supportActionBar!!.setDisplayHomeAsUpEnabled(true)
       supportActionBar!!.setHomeButtonEnabled(false)

       DrawerBuilder()
               .withActivity(this)
               .withHeader(R.layout.nav_header_main)
               .withToolbar(toolbar)
               .withActionBarDrawerToggleAnimated(true)
               .withTranslucentStatusBar(false)
               .withFullscreen(true)
               .withSavedInstance(savedInstanceState)
               .addDrawerItems(
                       menuHome,
                       menuEvent,

                       DividerDrawerItem()


               )
               .withOnDrawerItemClickListener { view, position, drawerItem ->
                   if (drawerItem != null) {
                       var intent: Intent? = null
                       if (drawerItem.identifier == 1L) {
                           intent = Intent(this@MainActivity, MainActivity::class.java)

                       } else if (drawerItem.identifier == 2L) {
                           intent = Intent(this@MainActivity, LeftMenuMerchant::class.java)
                       } 
                       if (intent != null) {
                           this@MainActivity.startActivity(intent)
                       }

                       val btnLogin = findViewById<Button>(R.id.btnLogin)
                       btnLogin?.setOnClickListener {
                           var intent = Intent(this, LoginPage::class.java)
                           startActivity(intent)

                       }
                   }

                   false
               }
               .build()
       //End of Navigation drawer

   }

如何在 OnCreate() 中调用登录按钮事件,以便在单击登录按钮后链接到登录页面?

请帮忙。谢谢

【问题讨论】:

    标签: android button kotlin navigation-drawer


    【解决方案1】:

    请试试这个代码导航视图。我们必须采取getHeaderView。

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    View headerview = navigationView.getHeaderView(0);
    Button profilename = (Button ) headerview.findViewById(R.id.prof_username);
    profilename.setText("your name")
    

    【讨论】:

    • 我想在我的抽屉导航标题栏中添加按钮,而不是 textview。你能告诉我如何在我的场景中嵌入你的代码吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-12-20
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多