簡體   English   中英

用於SwitchPreferenc的android自定義切換小部件

[英]android custom Switch widget for SwitchPreferenc

我搜索stackoverflow並找到下一個相關主題:

  1. 我如何設計Android Switch風格?
  2. Android 4中的自定義切換小部件
  3. 設置switchStyle - 找不到錯誤資源 - 為什么?

我還在google group上找到bugreport: 問題36636:無法覆蓋樣式switchStyle最后使用Switch小部件找到新的問題:

  • 我嘗試創建自己的Preference.SwitchPreference並使用Switch小部件定義布局

     android:id="@+android:id/switchWidget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/switch_thumb" android:layout_gravity="center" android:padding="16dip" android:focusable="false" /> 

但我得到一個compliation錯誤:錯誤:資源不公開。 ('id'的值為' @ + android:id / switchWidget ')。 所以我不能用這種方式。

  • 第二種方法我嘗試擴展Switch類從代碼中添加set資源。 但是我發現方法setThumbResource只能從API 16中獲得。但我仍然無法應用@ + android:id / switchWidget,因為它不公開。

那么,我如何獲得SDK API 15的自定義切換首選項??? 或者我如何自定義切換首選項?

剛剛找到了實現這一目標的糟糕方法。

首先, src / com / myapp / views / preference / MySwitchPreference.java

public class MySwitchPreference extends SwitchPreference {
    public MySwitchPreference(Context context) {
        super(context);
    }

    public MySwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        if (view instanceof ViewGroup) {
            setLayout((ViewGroup) view);
        }
    }

    @SuppressLint("NewApi")
    private void setLayout(ViewGroup viewGroup) {
        int count = viewGroup.getChildCount();
        for(int n = 0; n < count; ++n) {
            View childView = viewGroup.getChildAt(n);
            if(childView instanceof Switch) {
                final Switch switchView = (Switch) childView;

                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                    switchView.setThumbResource(com.myapp.R.drawable.switch_inner);
                    switchView.setTrackResource(com.myapp.R.drawable.switch_track);
                }
                return;
            }
            else if (childView instanceof ViewGroup)
                setLayout((ViewGroup) childView);
        }
    }
}

現在, res / xml / preferences.xml

<com.myapp.views.preference.MySwitchPreference
            android:switchTextOff="Off"
            android:switchTextOn="On"
            android:title="whatever"
            android:key="switch" />

有點棘手,只能使用Android> 16。

不太了解切換問題,但您可以使用ToggleButton ,如下所示:

在布局中定義按鈕:

<ToggleButton
            android:id="@+id/your_awesome_toggle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:gravity="center_vertical|center_horizontal"
            android:layout_marginRight="15dp"
            android:textOn=""
            android:textOff=""
            android:background="@drawable/toggle_button"
        />

創建一個選擇器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:state_checked="false" 
        android:state_focused="false"
        android:drawable="@drawable/switch_off_btn" />
    <item 
        android:state_checked="true" 
        android:state_focused="false" 
        android:drawable="@drawable/switch_on_btn" />
    <item 
        android:drawable="@drawable/switch_off_btn" />
</selector>

OnClickListener

    toggleOnOff = (ToggleButton) findViewById(R.id.your_awesome_toggle);

    toggleOnOff.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            updateButtons();
            if(toggleOnOff.isChecked()){
                 SharedPreferences emailPrefs = getSharedPreferences(rememberToggleOnOff,MODE_PRIVATE);
                 SharedPreferences.Editor editor = yourPrefs.edit();
                 editor.putBoolean("mon", true);
                 editor.commit();
            }
            else {
                SharedPreferences emailPrefs = getSharedPreferences(rememberToggleOnOff,MODE_PRIVATE);
                SharedPreferences.Editor editor = yourPrefs.edit();
                editor.putBoolean("mon", false);
                editor.commit();
            }
        }
    });
    checkToggleState();

checkToggleState方法:

/**
     * Checks the state of the Toggle button preferences.  
     * If preferences are true set the toggle to on, if false set the toggle off.  
     * 
     */
    private void checkToggleState() {
        SharedPreferences yourPrefs = getSharedPreferences(rememberToggleOnOff,MODE_PRIVATE);
        boolean mON = yourPrefs.getBoolean("mon", true);
        if(mON) {
            toggleOnOff.setChecked(true);
        }
        else {
            toggleOnOff.setChecked(false);
        }
    }

更改:

android:id="@+android:id/switchWidget"

至:

android:id="@+id/switchWidget"

這里可以找到一個簡單的開關示例。

Switch小部件僅支持14級及以上API級別,但如果您想使用Switch Preference pre API級別14,請選中此項

更新:如果你想為自己的開關設置樣式,試試這個

繼承SwitchPreference類並在preferences.xml中使用它,其布局指向您的自定義布局。 然后在繼承的SwitchPreference類的onBind方法中,您可以通過id和set listeners找到相應的視圖。 不要忘記在onBind()中調用super。

更改:

android:id="@+android:id/switchWidget"

至:

android:id="@*android:id/switchWidget"

暫無
暫無

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

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