首先讲解AlerDialog的构造过程:
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
//首先构造AlerDialog的构造器
builder.setTitle("标题");//设置标题
builder.setIcon(R.mipmap.ic_launcher_round);//设置图标大小;
builder.setMessage("这是输入框的内容");
//在对话框中添加一个确认按钮;
builder.setPositiveButton ("确认",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT ).show ();
}
} );
AlertDialog dialog=builder.create ();//实例化AlerDialog类
dialog.show ();//显示该对话框
接着讲解AlerDialog常用按钮形式:1.确认按钮:builder.setPositiveButton(...) 2.取消按钮builder.setNegativeButton 3.中立按钮builder.setNeutralButton(...)
接下来讲解实现全过程:
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"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/sure"
android:text="确认对话框"
android:layout_gravity="center"/>
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/single"
android:text="单选对话框"
android:layout_gravity="center"/>
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/more_opt"
android:text="多选对话框"
android:layout_gravity="center"/>
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/list"
android:text="列表对话框"
android:layout_gravity="center"/>
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/define"
android:text="自定义对话框"
android:layout_gravity="center"/>
接下来主页面代码:
package com.mingrisoft.alertdialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.Toolbar;
import java.sql.BatchUpdateException;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivityextends AppCompatActivityimplements View.OnClickListener {
private ButtonmSure;
private ButtonmSingle;
private ButtonmMoreOpt;
private ButtonmList;
private ButtonmDefine;
//以下是下面多选框或者列表框所要存储的数据,采取java中Container(容器类)来存储;
private String[]sexList={"男","女"};//单选列表
private String[]likeList={"篮球","足球","排球","听音乐","看电影"};//多选列表
private String[]itemList={"项目经理","策划","Android工程师","电工","美工"};//列表
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
initView ();
}
private void initView() {
mSure = (Button) findViewById ( R.id.sure );
mSingle = (Button) findViewById ( R.id.single );
mMoreOpt = (Button) findViewById ( R.id.more_opt );
mList = (Button) findViewById ( R.id.list );
mDefine = (Button) findViewById ( R.id.define );
mSure.setOnClickListener (this );
mSingle.setOnClickListener (this );
mMoreOpt.setOnClickListener (this );
mList.setOnClickListener (this );
mDefine.setOnClickListener (this );
}
@Override
public void onClick(View v) {
switch (v.getId ()) {
case R.id.sure:
showDialog1();//确认对话框
break;
case R.id.single:
showDialog2();//单选对话框
break;
case R.id.more_opt:
showDialog3();//多选对话框
break;
case R.id.list:
showDialog4();//列表对话框
break;
case R.id.define:
showDialog5();//自定义对话框
break;
}
}
private void showDialog1() {
AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("购物车" );
builder.setIcon ( R.mipmap.ic_launcher );
builder.setMessage ("是否购买该物品" );
builder.setPositiveButton ("确认",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT ).show ();
}
} );
builder.setNegativeButton ("取消",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
}
} );
builder.setNeutralButton ("让我再想想",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"待会再买",Toast.LENGTH_LONG ).show ();
}
} );
AlertDialog dialog=builder.create ();
dialog.show ();
}
private void showDialog2() {
AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("性别:" );
builder.setIcon ( R.mipmap.android );
builder.setSingleChoiceItems (sexList, -1,new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
String sex=sexList[which];
Toast.makeText ( MainActivity.this,"这个性别为"+sex,Toast.LENGTH_SHORT ).show ();
}
} );
//设置中立事件以及点击事件
builder.setNeutralButton ("性别暂时不清楚",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"暂时不知道性别",Toast.LENGTH_LONG ).show ();
}
} );
builder.setPositiveButton ("确定",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"这是您的选择",Toast.LENGTH_SHORT ).show ();
}
} );
builder.setNegativeButton ("取消",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss ();//关闭对话框
}
} );
AlertDialog dialog=builder.create ();
dialog.show ();
}
private void showDialog3() {
AlertDialog.Builder builder =new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("个人爱好" );
builder.setIcon ( R.mipmap.android );
builder.setMultiChoiceItems (likeList,null,new DialogInterface.OnMultiChoiceClickListener () {
@Override
public void onClick(DialogInterface dialog,int which,boolean isChecked) {
if (isChecked) {
Toast.makeText ( MainActivity.this,"我喜欢" +likeList[which], Toast.LENGTH_SHORT ).show ();
}else {
Toast.makeText ( MainActivity.this,"我不喜欢" +likeList[which], Toast.LENGTH_SHORT ).show ();
}
}
} );
builder.setPositiveButton ("确定",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"这是您的选择", Toast.LENGTH_SHORT ).show ();
}
});
//设置“中立”按钮,及点击事件
builder.setNeutralButton ("等等",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"让我想一想",Toast.LENGTH_LONG ).show ();
}
} );
builder.setNegativeButton ("取消",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss ();//关闭对话框
}
} );
AlertDialog dialog=builder.create ();
dialog.show ();//显示对话框
}
private void showDialog4() {
AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("职业岗位" );
builder.setIcon ( R.mipmap.android );
builder.setItems (itemList,new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"你的职业"+itemList[which],Toast.LENGTH_SHORT ).show ();
}
} );
AlertDialog dialog=builder.create ();
dialog.show ();//显示对话框
}
private void showDialog5() {
//自定义对话框
LayoutInflater inflater=LayoutInflater.from (this );
View view=inflater.inflate ( R.layout.self_define,null );
AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("自定义布局" );
builder.setView ( R.layout.self_define );
builder.setPositiveButton ("确定",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"确定选择",Toast.LENGTH_LONG ).show ();
}
} );
builder.setNegativeButton ("取消",new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"取消选择",Toast.LENGTH_LONG ).show ();
}
} );
AlertDialog dialog=builder.create ();
dialog.show ();//显示对话框
}
}
https://www.jb51.net/article/83192.htm