点击一次选中,点击二次取消(或第二个选中),并退出时保存状态

说到上面这个,好多人都做过这个,并知道使用SharedPreferences去保存状态,先上图

实现第一种情况

直接上代码,布局就不贴了,不想多说:

public class PrintSettintActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.agree_print_shape)
    ImageView mAgreePrintShape;
    @BindView(R.id.print_relyout)
    RelativeLayout mPrintRelyout;
    @BindView(R.id.not_agree_print_shape)
    ImageView mNotAgreePrintShape;
    @BindView(R.id.not_print_relyout)
    RelativeLayout mNotPrintRelyout;
    @BindView(R.id.print_text)
    TextView mPrintText;
    @BindView(R.id.no_print_text)
    TextView mNoPrintText;

    private boolean mIsClick;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_choice);
        ButterKnife.bind(this);
        mHeadTitleBar.setTitle("打印机");
        setBackEnabled("我的");
        //得到保存的标识状态
        mIsClick = PrintSettingUtils.get(this, PrintSettingUtils.IS_CKICK, false);
        mPrintRelyout.setOnClickListener(this);
        mNotPrintRelyout.setOnClickListener(this);
        //保存状态
        if(mIsClick){
            mPrintText.setTextColor(Color.parseColor("#8DC420"));
            mNoPrintText.setTextColor(Color.parseColor("#666666"));
            mAgreePrintShape.setVisibility(View.VISIBLE);
            mNotAgreePrintShape.setVisibility(View.GONE);
        }else{
            mPrintText.setTextColor(Color.parseColor("#666666"));
            mNoPrintText.setTextColor(Color.parseColor("#8DC420"));
            mAgreePrintShape.setVisibility(View.GONE);
            mNotAgreePrintShape.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBackClick() {
        finish();
    }

    @Override
    public void onClick(View v) {
        //控制变量
        mIsClick = !mIsClick;
        PrintSettingUtils.set(this,PrintSettingUtils.IS_CKICK,mIsClick);
        if(mIsClick){
            mPrintText.setTextColor(Color.parseColor("#8DC420"));
            mNoPrintText.setTextColor(Color.parseColor("#666666"));
            mAgreePrintShape.setVisibility(View.VISIBLE);
            mNotAgreePrintShape.setVisibility(View.GONE);
        }else{
            mPrintText.setTextColor(Color.parseColor("#666666"));
            mNoPrintText.setTextColor(Color.parseColor("#8DC420"));
            mAgreePrintShape.setVisibility(View.GONE);
            mNotAgreePrintShape.setVisibility(View.VISIBLE);
        }

实现第二种情况:
这里写图片描述

上代码:

public class PrintSettintActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.agree_print_shape)
    ImageView mAgreePrintShape;
    @BindView(R.id.print_relyout)
    RelativeLayout mPrintRelyout;
    @BindView(R.id.not_agree_print_shape)
    ImageView mNotAgreePrintShape;
    @BindView(R.id.not_print_relyout)
    RelativeLayout mNotPrintRelyout;
    @BindView(R.id.print_text)
    TextView mPrintText;
    @BindView(R.id.no_print_text)
    TextView mNoPrintText;

    private boolean mIsClick;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print_choice);
        ButterKnife.bind(this);
        mHeadTitleBar.setTitle("打印机");
        setBackEnabled("我的");
        //得到保存的标识状态
        mIsClick = PrintSettingUtils.get(this, PrintSettingUtils.IS_CKICK, false);
        mPrintRelyout.setOnClickListener(this);
        mNotPrintRelyout.setOnClickListener(this);
        //保存状态
        if(mIsClick){
            mPrintText.setTextColor(Color.parseColor("#8DC420"));
            mNoPrintText.setTextColor(Color.parseColor("#666666"));
            mAgreePrintShape.setVisibility(View.VISIBLE);
            mNotAgreePrintShape.setVisibility(View.GONE);
        }else{
            mPrintText.setTextColor(Color.parseColor("#666666"));
            mNoPrintText.setTextColor(Color.parseColor("#8DC420"));
            mAgreePrintShape.setVisibility(View.GONE);
            mNotAgreePrintShape.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBackClick() {
        finish();
    }

    @Override
    public void onClick(View v) {
         switch (v.getId()) {
            case R.id.print_relyout :
                if(!mIsClick) {
                    mPrintText.setTextColor(Color.parseColor("#8DC420"));
                    mNoPrintText.setTextColor(Color.parseColor("#666666"));
                    mAgreePrintShape.setVisibility(View.VISIBLE);
                    mNotAgreePrintShape.setVisibility(View.GONE);
                    mIsClick = true;
                    PrintSettingUtils.set(this,PrintSettingUtils.IS_CKICK,mIsClick);
                }else {
                    mPrintText.setTextColor(Color.parseColor("#666666"));
                    mNoPrintText.setTextColor(Color.parseColor("#666666"));
                    mAgreePrintShape.setVisibility(View.GONE);
                    mNotAgreePrintShape.setVisibility(View.GONE);
                    mIsClick = false;
                    PrintSettingUtils.set(this,PrintSettingUtils.IS_CKICK,mIsClick);
                }

                break;
            case R.id.not_print_relyout:
                if(mIsClick) {
                    mNoPrintText.setTextColor(Color.parseColor("#8DC420"));
                    mPrintText.setTextColor(Color.parseColor("#666666"));
                    mAgreePrintShape.setVisibility(View.GONE);
                    mNotAgreePrintShape.setVisibility(View.VISIBLE);
                    mIsClick = false;
                    PrintSettingUtils.set(this,PrintSettingUtils.IS_CKICK,mIsClick);
                }else {
                    mNoPrintText.setTextColor(Color.parseColor("#666666"));
                    mPrintText.setTextColor(Color.parseColor("#666666"));
                    mAgreePrintShape.setVisibility(View.GONE);
                    mNotAgreePrintShape.setVisibility(View.GONE);
                    mIsClick = true;
                    PrintSettingUtils.set(this,PrintSettingUtils.IS_CKICK,mIsClick);
                }
                break;
        }

二种情况合用工具类:

public class PrintSettingUtils {
    public static final String IS_CKICK = "is_Click"; // 标识
    /**
     * 获取配置
     */
    public static boolean get(Context context, String name, boolean defaultValue) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean value = prefs.getBoolean(name, defaultValue);
        return value;
    }

    /**
     * 保存用户配置
     * @return
     */
    public static boolean set(Context context, String name, boolean value) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(name, value);
        return editor.commit(); //提交
    }
}

以上,就是!很lower

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值