Java 处理操作栏上开关小部件的单击事件

Java 处理操作栏上开关小部件的单击事件,java,android,Java,Android,这就是我如何将开关小部件添加到操作栏的方式。我已经在玩了。但我不知道如何应对点击事件 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.map, menu); return true; } @Ov

这就是我如何将开关小部件添加到操作栏的方式。我已经在玩了。但我不知道如何应对点击事件

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.map, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    System.out.println("actionbar clicked");
    // Handle item selection
    if (item.getItemId()==R.id.myswitch) {
        Switch actionView = (Switch)item.getActionView();
        actionView.setOnCheckedChangeListener(new OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
                status = isChecked;
                System.out.println(status);
            }
        });

        return true;   
    }
    return super.onOptionsItemSelected(item);
}
这是我的交换机布局和地图资源文件

switch_status.xml

<?xml version="1.0" encoding="utf-8"?>
<Switch
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:showText="true"
    android:textOff="Free"
    android:textOn="Booked" />    

map.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >

<item
    android:id="@+id/myswitch"
    android:showAsAction="always"
    android:title=""
    android:actionLayout="@layout/switch_status" /> 
</menu>

当点击开关时,它正在改变它的状态,但我在控制台上甚至看不到像“actionbar clicked”和“true”/“false”这样的消息。而且也没有错误

我不确定这是否是处理actionbar点击的正确方法。如果有人能帮我写这段代码,那就太好了。
感谢您使用工具栏执行此操作的最佳方法。

在工具栏中,您可以添加任何视图并控制它。

您不需要设置OnCheckedChangeListener,OnOptions ItemSelected是“onClickListener”

使用此开关:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    System.out.println("actionbar clicked");
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.myswitch:
           System.out.println(((Switch)item.getActionView()).isChecked());
        break;
    }
    return super.onOptionsItemSelected(item);
}

我有同样的问题,并通过以下技术实现
  • 为如下开关添加了onClick方法
  • <Switch
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:showText="true"
        android:textOff="Free"
        android:textOn="Booked"
        android:onClick="onSwitch"
    />
    

    你能给我看看样品吗?请这真是个好选择。但仍然需要了解它。
    public void onSwitch(View view) {
        Switch androidSwitch = findViewById(R.id.switch1);
        if(androidSwitch.isChecked()){
            Log.i("Test","enabled");
        }else{
            Log.i("Test","disabled");
        }
    }