警报对话框自定义

时间:2012-01-17 00:10:35

标签: java android eclipse android-alertdialog

我刚刚添加了一个单击后退按钮时出现的警报对话框。它被设置为我相信的默认android警报。是否有自定义警报对话框的外观,例如更改背景或将drawable设置为背景?我是新手,所以我不知道该怎么做。谢谢,我的代码低于我用于警告对话框的代码。

警报对话框:

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

    }

3 个答案:

答案 0 :(得分:3)

喜欢这个..

创建xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/dialog_layout_root"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:padding="10dp"
          >

然后,您可以使用以下内容在构建器上设置布局:

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

编辑:

您应该将代码重新安排为类似的内容...... 在班级创建一个AlertDialog.Builder。

private AlertDialog.Builder builder;

在你的onCreate()中创建你的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);
    }

}

答案 1 :(得分:1)

我会在这里写一个更详细的答案,但谷歌写的本教程比我更好:只需转到http://developer.android.com/guide/topics/ui/dialogs.html,然后转到创建自定义对话框。 这可能是谷歌为Android写的最好的教程之一。

答案 2 :(得分:0)

如何创建CustumDialog在Android文档中进行了解释:http://developer.android.com/guide/topics/ui/dialogs.html 在页面的底部,您可以找到一个名为“创建自定义对话框”的点。