如何保存 Android 的可见性

我尝试保存卡片视图的可见性状态,起初我的视图消失了,但是如果用户按下按钮,卡片视图就会显示。有人可以帮助我并告诉我如何存储可见性,我找不到有关此主题的任何内容

太感谢了。

我尝试了 shardPrefs 但不起作用

@Override

protected void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    cv1= (CardView) findViewById(R.id.cv1);

    cv2= (CardView) findViewById(R.id.cv2);

    cv3= (CardView) findViewById(R.id.cv3);

    cv4= (CardView) findViewById(R.id.cv4);

    cv5= (CardView) findViewById(R.id.cv5);

    cv6= (CardView) findViewById(R.id.cv6);

    cv7= (CardView) findViewById(R.id.cv7);

    cv8= (CardView) findViewById(R.id.cv8);

    cv9= (CardView) findViewById(R.id.cv9);


    AddBar = findViewById(R.id.AddBar);

    AddVoorraad =findViewById(R.id.AddVoorraad);


    AddBar.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            clickcount = clickcount + 1;




            if (clickcount == 1) {


                cv1.setVisibility(View.VISIBLE);





            }


            if (clickcount == 2) {

                cv2.setVisibility(View.VISIBLE);

            }



            if (clickcount == 3) {

                cv3.setVisibility(View.VISIBLE);



            }

            if (clickcount == 4) {

                cv4.setVisibility(View.VISIBLE);



            }


            if (clickcount == 5) {

                cv5.setVisibility(View.VISIBLE);

            }



            if (clickcount == 6) {

                cv6.setVisibility(View.VISIBLE);



            }


            if (clickcount==7 ){

                cv7.setVisibility(View.VISIBLE);



            }


        }


    });


    AddVoorraad.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            clickcountV=clickcountV+1;


            if (clickcountV==1) {

                cv8.setVisibility(View.VISIBLE);



            }


            if (clickcount==2) {

                cv9.setVisibility(View.VISIBLE);

            }


        }

    });



鸿蒙传说
浏览 134回答 3
3回答

catspeake

尝试根据条件存储每个CardView内部的可见性。SharedPrefs并在 CardView 初始化期间检索并设置它们。检查下面的代码:SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);int isVisible = sharedPreferences.getInt("cv1", View.GONE);cv1.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv2", View.GONE);cv2.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv3", View.GONE);cv3.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv4", View.GONE);cv4.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv5", View.GONE);cv5.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv6", View.GONE);cv6.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv7", View.GONE);cv7.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv8", View.GONE);cv8.setVisibility(isVisible);isVisible = sharedPreferences.getInt("cv9", View.GONE);cv9.setVisibility(isVisible);AddBar.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        clickcount = clickcount + 1;        if (clickcount == 1) {            cv1.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv1", View.VISIBLE).commit();        }        if (clickcount == 2) {            cv2.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv2", View.VISIBLE).commit();        }        if (clickcount == 3) {            cv3.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv3", View.VISIBLE).commit();        }        if (clickcount == 4) {            cv4.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv4", View.VISIBLE).commit();        }        if (clickcount == 5) {            cv5.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv5", View.VISIBLE).commit();        }        if (clickcount == 6) {            cv6.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv6", View.VISIBLE).commit();        }        if (clickcount==7 ){            cv7.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv7", View.VISIBLE).commit();        }    }});AddVoorraad.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        clickcountV=clickcountV+1;        if (clickcountV==1) {            cv8.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv8", View.VISIBLE).commit();        }        if (clickcountV==2) {            cv9.setVisibility(View.VISIBLE);            sharedPreferences.edit().putInt("cv9", View.VISIBLE).commit();        }    }});

DIEA

当配置更改(旋转)时,您可以从 onSaveInstanceState 检索值,但是当您关闭应用程序(后退按钮)时,您需要从持久性存储中检索状态,例如共享首选项。

慕盖茨4494581

我不知道你到底尝试了什么,但如果你想做的只是存储状态以供以后使用,你当然可以使用 SharedPreferences 来实现这一点。您可以执行以下操作将状态保存为表示状态的字符串:SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();SharedPreferences.Editor editor = prefs.edit();  editor.putString("cv1_visibility", "VISIBLE");editor.putString("cv2_visibility", "INVISIBLE");editor.putString("cv3_visivility", "GONE");...editor.commit();然后你可以像这样读取这些值:SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();String cv1_state = prefs.getString("cv1_visibility", "NOT_FOUND");String cv2_state = prefs.getString("cv2_visibility", "NOT_FOUND");String cv3_state = prefs.getString("cv3_visibility", "NOT_FOUND");...然后根据活动开始时的状态进行适当的处理。请注意,getString()有一个defValue参数(第二个参数),该参数是首选项不存在时要返回的值。编辑:您可以选择将它们存储为 int,但请注意其中一些常量不是 int。看看这个就明白我的意思了:https ://developer.android.com/reference/android/view/View
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java