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

Switch onCheckedChangedListner not registering clicks inside toolbar menu

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

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

尝试

(compoundButton.isChecked())

In boolean

试试另一个版本的 setonCheckedChangedListner

示例:

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (!compoundButton.isSelected()) { Log.i("Yeah" , "Is Not Selected"); invertLock(-1); } else { if (Utilities.isLockEnabled(context)) { Log.i("Yeah" , "Is Locked"); Utilities.showLockEnabled(context); } else { Log.i("Yeah" , "Is Not Locked"); invertLock(1); } } }

inflate ActionBar 上的外部布局似乎首先创建了一个 view 来容纳 ChildViewlayout

喜欢例子

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;
}