我做了一个简单的基于menu.the的应用程序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方法后的not.....now,当选择一个菜单项时,消息都会正确显示在我的logcat筛选器中,但log.d对isChecked方法没有响应。为什么会发生这种情况?
发布于 2014-07-03 09:18:09
只要复选框状态changes.
OnPrepareOptionsMenu()的大小写错误,就应该调用onPrepareOptionsMenu()。确保添加@Override注释以避免此类问题。发布于 2014-07-03 09:18:29
发布于 2014-07-03 10:59:43
试试这个:
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()
}https://stackoverflow.com/questions/24543437
复制相似问题