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

问题描述 投票:3回答:2

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

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" />
android android-actionbar android-toolbar android-menu
2个回答
3
投票

inflate ActionBar的外部布局似乎首先创建一个view,为你的ChildView保存你的layout

像例子

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

0
投票

尝试

(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); } } }
© www.soinside.com 2019 - 2024. All rights reserved.