[英]onOptionsItemSelected is not working in android?
I have a menu_item
and here is the code: 我有一个menu_item
,这是代码:
<item
android:id="@+id/action_search"
android:actionViewClass="android.widget.TextView"
android:title="In Stocks"
app:showAsAction="always" />
<item
android:id="@+id/action_checkbox"
app:actionViewClass="android.widget.CheckBox"
android:title="@string/action_check"
app:showAsAction="always"
/>
I am trying to catch Menu_item_clicked_event
So, I write following code in Activity
file 我正在尝试捕获Menu_item_clicked_event
因此,我在Activity
文件中编写了以下代码
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_checkbox:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.action_search:
System.out.println("xas");
default:
return super.onOptionsItemSelected(item);
}
}
When I am pressing the action_search
it's working but not for action_checkbox
. 当我按下action_search
它起作用了,但是对于action_checkbox
却action_checkbox
。 Why this is happening? 为什么会这样呢? I am getting No exception in ADB logs
. 我在ADB logs
没有任何异常。 Is this is the right way to do check for Checkboxes
? 这是检查Checkboxes
的正确方法吗?
app:actionViewClass should be android:actionViewClass="android.widget.CheckBox" app:actionViewClass应该是android:actionViewClass =“ android.widget.CheckBox”
<item
android:id="@+id/action_checkbox"
android:actionViewClass="android.widget.CheckBox"
android:icon="@drawable/unchecked"
android:checkable="true"
android:checked="false"
android:title="@string/action_check"
app:showAsAction="always"
/>
However checkbox will not be visble. 但是,该复选框将不可见。 Referthis 引用此
you could have your own icon and set it android:icon= 您可以拥有自己的图标并将其设置为android:icon =
And in class 在课堂上
case R.id.action_checkbox:
if(item.isChecked()){
item.setIcon(R.drawable.unchecked);
}
else {
item.setIcon(R.drawable.checked);
}
item.setChecked(!item.isChecked());
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.