Android各种对话框

在这里插入图片描述

DialogActivity.java

package cn.edu.zufe.app001;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;

public class DialogActivity extends AppCompatActivity {

    final int COMMON_DAILOG = 1;
    final int LIST_DIALOG = 2;
    final int LIST_DIALOG_SINGLE = 3;
    final int LIST_DIALOG_MULTIPLE = 4;
    boolean[] mulFlags = new boolean[]{true,false,true};
    String[] items = null;

    final int PROGRESS_DIALOG = 5;
    final int INCREASE = 5;
    ProgressDialog pd;
    Handler myHandler;

    final int DATE_DIALOG = 6;
    final int TIME_DIALOG = 7;
    Calendar c = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        items = getResources().getStringArray(R.array.msa);
        this.myHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case INCREASE:
                        pd.incrementProgressBy(1);
                        if(pd.getProgress() >= 100){
                            pd.dismiss();
                        }
                        break;
                }
                super.handleMessage(msg);
            }
        };
        Button btn01 = (Button)findViewById(R.id.button1);
        btn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(COMMON_DAILOG);
            }
        });
        Button btn02 = (Button)findViewById(R.id.button2);
        btn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(LIST_DIALOG);
            }
        });
        Button btn03 = (Button)findViewById(R.id.button3);
        btn03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(LIST_DIALOG_SINGLE);
            }
        });
        Button btn04 = (Button)findViewById(R.id.button4);
        btn04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(LIST_DIALOG_MULTIPLE);
            }
        });
        Button btn05 = (Button)findViewById(R.id.button5);
        btn05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG);
            }
        });
        Button btn06 = (Button)findViewById(R.id.button6);
        btn06 .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(PROGRESS_DIALOG);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        AlertDialog.Builder b = new AlertDialog.Builder(this);
        switch(id){
            case COMMON_DAILOG:
                b.setIcon(R.drawable.ic_header);
                b.setTitle(R.string.title1);
                b.setMessage(R.string.dialg_msg1);
                b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EditText et01 = (EditText)findViewById(R.id.et_name);
                        et01.setText(R.string.dialg_msg1);
                    }
                });
                dialog = b.create();
                break;
            case LIST_DIALOG:
                b.setIcon(R.drawable.ic_header);
                b.setTitle(R.string.title2);
                b.setItems(R.array.msa, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EditText et01 = (EditText)findViewById(R.id.et_name);
                        et01.setText("您选择了" + getResources().getStringArray(R.array.msa)[which]);
                    }
                });
                dialog = b.create();
                break;
            case LIST_DIALOG_SINGLE:
                b.setIcon(R.drawable.ic_header);
                b.setTitle(R.string.title3);
                b.setSingleChoiceItems(R.array.msa, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        EditText et01 = (EditText)findViewById(R.id.et_name);
                        et01.setText("您选择了" + getResources().getStringArray(R.array.msa)[which]);
                    }
                });
                b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = b.create();
                break;
            case LIST_DIALOG_MULTIPLE:
                b.setIcon(R.drawable.ic_header);
                b.setTitle(R.string.title4);
                b.setMultiChoiceItems(R.array.msa, mulFlags, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        mulFlags[which] = isChecked;
                        String result = "您选择了:";
                        for(int i=0 ;i<mulFlags.length; i++){
                            if(mulFlags[i]){
                                result += items[i] + ",";
                            }
                        }
                        EditText et01 = (EditText)findViewById(R.id.et_name);
                        et01.setText(result.substring(0,result.length()-1));
                    }
                });
                b.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog = b.create();
                break;
            case DATE_DIALOG:
                c = Calendar.getInstance();
                dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        EditText et01 = (EditText)findViewById(R.id.et_name);
                        et01.setText("您选择了" + year + "年" + (month +1) + "月" + dayOfMonth + "日");
                    }
                },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
                break;
            case PROGRESS_DIALOG:
                pd = new ProgressDialog(this);
                pd.setMax(100);
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd.setTitle(R.string.titleok);
                pd.setMessage("请稍等...");
                pd.setCancelable(false);
                dialog = pd;
                break;
        }
        return dialog;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        if(id == PROGRESS_DIALOG){
            pd.incrementProgressBy(-pd.getProgress());
            new Thread(){
                public void run(){
                    while(true){
                        myHandler.sendEmptyMessage(INCREASE);
                        if(pd.getProgress() >= 100){
                            break;
                        }
                        try{
                            Thread.sleep(40);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
        super.onPrepareDialog(id, dialog);
    }
}

activity_dialog.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="cn.edu.zufe.app001.DialogActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/et_name">
    </EditText>
    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="显示普通对话框"
        android:layout_gravity="center"
        android:textSize="20sp">
    </Button>
    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:text="显示列表对话框"
        android:layout_gravity="center"
        android:textSize="20sp">
    </Button>
    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:text="显示单选对话框"
        android:layout_gravity="center"
        android:textSize="20sp">
    </Button>
    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:text="显示多选对话框"
        android:layout_gravity="center"
        android:textSize="20sp">
    </Button>
    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:text="显示日期选择对话框"
        android:layout_gravity="center"
        android:textSize="20sp">
    </Button>
    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/button6"
        android:text="显示进度条对话框"
        android:layout_gravity="center"
        android:textSize="20sp">
    </Button>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Android 自定义对话框,你可以通过以下步骤来实现: 1. 创建一个新的类继承自 DialogFragment,这将作为你的自定义对话框的实现类。 ```java public class CustomDialogFragment extends DialogFragment { // 在这里实现你的自定义对话框逻辑 } ``` 2. 在你的布局文件中定义自定义对话框的外观。可以使用 LinearLayout、RelativeLayout 或者其他布局容器来构建对话框的外观。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 在这里添加对话框的内容,比如文本、按钮等 --> </LinearLayout> ``` 3. 在自定义对话框的实现类中,重写 `onCreateDialog` 方法,将布局文件与对话框关联起来。 ```java @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); View dialogView = inflater.inflate(R.layout.custom_dialog_layout, null); builder.setView(dialogView); // 在这里设置对话框的标题、按钮等内容 return builder.create(); } ``` 4. 在需要显示对话框的地方,创建并显示你的自定义对话框实例。 ```java CustomDialogFragment dialog = new CustomDialogFragment(); dialog.show(getSupportFragmentManager(), "custom_dialog"); ``` 这就是一个基本的 Android 自定义对话框的实现过程。你可以进一步扩展自定义对话框的功能,根据你的需求添加文本、按钮、点击事件等等。希望这能帮到你!如果有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值