Android開發(fā)之AlertDialog實現(xiàn)彈出對話框
本文實例為大家分享了Android開發(fā)之AlertDialog實現(xiàn)彈出對話框的具體代碼,供大家參考,具體內(nèi)容如下
基本框架
我們在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"> ? ? <Button ? ? ? ? android:text="顯示對話框" ? ? ? ? android:onClick="display" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> </LinearLayout>
然后在java代碼中編寫點擊事件的響應(yīng):
package com.example.myalertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }
? ? public void display(View view) {
? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(this);
? ? ? ? builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
? ? ? ? ? ? ? ? .setTitle("對話框")
? ? ? ? ? ? ? ? .setMessage("Hello")
? ? ? ? ? ? ? ? .create()
? ? ? ? ? ? ? ? .show();
? ? }
}構(gòu)造方法
先聲明一個構(gòu)造器對象builder:AlertDialog.Builder builder=new AlertDialog.Builder(this);
之后就可以用該構(gòu)造器的各種方法設(shè)置對話框的屬性:
builder.setIcon(int iconId); 添加圖標(biāo)builder.setMessage(CharSequence message); 添加消息builder.setTitle(CharSequence title); 添加標(biāo)題builder.setView(View view); 設(shè)置自定義布局builder.create(); 創(chuàng)建對話框builder.show(); 顯示對話框
上面的這些函數(shù)都是可以鏈?zhǔn)秸{(diào)用的(詳見基本框架),不過由于setXXX是Builder函數(shù),create函數(shù)返回Dialog變量,而show是void函數(shù),所以這三類函數(shù)的順序不能交換,setXXX函數(shù)的內(nèi)部順序可交換。
運行基本框架中的代碼就可以得到一個簡單的彈出對話框:

添加按鈕
常見的對話框一般還有按鈕,包含確認(rèn)、取消等按鈕,我們也可以在java代碼中設(shè)置并聲明點擊事件:
package com.example.myalertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }
? ? public void display(View view) {
? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(this);
? ? ? ? builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
? ? ? ? ? ? ? ? .setTitle("對話框")
? ? ? ? ? ? ? ? .setMessage("Hello")
? ? ? ? ? ? ? ? .setPositiveButton("OK", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊確認(rèn)");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊取消");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNeutralButton("middle", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊中性");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .create()
? ? ? ? ? ? ? ? .show();
? ? }
}
點擊對應(yīng)按鈕可以看到事件被觸發(fā):

這三個按鈕可以根據(jù)自己的需要進行取舍與設(shè)置。
設(shè)置自定義布局
在layout文件夾中新建資源文件:

隨便添加一點ImageView和TextView:
<?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:background="@color/purple_500" ? ? android:orientation="horizontal"> ? ? <ImageView ? ? ? ? android:src="@mipmap/ic_launcher" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> ? ? <TextView ? ? ? ? android:text="Android" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> </LinearLayout>
在java代碼中利用該資源文件生成一個View:
View dialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);
之后就可以將對話框的步距設(shè)置為該View了:
.setView(dialog_view)

MainActivity.java完整代碼:
package com.example.myalertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }
? ? public void display(View view) {
? ? ? ? View dialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);
? ? ? ? AlertDialog.Builder builder=new AlertDialog.Builder(this);
? ? ? ? builder.setIcon(R.drawable.ic_baseline_all_inclusive_24)
? ? ? ? ? ? ? ? .setTitle("對話框")
? ? ? ? ? ? ? ? .setMessage("Hello")
? ? ? ? ? ? ? ? .setPositiveButton("OK", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊確認(rèn)");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊取消");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setNeutralButton("middle", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點擊中性");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .setView(dialog_view)
? ? ? ? ? ? ? ? .create()
? ? ? ? ? ? ? ? .show();
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之二:Activity的四種launchMode,有需要的可以了解一下。2016-11-11
Android實現(xiàn)TCP客戶端接收數(shù)據(jù)的方法
這篇文章主要介紹了Android實現(xiàn)TCP客戶端接收數(shù)據(jù)的方法,較為詳細的分析了Android基于TCP實現(xiàn)客戶端接收數(shù)據(jù)的相關(guān)技巧與注意事項,需要的朋友可以參考下2016-04-04
Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器
這篇文章主要介紹了Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Android TabLayout(選項卡布局)簡單用法實例分析
這篇文章主要介紹了Android TabLayout(選項卡布局)簡單用法,結(jié)合實例形式簡單分析了Android選項卡布局的界面布局與功能實現(xiàn)具體相關(guān)技巧,需要的朋友可以參考下2016-01-01

