android对话框合集AlertDialog
demo
mydialogs:
1.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">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="click01"
android:text="确定取消对话框"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="click02"
android:text="单选对话框"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="click03"
android:text="多选对话框"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="click04"
android:text="进度对话框"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:onClick="click05"
android:text="有具体进度的对话框"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
</android.support.constraint.ConstraintLayout>
2.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.glsite.mydialogs">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3.MainActivity.java
package com.glsite.mydialogs;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 确定取消对话框
* @param view
*/
public void click01(View view) {
// 工厂设计模式,得到创建对话框的工厂
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("警告")
.setMessage("若练此功,必先自宫,是否继续?")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"啊......即使自宫,也不一定能成功", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"如果不自宫,一定不成功", Toast.LENGTH_SHORT).show();
}
})
.show();
}
/**
* 单选对话框
* @param view
*/
public void click02(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择您的性别:");
final String[] items = {"男", "女", "人妖"};
builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"您的性别为:" + items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
// 千万注意,最后要调用show()方法
builder.show();
}
/**
* 多选对话框
* @param view
*/
public void click03(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择您爱吃的水果:");
final String[] items = {"苹果", "梨子", "香蕉", "菠萝", "哈密瓜"};
final boolean[] checkeds = {true, false, true, false, true};
builder.setMultiChoiceItems(items, checkeds, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < checkeds.length; i++) {
if (checkeds[i]) {
sb.append(items[i] + " ");
}
}
Toast.makeText(MainActivity.this,"您喜欢吃的水果是" + sb.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
/**
* 进度对话框
* @param view
*/
public void click04(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("请稍后:");
pd.setMessage("正在拼命加载中...");
pd.show();
new Thread(){
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pd.dismiss();
}
}.start();
}
/**
* 有具体进度的对话框
* @param view
*/
public void click05(View view) {
final ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("请稍后:");
pd.setMessage("正在拼命加载中...");
// 设置水平进度风格
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置进度条的最大值
pd.setMax(100);
pd.show();
new Thread(){
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(300);
pd.setProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pd.dismiss();
}
}.start();
}
}