警告对话框定制 [英] Alert Dialog Customization

查看:126
本文介绍了警告对话框定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是增加了一个警告对话框,来了点击返回按钮时。它设置为默认的Andr​​oid警报,我相信。反正是有自定义如何警告对话框的外观,如改变背景或设置一个可绘制的背景是什么?我是新来这个,所以我不知道该怎么做。谢谢你,我的code是低于我用的警告对话框。

I just added an Alert Dialog that comes up when the back button is clicked. It's set to the default android alert I believe. Is there anyway to customize how the alert dialog box looks such as change the background or set a drawable to the background? I am new to this so I am not sure what to do. Thanks, and my code is below that I used for the alert dialog.

提示对话框:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        //Handle the back button
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            //Ask the user if they want to quit
            new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(R.string.quit)
            .setMessage(R.string.really_quit)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    //Stop the activity and pause media player
                     mainSound.pause();
                    MainActivity.this.finish();    
                }

            })
            .setNegativeButton(R.string.no, null)
            .show();

            return true;
        }
        else {
            return super.onKeyDown(keyCode, event);
        }

    }

推荐答案

这样的..

创建XML布局

Create your xml layout

然后你可以用下面的建设者设置布局

And then you can set your layout on the builder with the following:

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup)    findViewById(R.id.dialog_layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);

编辑:

您应该重新安排你的code到这样的事情... 创建一个AlertDialog.Builder的一流水平。

You should rearrange your code to something like this... Create a AlertDialog.Builder at class level.

private AlertDialog.Builder builder;

在您的onCreate()创建AlertDialog

In your onCreate() create your AlertDialog

  LayoutInflater inflater = getLayoutInflater();
  View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup)     findViewById(R.id.dialog_layout_root));


  //Ask the user if they want to quit
        builder
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(R.string.quit)
        .setMessage(R.string.really_quit)
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                //Stop the activity and pause media player
                 mainSound.pause();
                MainActivity.this.finish();    
            }

        })
        .setNegativeButton(R.string.no, null)
        .setView(dailogLayout);





public boolean onKeyDown(int keyCode, KeyEvent event) {
    //Handle the back button
    if(keyCode == KeyEvent.KEYCODE_BACK) {

     builder.show();

        return true;
    }
    else {
        return super.onKeyDown(keyCode, event);
    }

}

这篇关于警告对话框定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆