Java 菜单中的On/Off开关未以编程方式返回状态

Java 菜单中的On/Off开关未以编程方式返回状态,java,android,menu,menuitem,Java,Android,Menu,Menuitem,我在菜单中添加了开/关开关。如果开关处于“关闭”状态,我想敬酒“开关处于关闭”状态。但当我从菜单打开开关时,它不工作。我已经试着调试它,代码没有进入if语句“if(id==R.id.myswitch)”中。我试着把简单的按钮控制它的工作,“按钮被点击”。但这个开关不起作用 我在菜单项中增加了布局“switch_layout.xml” 以下是xml和代码: switch_layout.xml <?xml version="1.0" encoding="utf-8"?> <Rela

我在菜单中添加了开/关开关。如果开关处于“关闭”状态,我想敬酒“开关处于关闭”状态。但当我从菜单打开开关时,它不工作。我已经试着调试它,代码没有进入if语句“if(id==R.id.myswitch)”中。我试着把简单的按钮控制它的工作,“按钮被点击”。但这个开关不起作用

我在菜单项中增加了布局“switch_layout.xml”

以下是xml和代码:

switch_layout.xml

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

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp" />

</RelativeLayout>
尝试上面的代码并检查getID()返回的ID

if (id == R.id.myswitch) {
        View view = item.getActionView();
        if (view != null) {
            Switch onOff = (Switch) view
                    .findViewById(R.id.switchForActionBar);
            if (onOff!=null) {
                if (onOff.isChecked()) {
                    Toast.makeText(context,
                            "Switch is ON",
                            Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(context,
                            "Switch is OFF",
                            Toast.LENGTH_LONG).show();
                }
            }
            else {
                Toast.makeText(context,
                        "Switch is null",
                        Toast.LENGTH_LONG).show();
            }

        }
        else {
            Toast.makeText(context,
                    "view is null",
                    Toast.LENGTH_LONG).show();
        }

    }
如果这个代码有效,请尝试一下,但我不确定它是否有效。这只是猜测

或者您可以尝试下面的代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

boolean bSwitch=false;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    int id = menu.getItem(0).getItemId();
    if (id == R.id.myswitch) {
        if (bSwitch) {
            menu.getItem(0).setTitle("smthng is ON");
        }
        else {
            menu.getItem(0).setTitle("smthng is OFF");
        }
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.myswitch) {
        if (bSwitch) {
            bSwitch=false;
        }
        else {
            bSwitch=true;
        }

    }
    return super.onOptionsItemSelected(item);
}

<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.newshunt.news.MainActivity" >

<item
    android:id="@+id/myswitch"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="smthng is OFF"/>
</menu>
@覆盖
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
布尔b开关=假;
@凌驾
公共布尔值OnPrepareOptions菜单(菜单){
//TODO自动生成的方法存根
int id=menu.getItem(0.getItemId();
if(id==R.id.myswitch){
如果(B开关){
menu.getItem(0).setTitle(“smthng打开”);
}
否则{
menu.getItem(0).setTitle(“smthng关闭”);
}
}
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
if(id==R.id.myswitch){
如果(B开关){
b开关=假;
}
否则{
b开关=真;
}
}
返回super.onOptionsItemSelected(项目);
}

A
开关
在单击菜单时会发生变化,或者是一个
土司
来解释什么是显而易见的?我个人不明白——在我看来,这是一个糟糕的UI/UX模式。
 if (id==R.id.myswitch){

            Switch onOff=(Switch) item.getActionView();
          //  if( onOff.isChecked()==false){
            if ( !onOff.isChecked())
                 //Toast that switch is off
            }

 }
if (id == R.id.myswitch) {
        View view = item.getActionView();
        if (view != null) {
            Switch onOff = (Switch) view
                    .findViewById(R.id.switchForActionBar);
            if (onOff!=null) {
                if (onOff.isChecked()) {
                    Toast.makeText(context,
                            "Switch is ON",
                            Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(context,
                            "Switch is OFF",
                            Toast.LENGTH_LONG).show();
                }
            }
            else {
                Toast.makeText(context,
                        "Switch is null",
                        Toast.LENGTH_LONG).show();
            }

        }
        else {
            Toast.makeText(context,
                    "view is null",
                    Toast.LENGTH_LONG).show();
        }

    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

boolean bSwitch=false;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    int id = menu.getItem(0).getItemId();
    if (id == R.id.myswitch) {
        if (bSwitch) {
            menu.getItem(0).setTitle("smthng is ON");
        }
        else {
            menu.getItem(0).setTitle("smthng is OFF");
        }
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.myswitch) {
        if (bSwitch) {
            bSwitch=false;
        }
        else {
            bSwitch=true;
        }

    }
    return super.onOptionsItemSelected(item);
}

<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.newshunt.news.MainActivity" >

<item
    android:id="@+id/myswitch"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="smthng is OFF"/>
</menu>