选择alertdialog项,然后在按下“确定”时更改Textview的颜色

时间:2016-01-04 10:08:02

标签: android textview alertdialog

我有一个textview,当我点击它时会打开一个警报multichoice项目,并允许我们选择它包含确定和取消按钮。现在,如果我选择alertdialog multichoice中的任何项目,那么我的textview颜色需要更改。我知道选择器,但如何选择项目更改textview颜色时使用它。

我的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;

的IList:

   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();

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

        }
    });

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

尝试使用选择器,但它无效。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff6ff57"/>
<item android:state_activated="true" android:color="#ffff1013"/>
<item android:color="#ff897fff" />
</selector>

1 个答案:

答案 0 :(得分:0)

创建全局TextView对象(oncreate方法外)

TextView txt_view;

然后在onCreate方法中初始化:

txt_view = findViewById(R.id.genere); 

然后在txt_view上设置onclick侦听器(初始化后)

txt_view.chat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ilist();
                }
            });

然后在onclick监听器内添加警告对话框的正按钮

txt_view.setColor(getResources().getColor(R.color.color_name));

您仍然可以使用getResources().getColor(R.color.color_name)但现在已将其停放,请参阅here以获取新方法。