我在我的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「不起作用。看我的支票。 –