AlertDialog的几种使用形式

AlertDialog的几种形式

1.基本的样式
	<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showAlertDialog1"
        android:text="3个按钮的AlertDialog" />

    public void showAlertDialog1(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Title");
        builder.setMessage("Message");
        builder.setIcon(R.mipmap.ic_launcher);

        builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {		//正面的按钮,如 确定,是 之类的
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Positive", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("Negative", new DialogInterface.OnClickListener() {		//反面的按钮,如 取消,否 之类的	
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Negative", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {		//中立的按钮,如 不确定 之类的
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Neutral", Toast.LENGTH_SHORT).show();
            }
        });
//        AlertDialog alertDialog = builder.create();		//这两句与下面的一句相同
//        alertDialog.show();

        builder.show();
    }



2.列表的样式
	<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showAlertDialog2"
        android:text="列表的AlertDialog" />

public void showAlertDialog2(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Title");
//      builder.setMessage("Message"); 这句不能加,加上就不会显示列表了

        final String s[] = {"Android", "IOS", "Windows Phone"};
        builder.setItems(s, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }


3.选项列表的样式
	<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showAlertDialog3"
        android:text="选项列表的AlertDialog" />

public void showAlertDialog3(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        final String s[] = {"Android", "IOS", "Windows Phone"};
        final ArrayList<String> list = new ArrayList<>();
        builder.setMultiChoiceItems(s, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if(isChecked)
                    list.add(s[which]);
                else
                    list.remove(s[which]);
            }
        });
        builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, list.toString(), Toast.LENGTH_SHORT).show();
                list.clear();
            }
        });
        builder.show();

    }


4.单选列表的样式
	<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showAlertDialog4"
        android:text="单选列表的AlertDialog" />

    public void showAlertDialog4(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Title");
        final String s[] = {"Android", "IOS", "Windows Phone"};
        builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() { 	//0代表初始是选择第一个
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }


5.自定义的样式

	<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showAlertDialog5"
        android:text="自定义的AlertDialog" />
	
在drawable目录下新建alert_dialog_layout xml布局文件

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

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:hint="Username"
        android:inputType="text" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:hint="Password"
        android:inputType="textPassword" />

</LinearLayout>


   public void showAlertDialog5(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        final View view = getLayoutInflater().inflate(R.layout.alert_dialog_layout, null); //注意,需定义为final
        builder.setView(view);
//        builder.setView(R.layout.alert_dialog_layout);  在5.0以上使用
        builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                EditText username = (EditText) view.findViewById(R.id.username); //在view中findId
                EditText password = (EditText) view.findViewById(R.id.password);
                String string = username.getText().toString() +"---"+ password.getText().toString();
                Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值