实例如图:
acticity.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.yongninggo.helloworld.MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="对话框"
android:layout_marginTop="10dip"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框"
android:layout_marginTop="10dip"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框"
android:layout_marginTop="10dip"/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框"
android:layout_marginTop="10dip"/>
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义对话框"
android:layout_marginTop="10dip"/>
</LinearLayout>
activity.java文件
package com.yongninggo.helloworld;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Activity extends AppCompatActivity {
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
final String[] ma = {"男","女"};
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setIcon(R.drawable.image1);
builder.setTitle("提示");
builder.setMessage("提示框内容");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您点击了确定",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setIcon(R.drawable.image1);
builder.setTitle("提示");
builder.setSingleChoiceItems(ma, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您选择了:"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您点击了"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setIcon(R.drawable.image1);
builder.setTitle("提示");
builder.setMultiChoiceItems(ma, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(Activity.this,"您点击了:"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您点击了"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setIcon(R.drawable.image1);
builder.setTitle("提示");
builder.setItems(ma, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您点击了:"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您点击了"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater.from(Activity.this);
View view = inflater.inflate(R.layout.activity1,null);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setView(view);
builder.show();
}
});
}
}
activity1.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="com.yongninggo.helloworld.MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<LinearLayout
android:id="@+id/lin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dip"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义Toast"
android:gravity="center_horizontal"/>
<ImageView
android:layout_width="300dip"
android:layout_height="182dp"
android:src="@drawable/image2"/>
</LinearLayout>
</LinearLayout>
onBackPressed退出操作提示框 (退出程序写在主入口的结尾)
@Override public void onBackPressed() { //super.onBackPressed(); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.image1); builder.setTitle("提示"); builder.setMessage("提示框内容"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); builder.setNegativeButton("取消",null); builder.show(); }