2016-05-13 202 views
0

我在我的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); 
    } 
} 

回答

0

试试这个,

menu_main.xml

<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="com.example.stackoverflow.MainActivity" > 

<item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:title="@string/action_settings" 
    app:showAsAction="never"/> 

    <item 
    android:id="@+id/action_check" 
    android:title="YOUR_TITLE" 
    android:orderInCategory="200" 
    app:showAsAction="never" 
    android:visible="true" 
    android:checkable="true"/> 

</menu> 

MainActivi ty.java

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 

     SharedPreferences settings = getSharedPreferences("settings", 0); 
     boolean isChecked = settings.getBoolean("checkbox", false); 
     MenuItem item = menu.findItem(R.id.action_check); 
     item.setChecked(isChecked); 

    return true; 
} 

@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(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    if (id == R.id.action_check) { 
     item.setChecked(!item.isChecked()); 
     SharedPreferences settings = getSharedPreferences("settings", 0); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean("checkbox", item.isChecked()); 
     editor.commit(); 
     Log.i("cheeck status" , "" + item.isChecked()); 

     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

这应该工作。

快乐编码。

2

无需应用:actionViewClass = “android.widget.CheckBox” 只是做像在OnCreate菜单选项本

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater menuInflater = getMenuInflater(); 
    menuInflater.inflate(R.menu.main_menu, menu); 
    return true; 
} 

和onOptionsItemSelected编写代码

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_bookmark: 
      if (item.isChecked()) { 
       item.setChecked(false); 
       Toast.makeText(MainActivity.this, "Un Checked", Toast.LENGTH_SHORT).show(); 
      } else { 
       item.setChecked(true); 
       Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
    } 
    return super.onOptionsItemSelected(item); 
} 

并在你的menu.xml文件夹中声明这样的菜单

<?xml version="1.0" encoding="utf-8"?> 

<item android:id="@+id/menu_bookmark" 
     android:checkable="true" 
     android:title="Bookmark"/> 

所以当你检查取消它表明面包和显示复选框选中或取消选中

+0

没有ü我的回答检查菜单项? http://stackoverflow.com/a/372​​03577/3981656 –

+0

谢谢。!但我需要我的支票显示在我的菜单上。 这就是为什么我使用app:actionViewClass =“android.widget.CheckBox。在你的例子中,我如何看到我的菜单中的检查?因为app:showAsAction =”always“不起作用。看我的支票。 –