自定义警报框

时间:2012-12-11 06:12:21

标签: android

我想要一个完全自定义的警报框,我希望我的背景图像,我的自定义按钮和我的自定义消息,目前即时通讯使用默认警报框只有我的消息,但我希望它完全我之前说的自定义警报框

请帮助我,如果可能,请分享示例代码段 thnks:)

目前的代码是这样的: -

AlertDialog.Builder alertDialogBuilder3 = new AlertDialog.Builder(context);
                alertDialogBuilder3.setTitle("Location Check");
                alertDialogBuilder3
                .setMessage("Do you want to cancel loading?")
                .setCancelable(false)
                .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        LAtestTab.this.finish();
                    }
                })
                .setNegativeButton("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();
                    }
                });
                ;

                AlertDialog alertDialog3 = alertDialogBuilder3.create();

                alertDialog3.show();

2 个答案:

答案 0 :(得分:6)

这是Java Code ....

exit.setOnClickListener(new OnClickListener() 
{   
     @Override
     public void onClick(View arg0) 
    {
            final Dialog dialog1 = new Dialog(CatchTheCatActivity.this);
            dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog1.setContentView(R.layout.custom_alert);

            Button yes = (Button) dialog1.findViewById(R.id.button1);
            Button no = (Button) dialog1.findViewById(R.id.button2);

            yes.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) 
                {
                    finish();   
                }
            });
            no.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) 
                {
                        dialog1.dismiss();  

                }
            });
            dialog1.show();
   }
});

这是XML文件...(custom_alert)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="110dp"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" android:gravity="center">


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:src="@drawable/textmessage" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center|bottom" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/yes_button"/>


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:background="@drawable/no_button" />

    </LinearLayout>

</LinearLayout>

答案 1 :(得分:2)

参考此链接

http://www.mkyong.com/android/android-custom-dialog-example/

http://android-er.blogspot.in/2011/06/custom-alertdialog.html

您可以直接从Layout Inflater创建视图,只需使用布局XML文件的名称和文件中布局的ID。

<?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) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);