Android?Studio使用自定義對(duì)話框效果
Android Studio基礎(chǔ)使用自定義對(duì)話框,供大家參考,具體內(nèi)容如下
兼容低版本的APP運(yùn)行
第一步:新建新的空白activity,布局XML設(shè)置如下
該APP的啟動(dòng)界面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="顯示單選對(duì)話框"/> ? ? <Button ? ? ? ? android:id="@+id/btn_alert_customer" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="自定義對(duì)話框"/> ? </LinearLayout>
自定義的對(duì)話框item_alert_dialog的布局XML設(shè)置
<?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
第三步:對(duì)話框?qū)崿F(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("單選對(duì)話框").setIcon(R.mipmap.ic_launcher).setSingleChoiceItems(new String[]{"中國(guó)", "德國(guó)", "日本"}, 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)為自定義的對(duì)話框,或圖片等等 ? ? ? ? ? ? ? ? builder.setTitle("自定義對(duì)話框").setIcon(R.mipmap.ic_launcher).setView(R.layout.item_alert_dialog); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? AlertDialog ad = builder.create(); ? ? ? ? ad.show(); ? ? } }
第四步:運(yùn)行APP效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)單選對(duì)話框
- Android studio自定義對(duì)話框效果
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
相關(guān)文章
Android 廣播監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼
這篇文章主要介紹了Android 廣播監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Kotlin類與屬性及構(gòu)造函數(shù)的使用詳解
這篇文章主要介紹了Kotlin語(yǔ)言中類與屬性及構(gòu)造函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Android Studio實(shí)現(xiàn)補(bǔ)間動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)補(bǔ)間動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開(kāi)回彈效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)背景圖滑動(dòng)變大松開(kāi)回彈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04android通過(guò)led實(shí)現(xiàn)手電筒功能
這篇文章主要為大家詳細(xì)介紹了android通過(guò)led實(shí)現(xiàn)手電筒功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android 實(shí)現(xiàn)截屏功能的實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)截屏功能的實(shí)例的相關(guān)資料,這里實(shí)現(xiàn)截屏的實(shí)例在代碼中注釋非常清楚,希望能幫助到大家,需要的朋友可以參考下2017-08-08