網站首頁 編程語言 正文
本文實例為大家分享了Android開發之AlertDialog實現彈出對話框的具體代碼,供大家參考,具體內容如下
基本框架
我們在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代碼中編寫點擊事件的響應:
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(); ? ? } }
構造方法
先聲明一個構造器對象builder
:AlertDialog.Builder builder=new AlertDialog.Builder(this);
之后就可以用該構造器的各種方法設置對話框的屬性:
builder.setIcon(int iconId);
添加圖標builder.setMessage(CharSequence message);
添加消息builder.setTitle(CharSequence title);
添加標題builder.setView(View view);
設置自定義布局builder.create();
創建對話框builder.show();
顯示對話框
上面的這些函數都是可以鏈式調用的(詳見基本框架),不過由于setXXX是Builder函數,create函數返回Dialog變量,而show是void函數,所以這三類函數的順序不能交換,setXXX函數的內部順序可交換。
運行基本框架中的代碼就可以得到一個簡單的彈出對話框:
添加按鈕
常見的對話框一般還有按鈕,包含確認、取消等按鈕,我們也可以在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) { ? ? ? ? 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","點擊確認"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .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(); ? ? } }
點擊對應按鈕可以看到事件被觸發:
這三個按鈕可以根據自己的需要進行取舍與設置。
設置自定義布局
在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);
之后就可以將對話框的步距設置為該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","點擊確認"); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .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(); ? ? } }
原文鏈接:https://blog.csdn.net/ShadyPi/article/details/123072074
相關推薦
- 2024-03-16 docker 獲取鏡像幾種方式
- 2022-06-21 Android基于Sqlite實現注冊和登錄功能_Android
- 2022-11-25 使用openssl實現私有CA的搭建和證書的頒發_相關技巧
- 2022-05-08 python函數裝飾器構造和參數傳遞_python
- 2022-04-06 Python?numpy中的ndarray介紹_python
- 2022-06-12 C語言詳解熱門考點結構體內存對齊_C 語言
- 2022-06-20 .NET?Core企業微信網頁授權登錄的實現_實用技巧
- 2022-05-09 .NET中常見的加解密算法詳解_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支