您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android Studio怎么使用自定義對(duì)話框效果的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Android Studio怎么使用自定義對(duì)話框效果文章都會(huì)有所收獲,下面我們一起來看看吧。
兼容低版本的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效果
關(guān)于“Android Studio怎么使用自定義對(duì)話框效果”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Android Studio怎么使用自定義對(duì)話框效果”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。