網(wǎng)站首頁 編程語言 正文
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
第三步:對話框實現(xiàn)抽象方式
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-24 淺析C++?atomic?和?memory?ordering_C 語言
- 2022-04-24 C語言字符函數(shù)中的isalnum()和iscntrl()你都知道嗎_C 語言
- 2022-10-05 python?matplotlib自定義colorbar顏色條及內置色條詳解_python
- 2022-03-14 跨域:Response to preflight request doesn t pass acce
- 2022-12-12 Python字典使用技巧詳解_python
- 2022-12-06 React.memo?和?useMemo?的使用問題小結_React
- 2022-03-14 Maven項目中遇見的一些問題(maven項目報錯)
- 2022-05-11 springboot多版本管理
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支