【发布时间】:2014-01-15 00:58:03
【问题描述】:
我有这个菜单布局:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/actionBarSave"
android:showAsAction="always"
android:scaleType="fitXY"
android:icon="@drawable/save_33x26"
style="@style/ActionButtonStyle"
android:title="save" />
<item android:id="@+id/actionBarLoad"
android:showAsAction="always"
android:icon="@drawable/load70x24"
android:scaleType="fitXY"
android:title="load" />
<item android:id="@+id/actionBarDelete"
android:showAsAction="always"
android:scaleType="fitXY"
android:icon="@drawable/delete_enabled"
android:title="delete" />
<item
android:id="@+id/actionBarSoundSwitch"
android:title="soundswitch"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:showAsAction="always"
android:actionLayout="@layout/sound_switch" />
</menu>
sound_switch 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="@+id/soundSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SFX"
android:layout_centerInParent="true" />
</RelativeLayout>
这是我尝试将 OnCheckedChangeListener 添加到开关的 java 代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
soundSwitch = (Switch)findViewById(R.id.actionBarSoundSwitch);
soundSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
soundOn = arg1;
}
});
switch (item.getItemId()) {
case R.id.actionBarSave:
onSave();
return true;
case R.id.actionBarLoad:
onLoad();
return true;
case R.id.actionBarDelete:
onDelete();
return true;
case R.id.actionBarSoundSwitch:
onSound();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
没有 logcat 错误,但我检查过,监听器不工作。任何想法为什么?
【问题讨论】:
-
有人可以回复此评论,以便我至少知道有人正在处理它吗?我仍在努力,但没有任何工作......
-
听起来您在添加操作栏切换按钮时使用了错误的路径...
-
@shoerat 谢谢你的回复,我可能是因为它不起作用,你有什么想法吗?
-
作为答案发布,希望对您有所帮助。