Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog

AlertDialog【警告对话框】:
图1:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第1张图片
图2:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第2张图片
图3:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第3张图片

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <Button
        android:id="@+id/btn_AlertDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:layout_gravity="center"
        android:onClick="showAD"
        android:text="一般的对话框"/>

</LinearLayout>

MainActivity.java :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showAD(View v){

        new AlertDialog.Builder(this)
                .setTitle("删除数据")   //设置标题
                .setMessage("你确定删除数据吗?")    //设置内容
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"删除数据",Toast.LENGTH_LONG).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"取消删除",Toast.LENGTH_LONG).show();
                    }
                })
                .show();        //  方法链调用

    }
    
}

点击运行即可。
AlertDialog 的常用 API:

show(): 显示对话框
AlertDialog.Builder :
create(): 创建 AlertDialog 对象
show(): 创建 AlertDialog 对象,同时将其显示出来
setTitle(): 设置标题
setMessage(): 设置内容
setPositiveButton():设置正面按钮
setNegativeButton():设置负面按钮
dismiss():移除 dialog
setSingleChoiceltems(…)设置单选项列表

—————————————————————————————————————

AlertDialog【列表对话框】:
图1:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第4张图片
图2:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第5张图片

activity_main.xml :

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

    <Button
        android:id="@+id/btn_AlertDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:layout_gravity="center"
        android:onClick="showWW"
        android:text="列表对话框"/>
    
</LinearLayout>

MainActivity.java :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void showWW(View v){

        final String[] items = {"红","蓝","绿","灰"};   //  final的变量在方法执行完后还存在

        new AlertDialog.Builder(this)
                .setTitle("指定背景颜色")
                .setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {    //  which就是选中的position
                        //  提示颜色
                        Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_LONG).show();
                        //  移除dilaog
                        dialog.dismiss();
                    }
                })
                .show();

    }

}

总结:

    
     *      重点:
     *      1.
     *      问题: 为什么我们可以直接利用接口类型参数来移除对话框?
     *
     *      分析: 注意观察在onClick 中第一个参数是一个 DialogInterface 接口类型的参数,那么为什么我们能直接利用这个参数移除掉AlertDialog
     *      因为,当列表对话框弹出后,这个方法就会传来 接口所对应的实现方法,那么这个对应的实现方法就是 AlertDialog ,所以可以利用其作为参数
     *      进行移除对话框     ——  原理: 多态
     *
     *      ——————————————————————————————————————————————————————————————————————
     *
     *     2.
     *      问题: 为什么这里的 items 必须要是 final ,否则就会报错?
     *
     *      分析: 因为如果 items 不是 final 类型,那么它就是一个局部变量,局部变量在方法执行完成后,就会从内存中释放,那么当我们再次
     *      点开这个列表对话框,想重新选择的时候,这个变量没了,还怎么读取的到数据呢?是不是呐!
     *      所以,我们要设成 final 类型,目的就是 为了让其变量还能保留在内存中!
     *
     

AlertDialog【自定义对话框】:

图1:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第6张图片

图2:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第7张图片
图3:
Android 实现对话框 警告对话框、列表对话框、自定义对话框 —— AlertDialog_第8张图片
activity_main.xml:

    <Button
        android:id="@+id/btn_zidingyi"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:layout_gravity="center"
        android:onClick="showCD"
        android:text="自定义对话框"/>

dialog_view.xml , 专门编写对话框的样式布局

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

    <ImageView
        android:id="@+id/img_imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_view"
        android:scaleType="fitXY"/>

    <!--scaleType  整个图片填充满上下-->

    <EditText
        android:id="@+id/edt_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="用户名"
        android:gravity="center"/>

    <EditText
        android:id="@+id/edt_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="密码"
        android:gravity="center"/>
    
</LinearLayout>

MainActivity.java

  public void showCD(View v){
        //  动态加载布局文件,得到对应的 View 对象
        View view = View.inflate(this,R.layout.dialog_view,null);
        //  问题1:    view的真是类型?  是布局文件根标签的类型,包含了子 View 对象
        //  问题2:    如何得到一个独立 View 的 子View ? view.findViewById(id)
                //  findViewById(id)是在 setContentView() 中的 View 中找,所以说我们在 MainActivity里,可以直接 findViewById 来获得到该组件
                //  就是因为MainActivity 最开始就已经加载了布局         setContentView(R.layout.activity_main)。

        final EditText edtName = view.findViewById(R.id.edt_username);
        final EditText edtPwd = view.findViewById(R.id.edt_password);

        new AlertDialog.Builder(this)
                .setView(view)
                .setNegativeButton("取消",null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //  读取用户名和密码
                        String username = edtName.getText().toString();
                        String password = edtPwd.getText().toString();
                        //  提示
                        Toast.makeText(MainActivity.this,"用户名:" + username + "\n密码"+password,Toast.LENGTH_LONG).show();
                    }
                })

                .show();
    }

你可能感兴趣的:(Android,学习)