简体   繁体   English

单击负号按钮时更改其颜色

[英]Change Color of Negative Button when It is clicked

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

                    }
                })

Hello I want to change the color of Button when I click the Negative button. 您好,当我单击否定按钮时,我想更改按钮的颜色。 So if Negative Button was red , and I click it it should change to Blue, and If I click Positive Button change Negative Button's color to red. 因此,如果“负片按钮”为红色,而我单击它则应更改为蓝色,如果我单击“正片按钮”,请将“负片按钮”的颜色更改为红色。 I dont know how to do this .... 我不知道该怎么做....

If I got it straight, you simply want to change the button's colour from red to blue and vice-versa each time someone presses the button. 如果我讲得很对,您只想在每次有人按下按钮时将按钮的颜色从红色更改为蓝色,反之亦然。 You were kind of ambiguous in the objective. 您在目标上有些big昧。

The easiest way I know of doing that is using a flag to see which colour is supposed to be on the button. 我知道的最简单的方法是使用标志来查看按钮上应该使用哪种颜色。 You also have the setBackgroundColor method that programmatically changes the color. 您还可以通过编程方式更改颜色的setBackgroundColor方法。

So I would do something like 所以我会做类似的事情

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

being that red and blue are custom colours. 红色和蓝色是自定义颜色。 You can make them in your colors.xml file. 您可以在colors.xml文件中创建它们。

You need to have the setBackground method inside onClick to make sure it's triggered. 您需要在onClick内部设置setBackground方法,以确保已触发它。

You can try this - 你可以试试这个-

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

try this: 尝试这个:

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

it helps you 它可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM