App重新打开时不会保留布局颜色更改

Layout color change is not retained when App reopened

单击 TextView 时会打开警告对话框。然后,如果用户选择任何值,布局颜色应该改变,否则不会。这工作正常。但是当我关闭并重新打开选项卡时,颜色不会保留。如何在应用程序重新打开后保留颜色。

XML:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="horizontal"
    android:id="@+id/linearLayout1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"    >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/genere"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/genere"
    android:layout_gravity="center"
    android:text=" Search Genre"
    android:textSize="20sp"/>

</LinearLayout>

点击时:

Views mViews = new Views();
mViews.genere.setOnClickListener(this);
  public void onClick(View v) {
switch (v.getId()){
    case R.id.genere:
         ilist();
        break;

Java:

    public ArrayList<String> ilist() {

    final String[] ratings = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    final boolean[] ratingschecked = {false, false, false, false, false, false, false, false, false, false};
    SharedPreferences sharedPreferences = this.getSharedPreferences("checkedrate_i", Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPreferences.edit();

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Ratings");
    builder.setMultiChoiceItems(ratings, ratingschecked, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            if (isChecked) {

                   if(!ilist.contains(ratings[which])){
                       ilist.add(ratings[which]);

                    }

            } else if (ilist.remove(ratings[which])) {

                if(ilist.contains(ratings[which])){
                    ilist.remove(ratings[which]);
                }


            }
        }
    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

                for (int i = 0; i < ilist.size(); i++) {
                editor.putString("selectedratings" + i, String.valueOf(ilist.get(i)));
            }
            editor.putInt("size", ilist.size());

            editor.apply();



> if(!(ilist.isempty)){
>               //change gray color
>             }
>            else{
>              //Change white color.
>               }

        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    AlertDialog dialog = builder.create();
    builder.show();
    return ilist;
    }

如何在重新打开应用程序时保留在列表方法中所做的颜色更改。

SharedPreferences 中保存所选颜色,然后在创建对话框时检索它。 或者简单地检查您在 SharedPreferences 中保存的 size 如果它大于 0 那么用户选择的值和颜色应该改变否则不会。