2016-05-13 11 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

アプリに不要に:= "android.widget.CheckBox" actionViewClassはちょうどこの

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

uが私の答えに確認したのですか? http://stackoverflow.com/a/372​​03577/3981656 –

+0

ありがとうございます!しかし私のチェックが私のメニューに表示されている必要があります。 これは、私がapp:actionViewClass = "android.widget.CheckBoxを使用する理由です。あなたの例では、私のメニューで私のチェックを見ることができますか?app:showAsAction =" always "は機能しません。私の小切手を見てください。 –

関連する問題