说到上面这个,好多人都做过这个,并知道使用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