網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Android開發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框_Android
作者:ShadyPi ? 更新時(shí)間: 2022-11-10 編程語(yǔ)言本文實(shí)例為大家分享了Android開發(fā)之AlertDialog實(shí)現(xiàn)彈出對(duì)話框的具體代碼,供大家參考,具體內(nèi)容如下
基本框架
我們?cè)趚ml中添加一個(gè)按鈕用來(lái)喚出對(duì)話框:
<?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="顯示對(duì)話框" ? ? ? ? android:onClick="display" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> </LinearLayout>
然后在java代碼中編寫點(diǎn)擊事件的響應(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("對(duì)話框") ? ? ? ? ? ? ? ? .setMessage("Hello") ? ? ? ? ? ? ? ? .create() ? ? ? ? ? ? ? ? .show(); ? ? } }
構(gòu)造方法
先聲明一個(gè)構(gòu)造器對(duì)象builder
:AlertDialog.Builder builder=new AlertDialog.Builder(this);
之后就可以用該構(gòu)造器的各種方法設(shè)置對(duì)話框的屬性:
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)建對(duì)話框builder.show();
顯示對(duì)話框
上面的這些函數(shù)都是可以鏈?zhǔn)秸{(diào)用的(詳見基本框架),不過由于setXXX是Builder函數(shù),create函數(shù)返回Dialog變量,而show是void函數(shù),所以這三類函數(shù)的順序不能交換,setXXX函數(shù)的內(nèi)部順序可交換。
運(yùn)行基本框架中的代碼就可以得到一個(gè)簡(jiǎn)單的彈出對(duì)話框:
添加按鈕
常見的對(duì)話框一般還有按鈕,包含確認(rèn)、取消等按鈕,我們也可以在java代碼中設(shè)置并聲明點(diǎn)擊事件:
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("對(duì)話框") ? ? ? ? ? ? ? ? .setMessage("Hello") ? ? ? ? ? ? ? ? .setPositiveButton("OK", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) { ? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點(diǎn)擊確認(rèn)"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) { ? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點(diǎn)擊取消"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .setNeutralButton("middle", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) { ? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點(diǎn)擊中性"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .create() ? ? ? ? ? ? ? ? .show(); ? ? } }
點(diǎn)擊對(duì)應(yīng)按鈕可以看到事件被觸發(fā):
這三個(gè)按鈕可以根據(jù)自己的需要進(jìn)行取舍與設(shè)置。
設(shè)置自定義布局
在layout
文件夾中新建資源文件:
隨便添加一點(diǎn)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代碼中利用該資源文件生成一個(gè)View:
View dialog_view=getLayoutInflater().inflate(R.layout.dialog_view,null);
之后就可以將對(duì)話框的步距設(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("對(duì)話框") ? ? ? ? ? ? ? ? .setMessage("Hello") ? ? ? ? ? ? ? ? .setPositiveButton("OK", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) { ? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點(diǎn)擊確認(rèn)"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) { ? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點(diǎn)擊取消"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .setNeutralButton("middle", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialogInterface, int i) { ? ? ? ? ? ? ? ? ? ? ? ? Log.e("ShadyPi","點(diǎn)擊中性"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .setView(dialog_view) ? ? ? ? ? ? ? ? .create() ? ? ? ? ? ? ? ? .show(); ? ? } }
原文鏈接:https://blog.csdn.net/ShadyPi/article/details/123072074
相關(guān)推薦
- 2022-07-19 Angular @Inject 注解的實(shí)際應(yīng)用例子和工作原理淺析
- 2023-07-04 SpringBoot不在使用@Validated 做參數(shù)校驗(yàn)但是不想在Controller層怎么辦?
- 2022-04-25 C#使用NPOI設(shè)置Excel下拉選項(xiàng)_C#教程
- 2023-04-06 C語(yǔ)言順序表的基本操作(初始化,插入,刪除,查詢,擴(kuò)容,打印,清空等)_C 語(yǔ)言
- 2023-03-16 Python?asyncio異步編程常見問題小結(jié)_python
- 2023-02-27 pandas?pd.cut()與pd.qcut()的具體實(shí)現(xiàn)_python
- 2023-04-19 Android開發(fā)服務(wù)Service全面講解_Android
- 2022-08-20 Linux中sftp常用命令整理_linux shell
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支