我在导航标题栏中创建了一个登录按钮。我可以看到按钮,但点击它时没有反应。
我尝试在 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) {
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cabea2a3b98a87aba3a48ba9bea3bca3beb3e4b9beabb8be8ba9bea3bca3beb3" rel="noreferrer noopener nofollow">[email protected]</a>(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() 中调用登录按钮事件,以便单击登录按钮后链接到登录页面?
请帮忙。谢谢
最佳答案
请尝试此代码导航 View 。我们必须带到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")
关于android - 单击后抽屉导航标题按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57583609/