Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 菜单中的开关按钮,如何在正确或错误时至少烤面包_Android - Fatal编程技术网

Android 菜单中的开关按钮,如何在正确或错误时至少烤面包

Android 菜单中的开关按钮,如何在正确或错误时至少烤面包,android,Android,因此,我试图在菜单中添加一个开关,我想在开关打开或关闭时(至少在这一点上)敬酒。目前什么都没有发生,我也不太清楚为什么 我已经为我的菜单项和开关布局添加了代码 请告知,谢谢 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.switch, menu); return super.onCreateOptionsMenu(menu); } @Overr

因此,我试图在菜单中添加一个开关,我想在开关打开或关闭时(至少在这一点上)敬酒。目前什么都没有发生,我也不太清楚为什么

我已经为我的菜单项和开关布局添加了代码

请告知,谢谢

@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>

您不能在菜单项中添加开关或切换按钮。但若你们愿意,你们也可以在工具栏上做同样的事情

在此之前,我需要让您注意您添加的菜单项。您可以添加android:checkable=“true”

但如果你想显示开关或切换按钮,还是要使用工具栏的概念

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

尝试将代码移动到OnCreateOptions菜单,如下所示:

@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);
}
另外,请注意我是如何在前两行中介绍MenuItem和View的。基本上,您必须在菜单视图中查找开关,而不是在任何地方

在那之后,一切都很顺利

<item
android:id="@+id/show_secure"
android:enabled="true"
android:title=""
android:visible="true"
app:actionLayout="@layout/show_protected_switch"
app:showAsAction="ifRoom" />
<?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
            }
        }
    });
@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);
}