Android中的对话框Dialog继承自Object,Dialog的生命周期由Activity来控制,当Dialog显示内容时,Activity会失去焦点,用户将在Dialog上进行操作;当Dialog消失后,Activity重新获得焦点。
对话框样式:
1. 按钮对话框:
点击确认进行页面跳转
点击说明进行消息提示
说明:setIcon
:设置对话框的图标setTitle
:设置对话框标题setMessage
:设置对话框消息setView
: 对话框自定义样式设置setItems
:设置对话框中的列表setPositiveButton
:确定按钮的设置setNegativeButton
:取消按钮的设置setNeutralButton
:普通按钮的设置setCancelable
:false
代表点击对话框外区域时,dialog不取消,反之为true(默认)create
:创建对话框show
:显示对话框dismiss
方法会释放对话框所占的资源(建议在activity中的onDestory中调用)
代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bt1=findViewById(R.id.bt_1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
ShowDialog();
}
}
});
private void ShowDialog(){
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("标题");
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setMessage("消息");
dialog.setNeutralButton("说明",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast=Toast.makeText(MainActivity.this, "说明完毕", Toast.LENGTH_SHORT);
toast.show();
}
});
dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
});
dialog.setNegativeButton("取消",null);
dialog.setCancelable(false);//点击外部区域不取消dialog
dialog.create().show();
}
}
2. 列表对话框
(onClick外部没有变化就不复制进来了,见谅)
代码:
private void ShowDialog(){
final String[] items = {"A", "B", "C", "D"};
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("标题");
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast=Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT);
toast.show();
}
});
dialog.setNeutralButton("说明",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast=Toast.makeText(MainActivity.this, "说明完毕", Toast.LENGTH_SHORT);
toast.show();
}
});
dialog.setNegativeButton("取消",null);
dialog.setCancelable(false);
dialog.create().show();
}
3.多选对话框
确认后根据选择结果进行消息提示:
代码:
private void ShowDialog(){
final String[] items = {"A", "B", "C", "D"};
final boolean[] aa=new boolean[]{true,true,false,false};
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("标题");
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setNeutralButton("说明",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast=Toast.makeText(MainActivity.this, "说明完毕", Toast.LENGTH_SHORT);
toast.show();
}
});
dialog.setMultiChoiceItems(items,aa, new DialogInterface.OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which,boolean isChecked) {
aa[which]=isChecked;
}
});
dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder=new StringBuilder();
for(int i=0;i<aa.length;i++)
{
if (aa[i]==true) {
stringBuilder.append(items[i] + ",");
}
}
Toast.makeText(MainActivity.this,"确认选择:"+stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
});
dialog.setNegativeButton("取消",null);
dialog.setCancelable(false);
dialog.create().show();
}
4. 单选对话框
点击确认后提示消息
代码:
private int select=0;
private void ShowDialog_radiobutton(){
final String[] items={"苹果","西瓜","葡萄","哈密瓜","桃子"};
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setIcon(R.mipmap.ic_launcher)
.setTitle("最爱的水果")
.setSingleChoiceItems(items,select, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
select=which;
Toast.makeText(MainActivity.this,"选择"+items[select],Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (select != -1) {
Toast.makeText(MainActivity.this,"您最爱"+items[select],Toast.LENGTH_LONG).show();
}
}
});
dialog.create().show();
}
5. 编辑对话框
输入后提示:
代码:
private void ShowEditDialog(){
final EditText editText=new EditText(MainActivity.this);
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("请输入内容:");
dialog.setView(editText);
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"输入:"+editText.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
dialog.create().show();
}
6. 自定义对话框
确认后提示
页面布局:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="@string/tv_username"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/tv_username"
app:layout_constraintBaseline_toBaselineOf="@id/tv_username"
android:hint="请输入账号"/>
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/tv_password"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="@id/tv_username"
app:layout_constraintTop_toBottomOf="@id/tv_username" />
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@id/et_username"
app:layout_constraintBaseline_toBaselineOf="@id/tv_password"
android:hint="请输入密码"
android:inputType="textPassword"/>
</android.support.constraint.ConstraintLayout>
代码:
private void ShowCustomizeDialog(){
final View customize_dialog= LayoutInflater.from(MainActivity.this)
.inflate(R.layout.activity_customize_dialog,null);
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("登陆")
.setView(customize_dialog)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText et_username=customize_dialog.findViewById(R.id.et_username);
Toast.makeText(MainActivity.this,"欢迎用户"+et_username.getText().toString()+"登陆",Toast.LENGTH_SHORT)
.show();
}
})
.create()
.show();
}
7. 日期选择对话框
确认后提示
代码:
private void DateDialog(){
Calendar calendar=Calendar.getInstance();
DatePickerDialog dialog=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+"日".toString(),Toast.LENGTH_SHORT)
.show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();
}
8. 时间选择对话框
确认后提示
代码:
private void TimeDialog(){
Calendar calendar=Calendar.getInstance();
TimePickerDialog dialog=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_OF_DAY),calendar.get(Calendar.MINUTE),true);
dialog.show();
}