对话框类型:
提示对话框
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:onClick="dialogClick1"
android:text="提示对话框一" />
</RelativeLayout>
package com.example.application03;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
public class MainActivity_alertd extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog);
}
public void dialogClick1(View view){
//创建一个提示对话框的构造者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("请问你有女朋友吗?");
builder.setIcon(R.mipmap.ic_launcher);//设置小图标
//设置正面的按钮(确定)
builder.setPositiveButton("有", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_alertd.this,"你可以走了",Toast.LENGTH_LONG).show();
}
});
//中立按钮
builder.setNeutralButton("隐藏", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_alertd.this,"有没有并不重要",Toast.LENGTH_LONG).show();
}
});
//按钮(否)
builder.setNegativeButton("没有", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_alertd.this,"欢迎光临",Toast.LENGTH_LONG).show();
}
});
//AlertDialog dialog = builder.create();
//dialog.show();
builder.show();
}
}
列表选项对话框
public void dialogClick2(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] items = {"Android","IOS","Window Phone"};
builder.setTitle("请选择一个平台");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_alertd.this,items[which],Toast.LENGTH_LONG).show();
}
});
builder.show();
}
多选列表对话框
public void dialogClick3(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择一个平台");
final ArrayList<String> list = new ArrayList<String>();
final String[] items = {"Android","IOS","Window Phone"};
builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
list.add(items[which]);
}else{
list.remove(items[which]);
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_alertd.this,list.toString(),Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
list.clear();
dialog.dismiss();
}
});
builder.show();
}
多项单选对话框
String result = "Android";
public void dialogClick4(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择一个平台");
final ArrayList<String> list = new ArrayList<String>();
final String[] items = {"Android","IOS","Window Phone"};
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_alertd.this,"你选中了"+items[which],Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity_alertd.this,result,Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
自定义对话框
public void dialogClick5(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("登录对话框");
//调用其他的页面api21(android5.0)支持-->builder.setView(R.layout.login_layout);
//实例化布局文件
final View view = getLayoutInflater().inflate(R.layout.login_layout,null);
builder.setView(view);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText editText_username = (EditText) view.findViewById(R.id.editText_username);
EditText editText_password = (EditText) view.findViewById(R.id.editText_password);
String username = editText_username.getText().toString();
String password = editText_password.getText().toString();
Toast.makeText(MainActivity_alertd.this,username+"--"+password,Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
//alertdialog.xml
<Button
android:id="@+id/button5"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="116dp"
android:layout_marginTop="339dp"
android:text="自定义对话框"
android:onClick="dialogClick5" />
//login_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editText_username"
android:inputType="textPersonName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editText_password"
android:inputType="numberPassword"
android:hint="password"/>
</LinearLayout>
自定义对话框需要建立一个layout页面来作为对话框内容