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

时间:2016-01-06 06:18:36

标签: java android android-linearlayout android-alertdialog

单击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>

的OnClick:

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

爪哇:

    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;
    }

即使重新打开app,如何保留list方法中的颜色更改。

1 个答案:

答案 0 :(得分:1)

将所选颜色保存在SharedPreferences中,然后在创建对话框时检索它。 或者只需检查size中保存的SharedPreferences是否大于0,否则用户选择的值和颜色应该更改,否则不会更改。