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