網站首頁 編程語言 正文
Android Studio基礎使用自定義對話框,供大家參考,具體內容如下
兼容低版本的APP運行
第一步:新建新的空白activity,布局XML設置如下
該APP的啟動界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? 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"> ? ? ? <Button ? ? ? ? android:id="@+id/btn_single" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="顯示單選對話框"/> ? ? <Button ? ? ? ? android:id="@+id/btn_alert_customer" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="自定義對話框"/> ? </LinearLayout>
自定義的對話框item_alert_dialog的布局XML設置
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? 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"> ? ? ? <Button ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="自定義Dialog界面!" ? ? ? ? /> ? ? <TextView ? ? ? ? android:layout_width="250dp" ? ? ? ? android:layout_height="250dp" ? ? ? ? android:background="@mipmap/ic_launcher"/> ? </LinearLayout>
第二步:在MainActivity.java中綁定布局xml
第三步:對話框實現抽象方式
package com.xwb.zdydhk;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button btn_single = findViewById(R.id.btn_single);
? ? ? ? btn_single.setOnClickListener(this);
? ? ? ? Button btn_alert_customer = findViewById(R.id.btn_alert_customer);
? ? ? ? btn_alert_customer.setOnClickListener(this);
? ? }
? ? @Override
? ? public void onClick(View v){
? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this);
? ? ? ? switch (v.getId()){
? ? ? ? ? ? case R.id.btn_single:
? ? ? ? ? ? ? ? builder.setTitle("單選對話框").setIcon(R.mipmap.ic_launcher).setSingleChoiceItems(new String[]{"中國", "德國", "日本"}, 0, new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"選中的"+which,Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.btn_alert_customer:
? ? ? ? ? ? ? ? //setView(R.layout.item_alert_dialog)為自定義的對話框,或圖片等等
? ? ? ? ? ? ? ? builder.setTitle("自定義對話框").setIcon(R.mipmap.ic_launcher).setView(R.layout.item_alert_dialog);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? AlertDialog ad = builder.create();
? ? ? ? ad.show();
? ? }
}
第四步:運行APP效果
原文鏈接:https://blog.csdn.net/xwbk12/article/details/115188589
相關推薦
- 2022-06-12 C語言數據結構順序表的進階講解_C 語言
- 2022-04-23 es6實現數組對象深度去重
- 2022-05-31 在.NET?MAUI應用中配置應用生命周期事件_實用技巧
- 2022-09-23 Pandas中Apply函數加速百倍的技巧分享_python
- 2022-04-15 python尋找含有關鍵字文件和刪除文件夾方式_python
- 2022-10-05 C語言二叉樹的概念結構詳解_C 語言
- 2022-09-21 Python?Ast抽象語法樹的介紹及應用詳解_python
- 2022-10-26 C++消息隊列(定義,結構,如何創建,發送與接收)_C 語言
- 最近更新
-
- 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同步修改后的遠程分支