Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android CheckedTextView状态_Android_Checkedtextview - Fatal编程技术网

Android CheckedTextView状态

Android CheckedTextView状态,android,checkedtextview,Android,Checkedtextview,我有以下问题: 我已经为CheckedTextView创建了自定义Android类: public class CustomCheckedTextView extends CheckedTextView { public CustomCheckedTextView(Context context) { super(context); this.setOnClickListener (new View.OnClickListener() {

我有以下问题: 我已经为
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
,背景将为绿色。但当我旋转手机时,背景又变白了。为什么会发生这种情况?

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

android:configChanges="orientation"

不要为此使用configChanges。了解它发生的原因以及如何保存状态非常重要。请仔细阅读

当您旋转设备时,会调用设备
onResume()
,这样您也可以在其中编写代码。@juned所以我需要检查某个数组中的所有状态,并在每次旋转后将它们设置回原位?还有其他解决方案吗?你可以使用blganes101的答案:)虽然blganes101的答案可以达到80%的效果,但有时事情会变得非常糟糕,因为谷歌表示,它应该作为最后一个选项使用,并且更喜欢savedInstanceState和restoreInstanceState。。检查此处的答案:注意:应避免使用此属性,并仅作为最后手段使用。有关如何正确处理由于配置更改而重新启动的更多信息,请阅读。