如何在Android中创建自定义警报对话框? [英] How to create custom alert dialog in android?

查看:111
本文介绍了如何在Android中创建自定义警报对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个示例应用程序.我能够在具有一些标题和按钮的按钮单击上显示警报.但是现在我想显示一个具有用户名(标签)和文本字段(编辑字段)的弹出窗口和一个按钮. 单击按钮.我可以为此创建另一个弹出xml文件吗?

I am developing a sample app.I am able to show alert on button click having some tittle and button .But now I want to show a pop up window having username (Label) and text field (Edit field) and a button. on click on button.can I make another popup xml file for that ?

public void selfDestruct(View view) {
         // Kabloey
         Log.d("Naveen", "Test====");
         System.out.println("----------------------ghfgjhf-----------------");
         AlertDialog alertDialog = new AlertDialog.Builder(SecondActivity.this).create();
         alertDialog.setTitle("Reset...");
         alertDialog.setMessage("R u sure?");
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {

               //here you can add functions

            } });
         alertDialog.show();
     }

 

   <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

推荐答案

custom_dialog.xml

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/editText"
        android:hint="Enter some thing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <requestFocus />
    </EditText>

    <LinearLayout                
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"   
        android:layout_marginTop="10dp">     

        <Button
            android:id="@+id/save"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="SAVE" />

        <Button
            android:id="@+id/cancel"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Cancel" />

    </LinearLayout>
</LinearLayout>

需要膨胀custom_dialog.xml

final Dialog dialog = new Dialog(this);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Alert Dialog");

final EditText editText = (EditText) dialog.findViewById(R.id.editText);
Button btnSave          = (Button) dialog.findViewById(R.id.save);
Button btnCancel        = (Button) dialog.findViewById(R.id.cancel);
dialog.show();

这篇关于如何在Android中创建自定义警报对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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