Android开发----对话框

132 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

一.一个简单对话框的实现

1.先新建一个新的项目MyDialog

2.新建一个java类---StartGameDialogFragment,让其继承DialogFragment类

3.先简单写一个对话框的builder:

public class StartGameDialogFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setMessage("测试对话框").setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        return builder.create();
    }
}

4.下面开始绘制对话框,在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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="showDialog"//设置监听事件onClick()
        android:text="显示对话框">
    </Button>

</LinearLayout>

刚刚写的showDialog方法要在MainActivity里面创建对应的方法:

image.png

所以目前MainActivity.java实现类的代码如下:

public class MainActivity extends AppCompatActivity {

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

    public void showDialog(View view) {
        StartGameDialogFragment startGameDialogFragment=new StartGameDialogFragment();
        startGameDialogFragment.show(getSupportFragmentManager(),"");
    }
}

下面开始测试运行一下:

image.png

image.png

以上就是一个简单对话框的实现啦!

二.对话框的实现延伸

1.可以为对话框增加一个标题:

设置一个属性setTitle()即可

image.png

效果图:

image.png

其实也就是setTitle()设置标题区,setMessage()设置内容区

2.按钮也是分三种,肯定,否定,中性

例如设置一个中性按钮:

image.png

public class StartGameDialogFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setTitle("这是第一个对话框").setMessage("测试对话框").setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNeutralButton("中性按钮", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
            });
        return builder.create();
    }
}

3.对话框设置列表,单选框

需要注意,设置列表其实就是在替换对话框的内容区域,所以说不能再写setMessage()了,写setItem()即可:

image.png

public class StartGameDialogFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());

        String [] items={"red","blue","green"};
        builder.setTitle("这是第一个对话框").setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.out.println(items[i]);
            }
        }).setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNeutralButton("中性按钮", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
            });
        return builder.create();
    }
}

4.对话框自定义一个布局---以登录注册为例

先绘制一下登录页面:dialog_login.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="用户名">
        </TextView>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="9">
        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密    码">
        </TextView>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="9">
        </EditText>

    </LinearLayout>

</LinearLayout>

再StartGameDialogFragment.java文件里先写一个view对象:

image.png

然后将对话框里面的内容部分替换成我们写的一个登录页面:

image.png

public class StartGameDialogFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());

        View view=View.inflate(getContext(),R.layout.dialog_login,null);
        String [] items={"red","blue","green"};
        builder.setTitle("这是第一个对话框").setView(view).
        setPositiveButton("ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setNeutralButton("中性按钮", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
            });
        return builder.create();
    }
}

image.png

后续实现的略有复杂,需要源码的欢迎随时私信哦。

最后的实现效果如下:

image.png

本篇博客到此结束啦,欢迎各位大佬随时来访!