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 開機直接運行app并當(dāng)做手機桌面的實例
今天小編就為大家分享一篇Android 開機直接運行app并當(dāng)做手機桌面的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android UI設(shè)計與開發(fā)之實現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計與開發(fā)之實現(xiàn)應(yīng)用程序只啟動一次引導(dǎo)界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Windows下搭建Flutter開發(fā)環(huán)境
這篇文章介紹了Windows下搭建Flutter開發(fā)環(huán)境的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11詳解Android使用Html.fromHtml需要注意的地方
本篇文章主要介紹了詳解Android使用Html.fromHtml需要注意的地方,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07詳解如何使用Android Studio 進行NDK開發(fā)和調(diào)試
本篇文章主要介紹了詳解如何使用Android Studio 進行NDK開發(fā)和調(diào)試,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12