Android 提示框 Dialog

1.简单的信息提示框

new AlertDialog.Builder(context)
                            .setTitle("离开页面提示")
                            .setMessage("存在未保存的数据,若离开此页面未保存的数据会清空,确认离开此页面吗?")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Intent intent = new Intent(context, RegionInfoActivity.class);
                                    startActivityForResult(intent, UPDATE_REGION);
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                }
                            }).show();

2.带简单输入框的dialog

final EditText inputServer = new EditText(RegionInfoActivity.this);

                AlertDialog.Builder builder = new AlertDialog.Builder(RegionInfoActivity.this);
                builder.setTitle("请输入地区名称")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(inputServer)
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String regionName = inputServer.getText().toString();
                                if(regionName.length()>10){
                                    Toast.makeText(RegionInfoActivity.this,"地区名称不能超过10个字符", Toast.LENGTH_SHORT).show();
                                }else{
                                    Log.i(TAG, "saveregionNAME" + regionName);
                                    addRegion(regionName);
                                }

                            }
                        });
                builder.show();
            }
        });

3.自定义布局的dialog
(1)编写dialog布局文件,按照自己需要写就行,没有什么固定限制
以dialog_export_tag.xml为例


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:text="导出信息确认"
        android:textColor="#000"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="所选地区:" />

        <TextView
            android:id="@+id/tv_export_region"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textColor="#000" />

    LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="请选择要导出的资产类型" />

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

            <LinearLayout
                android:id="@+id/ll_export_device_type"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">LinearLayout>
        ScrollView>
    LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:gravity="center_vertical"
            android:text="请选择要导出的文件格式" />

        <RadioGroup
            android:id="@+id/rg_file_type"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/xls"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                android:text=".xls" />

            <RadioButton
                android:id="@+id/txt"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_weight="1"
                android:text=".txt" />

        RadioGroup>

    LinearLayout>

    <LinearLayout
        android:id="@+id/ll_export_column"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical"
        android:visibility="gone">

        <TextView
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
           android:layout_height="20dp"
            android:gravity="center_vertical"
            android:text="请输入资产条码导出的列数" />

        <EditText
            android:id="@+id/et_column"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginRight="40dp"
            android:digits="1234567890"
            android:text="1">EditText>

    LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_export_confirm"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@drawable/btn_shape_corner_normal_themecolor"
            android:text="确定"
            android:textColor="#fff"
            android:textSize="14sp" />

        <Button
            android:id="@+id/btn_export_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:background="@drawable/btn_shape_corner_normal_themecolor"
            android:text="取消"
            android:textSize="14sp"
            android:textColor="#fff" />
    LinearLayout>

LinearLayout>

(2)在Activity中调用此布局

 private View exportTag;
 private AlertDialog exportTagDialog;
 	
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
        exportTag = View.inflate(this, R.layout.dialog_export_tag, null);
        builder.setView(exportTag);
        builder.setCancelable(true);
        exportTagDialog = builder.create();
        

另外, exportTagDialog.cancel();方法可将使得弹框消失。
exportTagDialog.show();可使弹框显示
参考:自定义android弹出框https://www.jianshu.com/p/5b2f8198ba30

你可能感兴趣的:(Android)