首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带按钮的CheckBoxPreference

带按钮的CheckBoxPreference
EN

Stack Overflow用户
提问于 2015-12-23 12:09:34
回答 1查看 167关注 0票数 1

我在这里找到了我的问题的答案:CheckBoxPreference with additional Button?,但我真的不知道怎么做。可能是因为我不太懂英语。我需要放入ChekBoksPreferens按钮。有谁能举例说明如何做这件事?

代码语言:javascript
复制
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 ????
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2015-12-23 20:11:30

使用下面的代码将值保存在sharedpreferences中,然后通过单击按钮进行复选框验证。private SharedPreferences.Editor loginPrefsEditor;private SharedPreferences loginPreferences;//在onCreate之后

代码语言:javascript
复制
 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();

                        }

}


    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34428498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档