Android 如何使右上方图标打开新活动而不是显示项目?
所以,我有这个 这里是我如何设置的Android 如何使右上方图标打开新活动而不是显示项目?,android,android-layout,Android,Android Layout,所以,我有这个 这里是我如何设置的 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } 我的菜单->main.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.and
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
我的菜单->main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
嗯,这是默认设置,现在当我单击钟形图标时,它将显示菜单.xml
中的项但是,我想要的是,当我单击钟形图标时,它将打开新活动,而不是显示菜单中的项
我怎样才能做到这一点
我想知道为什么我的问题被标记为重复的?我的问题是,如何使右上方图标打开新活动而不是显示项目?
不是
如何从菜单项打开“活动”您可以使用onOptionsItemSelected()
方法执行此操作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
Intent intent = new (CurrentActivity.class, NextActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:icon="@drawable/bell"
android:visible="true"
app:showAsAction="always"
android:title="bell"/>
</menu>
从菜单中放入main.xml文件。@Abhishek啊,我忘了。对不起,请看这个,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:icon="@drawable/bell"
android:visible="true"
app:showAsAction="always"
android:title="bell"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Intent intent = new Intent(this, YourActivityName.class);
startActivity(intent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}