繁体   English   中英

复选框菜单项不起作用

[英]checkbox menu item doesn't work

我的menu_check.xml中有此文件,但是当我单击检查时,什么也没发生,所以Toast从不显示...这是什么问题? 非常感谢

  <?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);
    }
}

不需要app: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"/>

因此,当您取消选中菜单项时,它将显示吐司,并显示选中或未选中的复选框

尝试这个,

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>

MainActivity.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);
}

这应该工作。

编码愉快!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM