我的应用程序工具栏中有一个开关,可为用户提供更改语言的选项。
当我使用开关时,onCheckedChanged 方法不起作用。 此外,在我看来,onOptionsItemSelected 也没有被点击,因为我在屏幕上没有看到任何日志消息。
请注意,当我使用开关时应用程序没有崩溃。
对溢出菜单中存在的选项的点击工作正常。我正在获取这些情况下的日志。
Switch aSwitch;
RelativeLayout langSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = this;
// Removed non relevant code here.
}
@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);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.d("tag_id",String.valueOf(id));
langSwitch = findViewById(R.id.app_bar_switch);
aSwitch = langSwitch.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.savedPost){
Intent intent = new Intent(this,SavedPost.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
下面是我的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.app_name.android.app_name.MainActivity">
<item
android:id="@+id/app_bar_switch"
android:orderInCategory="2"
android:title=""
app:actionLayout="@layout/switch_item"
app:showAsAction="always" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/savedPost"
android:checkable="false"
android:orderInCategory="4"
android:title="@string/saved_post" />
</menu>
这是我的开关项目的布局文件。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/langSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textOff="@string/switch_text_off"
android:textOn="@string/switch_text_on"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
附加信息。
如果我使用 showAsAction 'always' 创建另一个菜单项(下面的代码)并单击一次,现在当我使用开关时,会出现 Toast 消息。我不知道为什么它不是第一次发生。另外,如果没有这个菜单项,我如何让它工作。
<item
android:id="@+id/english"
android:orderInCategory="3"
android:title="@string/switch_text_on"
app:showAsAction="always" />
最佳答案
在 ActionBar
上inflate
外部布局似乎首先创建一个 view
来保存你的 ChildView
你的布局
喜欢例子
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
在您的代码中试试这个
Switch aSwitch;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
View sw = menu.findItem(R.id.app_bar_switch).getActionView();
aSwitch = (Switch) sw.findViewById(R.id.langSwitch);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (!b){
//Language1 code
Toast.makeText(context,"Language1 Selected",Toast.LENGTH_SHORT).show();
}else{
//Language2 code
Toast.makeText(context,"Language2 Selected",Toast.LENGTH_SHORT).show();
}
}
});
return true;
}
关于android - 切换 onCheckedChangedListner 不在工具栏菜单内注册点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898647/