簡體   English   中英

如何將偵聽器設置為操作欄中的開關?

[英]How to set a listener to a switch in the action bar?

我有這個菜單布局:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/actionBarSave"
        android:showAsAction="always"
        android:scaleType="fitXY"
        android:icon="@drawable/save_33x26"
        style="@style/ActionButtonStyle"
        android:title="save" />
    <item android:id="@+id/actionBarLoad"
        android:showAsAction="always"
        android:icon="@drawable/load70x24"
        android:scaleType="fitXY"
        android:title="load" />
    <item android:id="@+id/actionBarDelete"
        android:showAsAction="always"
        android:scaleType="fitXY"
        android:icon="@drawable/delete_enabled"
        android:title="delete" />
    <item
        android:id="@+id/actionBarSoundSwitch"
        android:title="soundswitch"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:showAsAction="always"
        android:actionLayout="@layout/sound_switch" /> 
</menu>

sound_switch布局文件:

<?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/soundSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SFX"
        android:layout_centerInParent="true" />

</RelativeLayout>

這是我嘗試向開關添加OnCheckedChangeListener 的java 代碼:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        soundSwitch = (Switch)findViewById(R.id.actionBarSoundSwitch);
        soundSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                soundOn = arg1;
            }

        });
        switch (item.getItemId()) {
            case R.id.actionBarSave:
                onSave();
                return true;
            case R.id.actionBarLoad:
                onLoad();
                return true;
            case R.id.actionBarDelete:
                onDelete();
                return true;
            case R.id.actionBarSoundSwitch:
                onSound();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

沒有 logcat 錯誤,但我已經檢查過並且偵聽器不工作。 任何想法為什么?

由於沒有您在何處添加ActionBar開關按鈕以及您的目標 API 級別的信息,因此我只是發布了我將如何添加一個的信息。

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.actionbarswitch"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.actionbarswitch.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

主活動.java:

package com.example.actionbarswitch;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    private static final String TAG = "ActionBarSwitch";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Switch actionBarSwitch = new Switch(this);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(actionBarSwitch, new ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_VERTICAL | Gravity.END));

        actionBarSwitch.setOnCheckedChangeListener(this);
    }


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

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.i(TAG, "isChecked? " + isChecked);
    }
}

LogCat 輸出:

I/ActionBarSwitch(10081): isChecked? true
I/ActionBarSwitch(10081): isChecked? false

截圖(取自 Nexus5):ActionBar 開關測試

希望這可以幫助。

確保首先使用R.menu.mymenuxmlonPrepareOptionsMenuR.menu.mymenuxml菜單,然后使用菜單而不是活動上下文找到您的視圖

soundSwitch = (Switch)menu.findViewById(R.id.actionBarSoundSwitch);

在 onPrepareOptionMenu 中試試這個

MenuItem item = menu.findItem(R.id.actionBarSoundSwitch);  
soundSwitch = (Switch)item.getActionView().findViewById(R.id.soundSwitch);

試試這個

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/actionBarSave"
    android:showAsAction="always"
    android:scaleType="fitXY"
    android:icon="@drawable/save_33x26"
    style="@style/ActionButtonStyle"
    android:title="save" />
<item android:id="@+id/actionBarLoad"
    android:showAsAction="always"
    android:icon="@drawable/load70x24"
    android:scaleType="fitXY"
    android:title="load" />
<item android:id="@+id/actionBarDelete"
    android:showAsAction="always"
    android:scaleType="fitXY"
    android:icon="@drawable/delete_enabled"
    android:title="delete" />
<item
    android:id="@+id/actionBarSoundSwitch"
    android:title="soundswitch"
    android:layout_centerInParent="true"
    android:scaleType="fitXY"
    android:actionViewClass="android.widget.ImageButton"
    android:showAsAction="always"
    android:actionLayout="@layout/sound_switch" /> 
</menu>

//------------------------------------------------ ----------

   public boolean onCreateOptionsMenu(Menu menu) {
   super.onCreateOptionsMenu(menu);
   getMenuInflater().inflate(R.menu.menu_search, menu);
 soundSwitch = (Switch)findViewById(R.id.actionBarSoundSwitch);
   soundSwitch .setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }
});
return true;

}

<item
        android:title="Switch"
        app:actionLayout="@layout/switch_item"
        app:showAsAction="always" />

如果我們去“@layout/switch_item”我們會發現

<androidx.appcompat.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

我在 SwitchCompat 中添加了一個 id 標簽作為

android:id="@+id/switch_darkmode"

然后在活動中添加了 CheckedChanged Listener

SwitchCompat darkmode=findViewById(R.id.switch_darkmode);
darkmode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        System.out.println("SWITCH TOGGLED");
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM