Android CheckedTextView状态

时间:2013-06-24 08:42:24

标签: android checkedtextview

我遇到了以下问题: 我为CheckedTextView创建了自定义Android类:

public class CustomCheckedTextView extends CheckedTextView {
    public CustomCheckedTextView(Context context) {
        super(context);
                this.setOnClickListener (new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ((CheckedTextView) v) .toggle();
                        if (isChecked()){
                            setBackgroundColor(Color.GREEN);
                        } else {
                            setBackgroundColor(Color.RED);
                        }
                        }

            }) ;
        }
}

并在主要活动中使用它如下:

 LinearLayout llayout = (LinearLayout) findViewById(R.id.linearLayout1);
 final CustomCheckedTextView checkedTV = new CustomCheckedTextView(this);
 llayout.addView(checkedTV)

所以我可以点击CustomCheckedTextView,背景将是绿色。但是当我旋转手机背景变成白色时。为什么会这样?

2 个答案:

答案 0 :(得分:1)

请勿使用configChanges。了解它为何发生以及如何保存状态非常重要。请阅读documentation on this topic

答案 1 :(得分:-1)

将其添加到您的AndroidManifest.xml文件

android:configChanges="orientation"
相关问题