【问题标题】:isChecked() not workingisChecked() 不工作
【发布时间】:2014-07-03 10:59:43
【问题描述】:

我基于menu做了一个简单的app,java代码是

public class MainActivity extends Activity {
    TextView tv;
    CheckBox chb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.textView);
        chb=(CheckBox)findViewById(R.id.chbInFlate);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
       menu.add(0, 1, 0, "add");

        menu.add(0, 2, 0, "edit");
        menu.add(0, 3, 3, "delete");
        menu.add(1, 4, 1, "copy");
        menu.add(1, 5, 2, "paste");
        menu.add(1, 6, 4, "exit");

        return true;
    }

    public boolean OnPrepareOptionsMenu(Menu menu)
    {
        if(chb.isChecked()) {
            Log.d("my", "aaa");
            menu.setGroupVisible(1, true);
        } else {
            Log.d("my", "bbb");
            menu.setGroupVisible(1, false);
        }
        return super.onPrepareOptionsMenu(menu);
    }

    @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.
        Log.d("my", "bbb");
        StringBuilder sb=new StringBuilder();
        sb.append("Menu Item");
        sb.append("\r\n Group id="+ String.valueOf(item.getGroupId()));
        sb.append("\r\n Item id="+ String.valueOf(item.getItemId()));
        sb.append("\r\n Order ="+ String.valueOf(item.getOrder()));
        sb.append("\r\n Title="+ item.getTitle());
        tv.setText(sb.toString());
        return super.onOptionsItemSelected(item);
    }
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.add.MainActivity" >
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="@string/hello_world" />

    <CheckBox
        android:id="@+id/chbInFlate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="67dp"
        android:text="CheckBox" />

</RelativeLayout>

最初,所有6个菜单项都可见,无论检查是否检查或未检查复选框.....现在放置log.d方法后,选择一个菜单项时,消息在我的logcat过滤器中正确显示,但是log.d.d isChecked 方法没有响应。为什么会这样?

【问题讨论】:

  • 勾选复选框时需要调用invalidateOptionsMenu()

标签: java android


【解决方案1】:
  1. 您应该在复选框状态发生变化时调用invalidateOptionsMenu()

  2. OnPrepareOptionsMenu() 的大小写错误。应该是onPrepareOptionsMenu()。确保添加@Override 注释以避免此类问题。

【讨论】:

    【解决方案2】:

    尝试在 onCreate 方法的末尾添加:

    invalidateOptionsMenu();
    

    好像是同样的问题here

    【讨论】:

      【解决方案3】:

      试试这个:

        protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              tv=(TextView)findViewById(R.id.textView);
              chb=(CheckBox)findViewById(R.id.chbInFlate);
              invalidateOptionsMenu()  
          }
      

      【讨论】:

        猜你喜欢
        • 2016-06-14
        • 1970-01-01
        • 2016-12-30
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-30
        • 1970-01-01
        相关资源
        最近更新 更多