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方法中的颜色更改。
答案 0 :(得分:1)
将所选颜色保存在SharedPreferences
中,然后在创建对话框时检索它。
或者只需检查size
中保存的SharedPreferences
是否大于0
,否则用户选择的值和颜色应该更改,否则不会更改。