【声明】欢迎转载,但请保留文章原始出处:http://blog.csdn.net/yelangjueqi/article/details/46754711
import android.content.Context; import android.preference.SwitchPreference; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Switch; import com.wtk.gesture.utils.MyLogger; public class SmartSwitchPreference extends SwitchPreference { private static final String CLASS_TAG = MyLogger.APP_TAG + "/" + SmartSwitchPreference.class.getSimpleName(); public SmartSwitchPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public SmartSwitchPreference(Context context, AttributeSet attrs) { super(context, attrs); } public SmartSwitchPreference(Context context) { super(context); } @Override protected void onClick() { Log.d(CLASS_TAG, "onClick()"); } //下面这段代码,在Android L版本(5.0)之前是不需要的,在Android L版本上必须要有,否则switch获取不到点击事件 //此处废了我不少时间查找原因:从KK移植到L上面就不起作用了.因此L版本上面必须要有下面这段 @Override protected View onCreateView(ViewGroup parent) { View view = super.onCreateView(parent); Switch v = (Switch) view.findViewById(com.android.internal.R.id.switchWidget); if (v != null) { v.setClickable(true); } return view; } } |
调用Preference的setWidgetLayoutResource方法实现控件替换
<?xml version="1.0" encoding="utf-8"?> <Switch xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/prefrence_switch_id" android:layout_width="fill_parent" android:layout_height="fill_parent" > </Switch> |
import android.content.Context; import android.preference.SwitchPreference; import android.util.AttributeSet; import android.view.View; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Switch; import android.widget.Toast; import com.wtk.gesture.quick.R; import com.wtk.gesture.utils.MyLogger; public class GestureSwitchPreference extends SwitchPreference { private static final String CLASS_TAG = MyLogger.APP_TAG + "/" + GestureSwitchPreference.class.getSimpleName(); private Switch mSwitch; private boolean mChecked = false; private Context mContext; // // ///////////////////////////////////////////Custom Listenr Start // private OnRadioButtonCheckedListener mOnRadioButtonCheckedListener; // // public interface OnRadioButtonCheckedListener { // public void OnRadioButtonChecked(boolean isScreenOffView); // } // // public void setOnRadioButtonCheckedListener(OnRadioButtonCheckedListener // listener) { // mOnRadioButtonCheckedListener = listener; // } // // // ///////////////////////////////////////////Custom Listenr End public GestureSwitchPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; } public GestureSwitchPreference(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; //通过调用setWidgetLayoutResource方法来更新preference的widgetLayout,即更新控件区域 setWidgetLayoutResource(R.layout.smart_gesture_switch); } public GestureSwitchPreference(Context context) { super(context); mContext = context; //通过调用setWidgetLayoutResource方法来更新preference的widgetLayout,即更新控件区域 setWidgetLayoutResource(R.layout.smart_gesture_switch); } @Override protected void onBindView(View view) { mSwitch = (Switch) view.findViewById(R.id.prefrence_switch_id); //view即是代表的preference整个区域,可以对该view进行事件监听,也就是实现了preference整个区域的点击事件 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showToast("section-all"); //此处调用自定义的监听器A方法,该监听器A接口应由使用GestureSwitchPreference的类来实现,从而实现 //preference整个区域的点击事件.注:监听器A的定义可以参考OnRadioButtonCheckedListener接口的定义 } }); //switch开关的点击事件 if (mSwitch != null) { mSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton button, boolean checked) { mChecked = checked; showToast("only-switch-section"); //此处调用自定义的监听器B方法,该监听器B接口应由使用GestureSwitchPreference的类来实现,从而实现 //preference的switch点击事件.注:监听器B的定义可以参考OnRadioButtonCheckedListener接口的定义 } }); } setChecked(mChecked); super.onBindView(view); } public boolean isChecked() { return mChecked; } public void setChecked(boolean bChecked) { mChecked = bChecked; if (mSwitch != null) { mSwitch.setChecked(bChecked); } } private void showToast(String info) { Toast mToast = null; if (mToast == null) { mToast = Toast.makeText(mContext, info, 5000); } mToast.setText(info); mToast.show(); } } |
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <com.wtk.gesture.modules.GestureSwitchPreference android:key="GestureSwitchPreference" android:summary="概要" android:title="标题" /> </PreferenceScreen> |
public class SmartQuickGestureSettings extends PreferenceActivity { private static final String TAG = MyLogger.APP_TAG + "/" + SmartQuickGestureSettings.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.smart_quick_gesture_settings); } } |
点击switch:
import android.content.Context; import android.preference.Preference; import android.util.AttributeSet; import android.view.View; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; import com.wtk.gesture.quick.R; import com.wtk.gesture.utils.MyLogger; public class SmartGesturePrefrence extends Preference { private static final String CLASS_TAG = MyLogger.APP_TAG + "/" + SmartGesturePrefrence.class.getSimpleName(); public static boolean isScreenOffBtn = true;// default display gesture view private Context mContext; private OnRadioButtonCheckedListener mOnRadioButtonCheckedListener; public interface OnRadioButtonCheckedListener { public void OnRadioButtonChecked(boolean isScreenOffView); } public void setOnRadioButtonCheckedListener(OnRadioButtonCheckedListener listener) { mOnRadioButtonCheckedListener = listener; } public SmartGesturePrefrence(Context context) { this(context, null); } public SmartGesturePrefrence(Context context, AttributeSet attrs) { this(context, attrs, 0); mContext = context; } public SmartGesturePrefrence(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; setLayoutResource(R.layout.gesture_preference_layout); } @Override protected void onBindView(final View view) { super.onBindView(view); RadioGroup mRadioGroup = (RadioGroup) view.findViewById(R.id.radiogroup_gesture); RadioButton mScreenOffButton = (RadioButton) view.findViewById(R.id.btn_screen_off); RadioButton mPhoneCallingButton = (RadioButton) view.findViewById(R.id.btn_phone_calling); if (isScreenOffBtn) { mScreenOffButton.setChecked(true); } else { mPhoneCallingButton.setChecked(true); } mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.btn_screen_off: isScreenOffBtn = true; if (mOnRadioButtonCheckedListener != null) { mOnRadioButtonCheckedListener.OnRadioButtonChecked(true); } showToast("screen_off"); break; case R.id.btn_phone_calling: isScreenOffBtn = false; if (mOnRadioButtonCheckedListener != null) { mOnRadioButtonCheckedListener.OnRadioButtonChecked(false); } showToast("phone_calling"); break; } } }); } private void showToast(String info) { Toast mToast = null; if (mToast == null) { mToast = Toast.makeText(mContext, info, 5000); } mToast.setText(info); mToast.show(); } } |
<?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:gravity="center_vertical" android:minHeight="20dp" android:orientation="vertical" android:paddingEnd="?android:attr/scrollbarSize" android:paddingStart="?android:attr/scrollbarSize" > <RadioGroup android:id="@+id/radiogroup_gesture" android:layout_width="wrap_content" android:layout_height="52dip" android:layout_marginLeft="0dip" android:layout_marginRight="0dip" android:layout_marginTop="6dip" android:background="@android:color/black" android:gravity="center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/btn_screen_off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/zzz_radio_selector" android:button="@null" android:gravity="center" android:text="@string/title_mode_idle" android:textSize="16sp" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/zzz_gesture_tab_space" /> <RadioButton android:id="@+id/btn_phone_calling" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/zzz_radio_selector" android:button="@null" android:gravity="center" android:text="@string/title_mode_call" android:textSize="16sp" /> </RadioGroup> <TextView android:id="@+id/hint_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dip" android:layout_marginRight="6dip" android:ellipsize="marquee" android:fadingEdge="horizontal" android:singleLine="true" android:text="@string/gesture_operate_description" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="gone" /> </LinearLayout> |
3.效果图: