在菜单中切换按钮,如何在真或假时至少吐司

时间:2017-04-19 02:47:27

标签: android

所以我试图在菜单中添加一个开关,我想(至少在这一点上)当开关打开或关闭时做一个Toast。目前没有任何事情发生,我不太清楚为什么。

我添加了菜单项和开关布局的代码。

请指教,谢谢!!

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.switch, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.myswitch:
            Switch simpleSwitch = (Switch) findViewById(R.id.menuSwitch);
            simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        Toast.makeText(ModularActivity.this, "This is on", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(ModularActivity.this, "This is off", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            return true;
        case android.R.id.home:
            // Navigate back to parent activity (CatalogActivity)
            NavUtils.navigateUpFromSameTask(this);
            return true;

    }
    return true;
}

这是菜单项:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/myswitch"
    android:title=""
    app:showAsAction="always"
    app:actionLayout="@layout/activity_switch_layout"
    />

这是activity.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:text="WHITE"/>
    <Switch
        android:id="@+id/menuSwitch"
        android:checked="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:text="GREEN"
        android:layout_marginRight="5dp"
        android:textStyle="bold"
        android:textColor="@color/green"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您无法在菜单项中添加开关或toggleButton。但是如果你愿意的话,你可以在工具栏上做同样的事情。

在此之前,我需要引起您对已添加的菜单项的注意。您可以添加 android:checkable =&#34; true&#34;

<item
android:id="@+id/checkable_menu"
android:checkable="true"
android:title="@string/checkable" />

然后你可以在方法onOptionsItemSelected

中更改相同的状态
case R.id.checkable_menu:
        isChecked = !item.isChecked();
        item.setChecked(isChecked);
        return true;

但如果要显示开关或切换按钮,仍然使用工具栏概念...

<item
android:id="@+id/show_secure"
android:enabled="true"
android:title=""
android:visible="true"
app:actionLayout="@layout/show_protected_switch"
app:showAsAction="ifRoom" />

这是您的 switch.xml 布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">

   <ToggleButton
   android:id="@+id/switch_show_protected"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/switch_ptotected_btn_selector"
   android:textOff=""
   android:textOn=""/>
</RelativeLayout>

在你的活动中

ToggleButton mSwitchShowSecure;
mSwitchShowSecure = (ToggleButton) menu.findItem(R.id.show_secure).getActionView().findViewById(R.id.switch_show_protected);
mSwitchShowSecure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b){
                //Your code when checked

            } else {
                //Your code when unchecked
            }
        }
    });

答案 1 :(得分:0)

尝试将代码移动到onCreateOptionsMenu,如下所示:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    MenuItem menuItem = menu.findItem(R.id.myswitch);
    View view = MenuItemCompat.getActionView(menuItem);
    Switch simpleSwitch = (Switch) view.findViewById(R.id.menuSwitch);
    simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                Toast.makeText(MainActivity.this, "This is on", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "This is off", Toast.LENGTH_SHORT).show();
            }
        }
    });

    return super.onCreateOptionsMenu(menu);
}

另外,请注意我是如何在第2行中引入MenuItem和View的。您基本上不得不在任何地方寻找您的开关,而是在菜单视图中查找。

之后一切都很完美。