本文介绍了工具栏按钮单击事件功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在工具栏
中创建了设置按钮,现在,每当我单击设置按钮时,都需要将屏幕导航到设置屏幕.
I have created settings button in Toolbar
, now I need to navigate the screen to settings screen, when ever i click the settings button.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="schemas.android.com/apk/res/android"; xmlns:app="schemas.android.com/apk/res-auto">;
<item android:id="@+id/action_settings"
android:title="ConfigApp"
android:icon="@drawable/ic_action_name"
app:showAsAction="ifRoom" />
</menu>
推荐答案
您应该覆盖 onCreateOptionsMenu
和 onOptionsItemSelected
方法,并采用特定的 id
从菜单项中导航到设置屏幕.
you should override the onCreateOptionsMenu
and onOptionsItemSelected
methods and take that particular id
from menuitem and navigate to settings screen.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu_xml, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_settings:
Intent i = new Intent(yourActivity.this, SettingsActivity.class)
startActivity(i);
break;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
这篇关于工具栏按钮单击事件功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!