四种对话框(dialog)的简单使用方法

时间:2021-06-02 13:40:34

有普通对话框,单选对话框,复选对话框,进度条的两种实现方法话不多说,直接上代码

activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/mainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通对话框"
android:onClick="normalDialog"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选对话框"
android:onClick="singleDialog"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复选对话框"
android:onClick="MultiDialog"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条对话框"
android:onClick="progressDialog"/>
<Button
android:id="@+id/downlodebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条对话框"
android:onClick="progressDialog"/>
</LinearLayout> </android.support.constraint.ConstraintLayout>

MainActivity.java:

 package com.lgqrlchinese.dialogtext;

 import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
protected String result; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progerssNow();
} //普通对话框
public void normalDialog(View view) {
//构建器
AlertDialog.Builder b = new AlertDialog.Builder(this);
//设置标题
b.setTitle("普通对话框");
//设置提示信息
b.setMessage("是否确定退出");
//设置图标
b.setIcon(R.drawable.ic_launcher_background);
//添加确定按钮
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
//添加取消按钮
b.setNegativeButton("取消", null);
//创建对话框
b.create();
//显示
b.show();
} //单选对话框
public void singleDialog(View view) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("单选对话框");
//选项内容
final String item[] = {"A", "B", "C", "D"};
//setSingleChoiceItems(显示的选项内容,默认值,监听事件)
b.setSingleChoiceItems(item, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//取出数组中的数据
result = item[i];
//吐司显示出来
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
});
//设置确定按钮
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "选择的结果是" + result, Toast.LENGTH_SHORT).show();
}
}); b.create();
b.show();
} //复选对话框
public void MultiDialog(View view) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("复选框对话框");
b.setIcon(R.drawable.ic_launcher_background);
//设置选项
final String item[] = {"A", "B", "C", "D"};
//设置默认值
final boolean chechedItem[] = {true, false, true, true};
//设置多选setMultiChoiceItems(选项数组,默认值,监听事件)
b.setMultiChoiceItems(item, chechedItem, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) { }
});
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
StringBuffer stringBuffer = new StringBuffer();
for (int i1 = 0; i1 < chechedItem.length; i1++) {
if (chechedItem[i1]) {
stringBuffer.append(item[i1] + " ");
}
}
Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_SHORT).show();
}
});
b.create();
b.show();
} //进度条对话框
/*
* 在API level 26 中,ProgressDialog被声明不赞成使用,应使用的替代方法是ProgressBar
* */
public void progressDialog(View view) {
final ProgressDialog progressDialog = new ProgressDialog(this);
//设置标题
progressDialog.setTitle("正在加载中》》》");
//设置样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//(模拟下载)线程睡眠
new Thread() {
@Override
public void run() {
//设置最大值
progressDialog.setMax(100);
//设置当前值
for (int i = 0; i < 100; i++) {
progressDialog.setProgress(i);
SystemClock.sleep(500);
}
progressDialog.dismiss();
}
}.start();
progressDialog.show();
} //进度条对话框(推荐使用)
public void progerssNow() {
LinearLayout mainLayout;
Button downlodebutton;
mainLayout = findViewById(R.id.mainLinearLayout);
downlodebutton = findViewById(R.id.downlodebutton);
final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
downlodebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread() {
@Override
public void run() { progressBar.setMax(100);
for (int i = 0; i < 100; i++) {
progressBar.setProgress(i);
SystemClock.sleep(12);
} }
}.start();
}
});
mainLayout.addView(progressBar);
}
}