欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?Studio使用自定義對話框效果

 更新時間:2022年05月17日 17:20:19   作者:徐為波  
這篇文章主要為大家詳細介紹了Android?Studio使用自定義對話框效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android 廣播監(jiān)聽網(wǎng)絡狀態(tài)詳解及實例代碼

    Android 廣播監(jiān)聽網(wǎng)絡狀態(tài)詳解及實例代碼

    這篇文章主要介紹了Android 廣播監(jiān)聽網(wǎng)絡狀態(tài)詳解及實例代碼的相關資料,需要的朋友可以參考下
    2017-02-02
  • Kotlin類與屬性及構造函數(shù)的使用詳解

    Kotlin類與屬性及構造函數(shù)的使用詳解

    這篇文章主要介紹了Kotlin語言中類與屬性及構造函數(shù)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-09-09
  • Android Studio實現(xiàn)補間動畫

    Android Studio實現(xiàn)補間動畫

    這篇文章主要為大家詳細介紹了Android Studio實現(xiàn)補間動畫,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android實現(xiàn)背景圖滑動變大松開回彈效果

    Android實現(xiàn)背景圖滑動變大松開回彈效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)背景圖滑動變大松開回彈效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android Fragment的生命周期詳解

    Android Fragment的生命周期詳解

    本文主要講解Android Fragments的生命周期,這里整理了相關資料及對Fragment 的創(chuàng)建,有需要的小伙伴看下
    2016-09-09
  • android通過led實現(xiàn)手電筒功能

    android通過led實現(xiàn)手電筒功能

    這篇文章主要為大家詳細介紹了android通過led實現(xiàn)手電筒功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Android 實現(xiàn)截屏功能的實例

    Android 實現(xiàn)截屏功能的實例

    這篇文章主要介紹了Android 實現(xiàn)截屏功能的實例的相關資料,這里實現(xiàn)截屏的實例在代碼中注釋非常清楚,希望能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • Android 調用百度地圖API示例

    Android 調用百度地圖API示例

    在Android開發(fā)中有一個非常重要的應用就是實時定位,通過手機在手機地圖上進行實時定位,定位當前手機的位置,這篇文章主要介紹了Android 調用百度地圖API示例,有興趣的可以了解一下。
    2017-01-01
  • Android 中 Fragment的使用大全

    Android 中 Fragment的使用大全

    通常地 fragment做為宿主activity UI的一部分, 被作為activity整個view hierarchy的一部分被嵌入。本篇文章給大家介紹android fragment使用,需要的朋友一起看看吧
    2015-10-10
  • 詳解Android觀察者模式的使用與優(yōu)劣

    詳解Android觀察者模式的使用與優(yōu)劣

    這篇文章主要介紹了Android觀察者模式的相關資料,幫助大家更好的理解和學習Android的設計模式,感興趣的朋友可以了解下
    2020-09-09

最新評論