首先講解AlerDialog的構(gòu)造過(guò)程:
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
//首先構(gòu)造AlerDialog的構(gòu)造器
builder.setTitle("標(biāo)題");//設(shè)置標(biāo)題
builder.setIcon(R.mipmap.ic_launcher_round);//設(shè)置圖標(biāo)大小;
builder.setMessage("這是輸入框的內(nèi)容");
//在對(duì)話框中添加一個(gè)確認(rèn)按鈕;
builder.setPositiveButton ("確認(rèn)",new DialogInterface.OnClickListener () {
@Override
? ? public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"點(diǎn)擊了確定按鈕",Toast.LENGTH_SHORT ).show ();
}
} );
AlertDialog dialog=builder.create ();//實(shí)例化AlerDialog類
dialog.show ();//顯示該對(duì)話框
接著講解AlerDialog常用按鈕形式:1.確認(rèn)按鈕:builder.setPositiveButton(...)? ?2.取消按鈕builder.setNegativeButton 3.中立按鈕builder.setNeutralButton(...)
接下來(lái)講解實(shí)現(xiàn)全過(guò)程:
<?xml version="1.0" encoding="utf-8"?>
? ? 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="確認(rèn)對(duì)話框"
? ? ? ? android:layout_gravity="center"/>
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="50dp"
? ? ? ? android:id="@+id/single"
? ? ? ? android:text="單選對(duì)話框"
? ? ? ? android:layout_gravity="center"/>
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="50dp"
? ? ? ? android:id="@+id/more_opt"
? ? ? ? android:text="多選對(duì)話框"
? ? ? ? android:layout_gravity="center"/>
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="50dp"
? ? ? ? android:id="@+id/list"
? ? ? ? android:text="列表對(duì)話框"
? ? ? ? android:layout_gravity="center"/>
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="50dp"
? ? ? ? android:id="@+id/define"
? ? ? ? android:text="自定義對(duì)話框"
? ? ? ? android:layout_gravity="center"/>
接下來(lái)主頁(yè)面代碼:
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;
//以下是下面多選框或者列表框所要存儲(chǔ)的數(shù)據(jù),采取java中Container(容器類)來(lái)存儲(chǔ);
? ? private String[]sexList={"男","女"};//單選列表
? ? private String[]likeList={"籃球","足球","排球","聽(tīng)音樂(lè)","看電影"};//多選列表
? ? private String[]itemList={"項(xiàng)目經(jīng)理","策劃","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();//確認(rèn)對(duì)話框
? ? ? ? ? ? ? ? break;
case R.id.single:
showDialog2();//單選對(duì)話框
? ? ? ? ? ? ? ? break;
case R.id.more_opt:
showDialog3();//多選對(duì)話框
? ? ? ? ? ? ? ? break;
case R.id.list:
showDialog4();//列表對(duì)話框
? ? ? ? ? ? ? ? break;
case R.id.define:
showDialog5();//自定義對(duì)話框
? ? ? ? ? ? ? ? break;
}
}
private void showDialog1() {
AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("購(gòu)物車" );
builder.setIcon ( R.mipmap.ic_launcher );
builder.setMessage ("是否購(gòu)買該物品" );
builder.setPositiveButton ("確認(rèn)",new DialogInterface.OnClickListener () {
@Override
? ? ? ? ? ? public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"點(diǎn)擊了確定按鈕",Toast.LENGTH_SHORT ).show ();
}
} );
builder.setNegativeButton ("取消",new DialogInterface.OnClickListener () {
@Override
? ? ? ? ? ? public void onClick(DialogInterface dialog,int which) {
Toast.makeText(MainActivity.this,"點(diǎn)擊了取消按鈕",Toast.LENGTH_SHORT).show();
}
} );
builder.setNeutralButton ("讓我再想想",new DialogInterface.OnClickListener () {
@Override
? ? ? ? ? ? public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"待會(huì)再買",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,"這個(gè)性別為"+sex,Toast.LENGTH_SHORT ).show ();
}
} );
//設(shè)置中立事件以及點(diǎn)擊事件
? ? ? ? builder.setNeutralButton ("性別暫時(shí)不清楚",new DialogInterface.OnClickListener () {
@Override
? ? ? ? ? ? public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"暫時(shí)不知道性別",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 ();//關(guān)閉對(duì)話框
? ? ? ? ? ? }
} );
AlertDialog dialog=builder.create ();
dialog.show ();
}
private void showDialog3() {
AlertDialog.Builder builder =new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("個(gè)人愛(ài)好" );
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 ();
}
});
//設(shè)置“中立”按鈕夏伊,及點(diǎn)擊事件
? ? ? ? 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 ();//關(guān)閉對(duì)話框
? ? ? ? ? }
} );
AlertDialog dialog=builder.create ();
dialog.show ();//顯示對(duì)話框
? ? }
private void showDialog4() {
AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );
builder.setTitle ("職業(yè)崗位" );
builder.setIcon ( R.mipmap.android );
builder.setItems (itemList,new DialogInterface.OnClickListener () {
@Override
? ? ? ? ? ? public void onClick(DialogInterface dialog,int which) {
Toast.makeText ( MainActivity.this,"你的職業(yè)"+itemList[which],Toast.LENGTH_SHORT ).show ();
}
} );
AlertDialog dialog=builder.create ();
dialog.show ();//顯示對(duì)話框
? ? }
private void showDialog5() {
//自定義對(duì)話框
? ? ? ? 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 ();//顯示對(duì)話框
? ? }
}
https://www.jb51.net/article/83192.htm