android - 切换 onCheckedChangedListner 不在工具栏菜单内注册点击

标签 android android-actionbar android-toolbar android-menu

我的应用程序工具栏中有一个开关,可为用户提供更改语言的选项。

app toolbar

当我使用开关时,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" />

最佳答案

ActionBarinflate 外部布局似乎首先创建一个 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/

相关文章:

android - 在设置了最大高度和最大宽度的 ImageView 中定位图像

java - Android Activity 生命周期 : Problems with Onstop()

android - 通过按钮从一个选项卡 View 移动到另一个选项卡单击 Android ActionBar

android - 如何在 Android 中删除 ActionBar 中的左填充边距额外空间?

Android - Coordinator Layout、Drawer Layout 和 Fragments

java - 当重力=顶部时,文本不会从顶部开始

java - 无法在尚未调用 Looper.prepare() android 服务的线程内创建处理程序

android - 使用 XA_HTTP_CLIENT_HANDLER_TYPE 在 AndroidEnvironment 设置中部署应用程序

android - 在Android上强制溢出菜单按钮

android - 工具栏内不允许相对布局