AlertDialog.Builder弹出各类窗口实现



activity_main.xml添加代码



<TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="用于显示用资源xml文件定制的AlertDialog" />

  

<Button

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:text="AlertDialog.Builder弹出XML自定义Layout窗口"

    android:id="@+id/btnAlertDialogWithXML"

    android:onClick="onClicknAlertDialogWithXML"

  

    android:layout_gravity="center_horizontal" />

  

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="没有资源的AlertDialog"

    android:id="@+id/btnAlertDialog_NoRes"

    android:onClick="onClickAlertNoRes"

    android:layout_gravity="center_horizontal" />

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="没有资源和按钮的AlertDialog"

    android:id="@+id/btnAlertDialog_NoResNoBtn"

    android:onClick="onClickAlertNoResNoBtn"

    android:layout_gravity="center_horizontal" />

 

创建资源文件custom_alert_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent" android:layout_height="match_parent">

  

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

  

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="警告对话框用于对客户发出警告信息,而不是用于输入信息,如果要输入信息建议用自定义对话框。自定义资源文件只是为了丰富提示信息。"

            android:layout_gravity="center_horizontal" />

  

  

    </LinearLayout>

  

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

  

  

    </LinearLayout>

  

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:id="@+id/imageView"

        android:src="@drawable/ic_launcher"

        android:layout_gravity="center_horizontal" />

  

</LinearLayout>

 

添加MainActivity.java代码

public class MainActivity extends AppCompatActivity {

  

    private  DialogInterface dialogWithXml;

    private TextView tvAlertDialgoUserName,tvAlertDialgoPassword;

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

  

    public void onClicknAlertDialogWithXML(View view) {

        LayoutInflater inflater = LayoutInflater.from(this);

        View layout=inflater.inflate(R.layout.custom_alert_dialog_layout, null);

        AlertDialog.Builder builder =new AlertDialog.Builder(this);

        builder.setView(layout);

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialog, int which) {

                dialog.cancel();

            }

        });

  

        // builder.setCancelable(false);  //设置不能按返回键取消

        dialogWithXml=builder.create();

        builder.show();

    }

  

    public void onClickAlertNoRes(View view) {

        AlertDialog.Builder myAlert=new AlertDialog.Builder(this);

        myAlert.setMessage("这里是AlertDialgo信息,点击确定退出")

                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override

                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                    }

                })

                .setTitle("这是标题")

                .setIcon(R.drawable.ic_launcher)

                .create();

  

        myAlert.show();

    }

  

    public void onClickAlertNoResNoBtn(View view) {

        AlertDialog.Builder myAlert=new AlertDialog.Builder(this);

        myAlert.setMessage("这里是AlertDialgo信息,点击外围自动取消")

                .create();

        myAlert.show();

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值