对话框(AlertDialog)使用
2019-12-26 本文已影响0人
Lee_5566
image.png
image.png
image.png
image.png
单选对话框:
image.png
image.png
自定义对话框:
image.png
使用adapter的对话框:
image.png
目录
AlertDialog
AlertDialog也就处对话框。
使用方式分为6种:
- 简单dialog
- 列表dialog
- 单选dialog
- 多选dialog
- 自定义dialog
- 使用adapter的dialog
举例第一种的使用
代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("桃子");
builder.setMessage("喜欢吃桃子吗?");
builder.setPositiveButton("yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "想吃", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("不喜欢吃",null);
builder.setNeutralButton("不知道",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
实战
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="简单dialog"
android:onClick="dialog_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表dialog"
android:onClick="dialog_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选的dialog"
android:onClick="dialog_3"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选dialog"
android:onClick="dialog_4"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义dialog"
android:onClick="dialog_5"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="使用adapter的dialog"
android:onClick="dialog_6"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
代码:
package com.example.user.alertdiog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int index;
private String [] fru = {"苹果","桃子","西红柿","芒果","数学老师"};
boolean[] flag = {false,false,false,false,false};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dialog_1(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("桃子");
builder.setMessage("喜欢吃桃子吗?");
builder.setPositiveButton("yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "想吃", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("不喜欢吃",null);
builder.setNeutralButton("不知道",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void dialog_2(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setItems(fru, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择了"+fru[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void dialog_3(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setSingleChoiceItems(fru, index, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
index = which;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择了"+fru[index], Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void dialog_4(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择");
builder.setMultiChoiceItems(fru, flag, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flag[which] = isChecked;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer str = new StringBuffer();
for (int i = 0; i < fru.length; i++) {
if (flag[i]) {
str.append(fru[i] + " ");
}
}
Toast.makeText(MainActivity.this, "选择了" + str.toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void dialog_5(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("爱吃的水果");
final EditText et = new EditText(this);
et.setHint("哦我爱吃的水果是什么");
et.setSingleLine(true);
builder.setView(et);
builder.setNegativeButton("取消",null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String password = et.getText().toString();
if (password.equals("桃子")) {
Toast.makeText(MainActivity.this, "正确", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
public void dialog_6(View v){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fru);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("水果超市");
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择了"+fru[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
运行效果:
image.png
简单的对话框:
image.png
列表对话框:
image.png
单选对话框:
image.png
多选对话框:
image.png
自定义对话框:
image.png
使用adapter的对话框:
image.png