我在这里找到了问题的答案:带有附加按钮的复选框首选项?但我真的不知道该怎么做。可能是因为我不太懂英语。我需要输入ChekBoksPreferens按钮。有人能举个例子吗?
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class IconPreferenceScreen extends CheckBoxPreference {
private Drawable mIcon;
public IconPreferenceScreen(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference_icon);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconPreferenceScreen, defStyle, 0);
mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_myIcon);
}
@Override
public void onBindView(View view) {
super.onBindView(view);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
if (imageView != null && mIcon != null) {
imageView.getLayoutParams().height = 150;
imageView.getLayoutParams().width = 150;
imageView.setImageDrawable(mIcon);
}
}
public void setIcon(Drawable icon) {
if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
mIcon = icon;
notifyChanged();
}
}
public Drawable getIcon() {
return mIcon;
}
public class CheckBoxPreferenceSubclassWithButton{
???? what next ????
}
}
使用以下代码将值保存在sharedpreferences中,使用复选框验证并单击按钮。private SharedPreferences.Editor loginPreferencesEditor;private SharedPreferences loginPreferences;//创建之后
CheckBox saveCheckBox = (CheckBox) findViewById(R.id.test);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
// on button click
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.sunbmit:
if (saveCheckBox.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor
.putString("username", "value");
loginPrefsEditor
.putString("password", "value");
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}
}
}