标签:
checkbox改变时让字改变
public class Setup4Activity extends BaseSetupActivity {
private CheckBox cbProtect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup4);
cbProtect = (CheckBox) findViewById(R.id.cb_protect);
boolean protect = mPref.getBoolean("protect", false);
// 根据sp保存的状态,更新checkbox
if (protect) {
cbProtect.setText("防盗保护已经开启");
cbProtect.setChecked(true);
} else {
cbProtect.setText("防盗保护没有开启");
cbProtect.setChecked(false);
}
// 当checkbox发生变化时,回调此方法
cbProtect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
cbProtect.setText("防盗保护已经开启");
mPref.edit().putBoolean("protect", true).commit();
} else {
cbProtect.setText("防盗保护没有开启");
mPref.edit().putBoolean("protect", false).commit();
}
}
});
}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4921684.html