我自定义了一个控件MyCheckBox继承了LinearLayout,里面有两个textView和一个checkbox,textview可设置,checkbox却不行,意思就是调用它的setChecked方法状态却无改变,代码如下
public MyCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_checkbox, this);
text1 = (TextView)findViewById(R.id.mycheckbox_text_01);
text2 = (TextView)findViewById(R.id.mycheckbox_text_02);
myCB = (CheckBox)findViewById(R.id.my_checkbox);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCheckBox);
String str1 = ta.getString(R.styleable.MyCheckBox_cb_text1);
String str2 = ta.getString(R.styleable.MyCheckBox_cb_text2);
SharedPreferences sp = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
Boolean b = sp.getBoolean("notification", true);
text1.setText(str1);
text2.setText(str2);
myCB.setChecked(b);
ta.recycle();
}b的值取出来是true,可checkbox一直都没勾上在调用这个控件的Activity同样不能设置~
请问这是什么原因?
public MyCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_checkbox, this);
text1 = (TextView)findViewById(R.id.mycheckbox_text_01);
text2 = (TextView)findViewById(R.id.mycheckbox_text_02);
myCB = (CheckBox)findViewById(R.id.my_checkbox);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCheckBox);
String str1 = ta.getString(R.styleable.MyCheckBox_cb_text1);
String str2 = ta.getString(R.styleable.MyCheckBox_cb_text2);
SharedPreferences sp = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
Boolean b = sp.getBoolean("notification", true);
text1.setText(str1);
text2.setText(str2);
myCB.setChecked(b);
ta.recycle();
}b的值取出来是true,可checkbox一直都没勾上在调用这个控件的Activity同样不能设置~
请问这是什么原因?
第二,你在Activity中设置完CheckBox的状态后,调用一下postInvalidate()这个方法试一下
希望成功
没有用模拟器~我本意是想用过点击它的父控件也能对其进行设置的,但是却发现不可以~ 是什么原因,不能在外部对checkbox使用setChecked?