单击时更改负按钮的颜色

时间:2018-04-30 03:27:34

标签: java android

.setNegativeButton("Favorite", new DialogInterface.OnClickListener() {
                    @Override

                    public void onClick(DialogInterface dialogInterface, int i) {
                        fav=true;
                        user = db.userDao().findByName(email);
                        if(user.getmFavorite()==null)
                        {
//                            Log.i("Check List", user.getmFavorite());
                            Log.i("Number1", "Numer1");
                            //String favorite = user.getmFavorite();
                            user.setmFavorite(newFav);
                            Context context = getActivity();
                            fav = true;
                            toast = Toast.makeText(context, "Successfully Favorited", Toast.LENGTH_SHORT);
                            toast.show();
                            Log.i("Check List", user.getmFavorite());

                            db.userDao().Update(user);
                        }
                        else{

                            String favorites = user.getmFavorite();
                            tokenize(favorites);
                            Set<String> set = new HashSet<String>(items);
                            if(set.contains(newFav))
                            {
                                Log.i("Number221", "Numer221");
                                Log.i("Check List", user.getmFavorite());
                                Context context = getActivity();
                                fav = true;
                                toast = Toast.makeText(context, "This user is already favorite", Toast.LENGTH_SHORT);
                                toast.show();
                            }
                            else {
                                Log.i("Number331", "Numer331");
                                Log.i("Check List", user.getmFavorite());
                                String fav2 = favorites + "," + newFav;
                                user.setmFavorite(fav2);
                                Context context = getActivity();
                                fav = true;
                                toast = Toast.makeText(context, "Successfully Favorited", Toast.LENGTH_SHORT);
                                toast.show();
                                Log.i("Check List2", user.getmFavorite());
                                db.userDao().Update(user);
                            }
                        }

                    }
                })

您好我想在单击否定按钮时更改Button的颜色。 因此,如果“否定”按钮为红色,我单击它应该更改为蓝色,如果我单击“正面按钮”,则将“否定按钮”的颜色更改为红色。我不知道该怎么做....

3 个答案:

答案 0 :(得分:0)

如果我直截了当,你只需要在每次有人按下按钮时将按钮的颜色从红色变为蓝色,反之亦然。你的目标很模糊。

我知道这样做最简单的方法是使用一个标志来查看按钮上应该有哪种颜色。您还有以编程方式更改颜色的setBackgroundColor方法。

所以我会做类似

的事情
if(!isRed)
btn.setBackgroundColor(getResources().getColor(R.color.red));
else
btn.setBackgroundColor(getResources().getColor(R.color.blue));

红色和蓝色是自定义颜色。您可以在colors.xml文件中创建它们。

你需要在onClick中使用setBackground方法以确保它被触发。

答案 1 :(得分:0)

你可以试试这个 -

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            if(clicked)
            alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.YOUR_COLOR_NAME));
        }
    });

答案 2 :(得分:0)

试试这个:

public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View alertDialog= View.inflate(this.getActivity(), R.layout.alert_dialog,
                                        null);

builder.setTitle(getLocalizedString("Message"))
        .setView(eventEditDialogView)
        .setPositiveButton(getLocalizedString("Ok"), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
            }
        })
        .setNegativeButton(getLocalizedString("Cancel"), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        })
return builder.create();
 }

 @Override
 public void onStart() {
    super.onStart();
 Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
 positive.setTextColor(Color.BLACK);
 positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor));
 }

它可以帮助你