我是 android 开发的新手,我正在尝试弄清楚如何将用户带到我的 PreferenceActivity
。我已经创建了一个具有相应布局的 Preferences 类,但是我无法弄清楚如何在没有物理菜单按钮的情况下将用户带到手机上的此 Activity。
我使用的是 Nexus 4,它甚至没有物理菜单按钮。我已经看到菜单按钮(三个垂直点)不时弹出在虚拟按钮(右下角)上,我怎样才能使该按钮出现在手机上并让用户在 onkeydown 上进行设置?
最佳答案
I've seen the menu button (three vertical dots) pop up now and then on the virtual buttons (bottom right)
您看到的是“旧版菜单按钮”:
(来源:blogspot.com)
在您的应用中保留它是一种不好的做法。当您在 Android list 中将 targetSdkVersion
设置为 API 11 或更高版本时,它应该被删除。然后它应该转移到 ActionBar
。它应该看起来像这样:
(来源:blogspot.com)
查看右上角的溢出菜单。
如果您想了解更多,请阅读 this .
如果您想通过点击列表中的一个项目来启动您的 PreferenceActivity
,您必须创建一个菜单。
覆盖onCreateOptionsMenu
:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
我的菜单.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings"
android:title="Settings"
android:showAsAction="never"/>
</menu>
现在覆盖 onOptionsItemSelected
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.settings:
// Start your Activity in here.
break;
}
return super.onOptionsItemSelected(item);
}
关于android - 使用虚拟按钮打开 PreferenceActivity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22270977/