android-开关onCheckedChangedListner不注册工具栏菜单内的点击
作者:互联网
我的应用程序工具栏中有一个开关,可为用户提供更改语言的选项.
当我使用开关时,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创建另一个菜单项(下面的代码)并单击一次,那么现在当我使用开关时,会出现Toast消息.对于为什么它不是第一次没有发生在这里我一无所知.另外,如果没有此菜单项,如何使它工作.
<item
android:id="@+id/english"
android:orderInCategory="3"
android:title="@string/switch_text_on"
app:showAsAction="always" />
解决方法:
在ActionBar上膨胀一个外部布局,该外部布局似乎首先创建一个视图,该视图保存您的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-toolbar,android-actionbar,android-menu,android 来源: https://codeday.me/bug/20191110/2014162.html