android Dialog对话框

对话框

常用对话框

普通对话框

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//创建对话框构建者
                builder.setTitle("标题");//设置标题
                builder.setMessage("内容");//设置内容
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定的方法
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();//吐司
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消的方法
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();//吐司
                    }
                });
                AlertDialog alertDialog = builder.create();//使用建造者创建对话框
                alertDialog.show();//展示

单选对话框

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//创建对话框构建者
                builder.setTitle("单选");//设置标题
                final String[] s = new String[]{"足球", "篮球", "乒乓球"};//String类型数组
                builder.setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {//单选的方法
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();//吐司当前选择的内容
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog alertDialog = builder.create();//使用建造者创建对话框
                alertDialog.show();//展示

多选对话框

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//创建对话框构建者
                builder.setTitle("多选");//设置标题
                final String[] s = new String[]{"足球", "篮球", "乒乓球"};//String类型数组
                final boolean[] b = new boolean[]{false, false, false};//boolean类型数组
                builder.setMultiChoiceItems(s, b, new DialogInterface.OnMultiChoiceClickListener() {//多选方法
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (isChecked) {
                            Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int i = 0; i < b.length; i++) {
                            if (b[i]) {
                                Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                AlertDialog alertDialog = builder.create();//使用建造者创建对话框
                alertDialog.show();//展示

自定义对话框

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.asd, null);//布局文件
                builder.setView(inflate);//设置自定义布局
                ImageView imageView = inflate.findViewById(R.id.imageview);
                imageView.setOnClickListener(new View.OnClickListener() {//图片单击事件
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();//隐藏构建者
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog = builder.create();//使用建造者创建对话框
                alertDialog.show();//展示

日期对话框

Calendar calendar = Calendar.getInstance();//时间
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {//日期对话框
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        Toast.makeText(MainActivity.this, year + "年" + (month + 1) + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show();//吐司
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));//当前系统年月日的参数
                datePickerDialog.show();//展示

时间对话框

Calendar calendar = Calendar.getInstance();//时间
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {//时间对话框
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Toast.makeText(MainActivity.this, hourOfDay + "时" + minute + "分", Toast.LENGTH_SHORT).show();//吐司
                    }
                }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), true);//当前系统小时分钟的参数
                timePickerDialog.show();

水平对话框

 final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);//水平对话框
                progressDialog.setProgress(10);//设置进度值
                progressDialog.setSecondaryProgress(20);//设置第二进度值
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为水平类型
                Timer timer = new Timer();//模拟定时器
                timer.schedule(new TimerTask() {
                    int index = 0;
                    @Override
                    public void run() {
                        progressDialog.setProgress(index);
                        if (index >= 100) {
                            progressDialog.dismiss();//消失
                        }
                        index += 10;
                    }
                }, 0, 1000);
                progressDialog.show();//展示

自定义对话框2

 MyDialog myDialog=new MyDialog(MainActivity.this);
                myDialog.setStrTit("标题");
                myDialog.setStrMsg("内容");
                myDialog.setYesonClick(new MyDialog.yesonClick() {
                    @Override
                    public void onclick() {
                        Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog.setNoonClick(new MyDialog.noonClick() {
                    @Override
                    public void onclick() {
                        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                myDialog.show();
public class MyDialog extends Dialog {
    private TextView title;
    private TextView message;
    private Button no;
    private Button yes;
    private String strMsg;
    private String strTit;
    public interface yesonClick{
        void onclick();
    }
    private yesonClick yesonClick;

    public void setYesonClick(MyDialog.yesonClick yesonClick) {
        this.yesonClick = yesonClick;
    }
    public interface noonClick{
        void onclick();
    }
    private noonClick noonClick;

    public void setNoonClick(MyDialog.noonClick noonClick) {
        this.noonClick = noonClick;
    }

    public void setStrMsg(String strMsg) {
        this.strMsg = strMsg;
    }

    public void setStrTit(String strTit) {
        this.strTit = strTit;
    }

    public MyDialog(@NonNull Context context) {
        super(context);
        setContentView(R.layout.dsa);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dsa);
        initView();
        title.setText(strTit);
        message.setText(strMsg);
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                yesonClick.onclick();
            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                noonClick.onclick();
            }
        });
    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        message = (TextView) findViewById(R.id.message);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);
    }
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#11ffffff">
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="警告"

            android:textColor="#38ADFF"
            android:textSize="16sp"/>
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:layout_gravity="center"
            android:text="保护视力,少玩手机"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginTop="15dp"
            android:background="#E4E4E4"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/no"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:background="@null"
                android:gravity="center"
                android:lines="1"
                android:text="取消"
                android:textColor="#7D7D7D"
                android:textSize="16sp"/>
            <View
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="#E4E4E4"/>
            <Button
                android:id="@+id/yes"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginRight="10dp"
                android:background="@null"
                android:gravity="center"
                android:lines="1"
                android:text="确定"
                android:textColor="#38ADFF"
                android:textSize="16sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值