AndroidStudio无法更改AlertDialogJAVA中按钮的颜色。
创始人
2024-10-11 20:30:08
0

在AlertDialog中,如果要自定义按钮的颜色,可以采用如下代码:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Message")
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // do something
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // do something
           }
       });

AlertDialog alertDialog = builder.create();
alertDialog.show();

Button positiveButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
positiveButton.setTextColor(getResources().getColor(R.color.colorAccent));

Button negativeButton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
negativeButton.setTextColor(getResources().getColor(R.color.colorPrimary));

其中,首先需要创建一个AlertDialog.Builder实例,然后通过设置其按钮的颜色即可。使用getButton()方法来获取对应的按钮,然后通过setTextColor()方法来设置颜色。

需要注意的是,颜色值需要从资源文件中获取,例如从color.xml文件中获取颜色值,如上面的代码所示。

相关内容

热门资讯

透视科技"德州局透视... 您好,德州局透视这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游...
透视攻略"hhpok... 透视攻略"hhpoker怎么防作弊"详细辅助专业教程(辅助器免费辅助);亲真的是有正版授权,小编(透...
透视游戏"福建大菠萝... 此外,数据分析德州(福建大菠萝万能辅助器)辅助神器app还具备辅助透视行为开挂功能,通过对客户福建大...
透视ai代打"智星德... 透视ai代打"智星德州辅助译码插件靠谱吗"详细辅助必备教程(-h5下载方法);支持多人共享记分板与复...
透视辅助"佛手在线大... 透视辅助"佛手在线大菠萝辅助"详细辅助新2025版(的辅助器方法)1、很好的工具软件,可以解锁游戏的...
透视私人局"wepo... 透视私人局"wepokerplus作弊"详细辅助必赢方法(底牌透视透明);人气非常高,ai更新快且高...
透视美元局"德普之星... 透视美元局"德普之星辅助软件"详细辅助玩家教你(透视脚本下载方法);最新版2024是一款经典耐玩的益...
透视透视"智星菠萝透... 透视透视"智星菠萝透视"详细辅助wpk教程(有用神器)关于智星菠萝透视机制的,其中提到了后台系统,玩...
透视插件"wepok... 您好,wepoker私人局开挂视频这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302...
透视辅助"poker... 透视辅助"pokernow辅助工具"详细辅助揭秘教程(私人局辅助器可以用透视)准备好在pokerno...