我在我的menu_check.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_selecciontodo"
android:title="@string/check"
android:checkable="true"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="always" />
</menu>
,这在我的Java类
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_check, menu);
checkBox = (CheckBox) menu.findItem(R.id.action_selecciontodo).getActionView();
checkBox.setText("Select all");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_selecciontodo:
CheckBox checkBox= (CheckBox) item.getActionView();
if (checkBox.isChecked())
Toast.makeText(getApplicationContext(), "You selected all", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
没有ü我的回答检查菜单项? http://stackoverflow.com/a/37203577/3981656 –
谢谢。!但我需要我的支票显示在我的菜单上。 这就是为什么我使用app:actionViewClass =“android.widget.CheckBox。在你的例子中,我如何看到我的菜单中的检查?因为app:showAsAction =”always“不起作用。看我的支票。 –