【发布时间】:2012-03-19 08:08:23
【问题描述】:
在 API 级别 14 之前,没有切换首选项。如果我使用preferences.xml 来创建我的偏好屏幕,有什么方法可以区分API 级别吗?那么有一个旧版本的复选框和一个 API 14 的开关?
最好的方法是什么?
【问题讨论】:
标签: android preferences
在 API 级别 14 之前,没有切换首选项。如果我使用preferences.xml 来创建我的偏好屏幕,有什么方法可以区分API 级别吗?那么有一个旧版本的复选框和一个 API 14 的开关?
最好的方法是什么?
【问题讨论】:
标签: android preferences
如果我使用preferences.xml 创建我的偏好屏幕,有什么方法可以区分API 级别吗?那么有一个旧版本的复选框和一个 API 14 的开关?
创建一个包含preferences.xml 的res/xml-v14/ 目录,其中包含您的SwitchPreference。创建一个包含preferences.xml 文件的res/xml/ 目录,该文件将SwitchPreference 替换为CheckBoxPreference。 Android 将根据运行应用的设备版本加载正确的 preferences.xml 文件版本。
【讨论】:
_switchPreference,而不仅仅是显示的那些。否则,其他方法将适用于基础 CheckBoxPreference 内容,您可能会不同步。
您还可以使用 android-switch-backport 库,它具有适用于 Android 2.1+ 的 SwitchPreference。
【讨论】:
gradle 时,我的项目卡在compiling dependencies 上。
有一个解决方法,我不确定它有多满,通过在 CheckBoxPreference 中包装要使用的视图(可能会错过一些功能,但在一般使用中,它可以工作)。
解决方法将使用针对 pre-API-14 的 CheckBoxPreference 和针对 API 14 及更高版本的 SwitchPreference。
代码如下:
public class SwitchPreference extends CheckBoxPreference
{
android.preference.SwitchPreference _switchPreference =null;
public SwitchPreference(final Context context)
{
super(context);
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
_switchPreference=new android.preference.SwitchPreference(context);
}
public SwitchPreference(final Context context,final AttributeSet attrs)
{
super(context,attrs);
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
_switchPreference=new android.preference.SwitchPreference(context,attrs);
}
public SwitchPreference(final Context context,final AttributeSet attrs,final int defStyle)
{
super(context,attrs,defStyle);
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
_switchPreference=new android.preference.SwitchPreference(context,attrs,defStyle);
}
@Override
protected View onCreateView(final ViewGroup parent)
{
final View view;
if(VERSION.SDK_INT>=VERSION_CODES.ICE_CREAM_SANDWICH)
{
view=_switchPreference.getView(null,parent);
// set as checked the view and the view's children, each in case it extend from Checkable
ViewUtil.setChecked(view,isChecked());
// set as non-clickable the view and the view's children
ViewUtil.setClickable(view,false);
}
else view=super.onCreateView(parent);
return view;
}
【讨论】:
试试这个代码:
public class SettingsActivity extends PreferenceActivity {
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_settings);
PreferenceScreen rootScreen = getPreferenceManager()
.createPreferenceScreen(this);
setPreferenceScreen(rootScreen);
Preference NotifCheck=null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
NotifCheck = new SwitchPreference(this);
} else {
NotifCheck = new CheckBoxPreference(this);
}
NotifCheck.setKey("ShowNotification");
NotifCheck.setTitle(R.string.ShowNotification);
NotifCheck.setEnabled(true);
rootScreen.addPreference(NotifCheck);
// Show the Up button in the action bar.
setupActionBar();
}
}
【讨论】:
您可以使用 SwitchCompat:
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"
app:showText="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
在 setOnCheckedChangeListener 上:
SwitchCompat switchCompat = (SwitchCompat)convertView.findViewById(R.id.switch_compat);
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
textView.setText("check");
} else {
textView.setText("unCheck");
}
}
});
希望对你有帮助。
【讨论】: