您的位置:首页 > 移动开发 > Android开发

android 自定义checkbox怪异事件(未完)

2015-12-07 13:17 441 查看
 自定义的控件发生怪事,若有人知道麻烦告知下。

后来试验发现在onResume()方法中初始化siv选择状态,就没有这些问题。可能和activity生命周期有关。

自定义一个包含checkbox的控件。

控件布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:background="@drawable/item_setting_selector"

    android:layout_height="50dip" >

    

    <TextView

        android:id="@+id/tv_setting_item"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginLeft="10dip"

        android:layout_centerVertical="true"

        android:text="这是什么么功能"

        android:textSize="18sp" />

    <CheckBox

        android:id="@+id/cb_setting_item"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         android:layout_marginRight="10dip"

        android:layout_alignParentRight="true"

        android:layout_centerVertical="true"

        android:clickable="false"

        android:focusable="false"/>

    

    <View

        android:layout_marginLeft="5dip"

        android:layout_marginRight="5dip"

        android:layout_below="@id/cb_setting_item"

        android:layout_width="wrap_content"

        android:layout_height="1dip"

        android:background="#44000000"/>

</RelativeLayout>

控件源码:

public class SettingItemView extends LinearLayout {
private TextView tv;
private CheckBox cb;

public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
String text = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.mobilesecurity", "mytext");
tv.setText(text);
}

public SettingItemView(Context context) {
super(context);
initView(context);
}

private void initView(Context context) {
this.setOrientation(LinearLayout.VERTICAL);
View view = View.inflate(context, R.layout.item_setting, null);
tv = (TextView) view.findViewById(R.id.tv_setting_item);
cb= (CheckBox) view.findViewById(R.id.cb_setting_item);
this.addView(view);
}

public  boolean isChecked(){
return cb.isChecked();
}

public void setChecked(boolean checked){
//System.out.println(checked+"    "+",  cb ="+cb.toString()+",  SettingItemView = "+this.toString());
cb.setChecked(checked);
}

}

加入activity中

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
siv_update = (SettingItemView) findViewById(R.id.siv_update_setting);
siv_blacknumber = (SettingItemView) findViewById(R.id.siv_blacknumber_setting);

siv_update.setChecked(sp.getBoolean("update", false));
siv_blacknumber.setChecked(sp.getBoolean("black", false));

siv_update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Editor ed = sp.edit();
if(siv_update.isChecked()){
//System.out.println("
4000
取消自动更新");
siv_update.setChecked(false);
ed.putBoolean("update", false);

}else{
//System.out.println("开启自动更新");
siv_update.setChecked(true);
ed.putBoolean("update", true);
}
ed.commit();
}
});

siv_blacknumber.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Editor ed = sp.edit();
if(siv_blacknumber.isChecked()){
siv_blacknumber.setChecked(false);
ed.putBoolean("black", false);

}else{
siv_blacknumber.setChecked(true);
ed.putBoolean("black", true);
}
ed.commit();
}
});
}



最小化该界面,在app管理中停止该应用。长按home键,重新打开该界面。会出现奇怪的现象。这两个复选框都选上了。但退出后再打开该界面又正确了。



很好奇,为什么会这样。我把自定义控件中setChecked()方法加入输出语句,发现没有问题,是一个false,一个true。但显示的界面还是两个都勾选上了。我又试了原生的CheckBox,是不会出现这种现象的。不知道为什么会出现这样的情况?

隐隐感觉可能是Activity在创建activity时候调用的我不知道的方法,修改了checkbox的状态。我想到onSaveInstanceState()和onRestoreInstanceState(),我把这个两个方法任意一个方法屏蔽了,竟然发现是对了。具体原理还不清楚。待以后深入学习android后,再说了。

protected void onSaveInstanceState(Bundle outState) {
System.out.println("onSaveInstanceState : " + siv_blacknumber.toString() + "   "
+ siv_update.toString());
//super.onSaveInstanceState(outState);
}

protected void onRestoreInstanceState(Bundle savedInstanceState) {
System.out.println("onRestoreInstanceState : " + siv_blacknumber.toString() + "   "
+ siv_update.toString());
//super.onRestoreInstanceState(savedInstanceState);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: