简体   繁体   English

如何在Android的AlertDialog按钮中添加按钮单击效果

[英]How to add button click effect in alertdialog buttons in android

I am having an alertdialog box in which i have customized the the background for the alertdialog buttons.In that i would like to add an effect for the button click can anyone tell me how can i add an button click effect for the alertdialog as well how could i change the size of the button in alert dialog. 我有一个alertdialog框,其中我自定义了alertdialog按钮的背景。我想为按钮单击添加效果,有人可以告诉我如何为alertdialog添加按钮单击效果吗?我可以更改警报对话框中按钮的大小吗?

  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            NewDaybook_Activity.this);

    // set title
    alertDialogBuilder.setTitle(R.string.app_name);

    // set dialog message
    alertDialogBuilder
            .setMessage(R.string.clickyestoexit)
            .setCancelable(false)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, close
                    // current activity
                    Intent a = new Intent(Intent.ACTION_MAIN);
                    a.addCategory(Intent.CATEGORY_HOME);
                    a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(a);
                }
            })
            .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();
    Button nbutton = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setTextColor(getResources().getColor(R.color.colorAccent));
    Button pbutton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(getResources().getColor(R.color.colorAccent));
    pbutton.setPadding(0, 10, 10, 0);
    pbutton.setTextColor(Color.WHITE);

在此处输入图片说明

Kitkat版本上方已经存在波纹效果,对于Kitkat之前的版本,请使用此来产生波纹效果。

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

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