日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

安卓进阶系列-03上弹选择框(PopupDialog)的使用

發(fā)布時(shí)間:2024/4/11 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓进阶系列-03上弹选择框(PopupDialog)的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

主要介紹上彈選擇框的使用,這個(gè)功能基于Dialog實(shí)現(xiàn),為安卓開發(fā)常見控件之一。實(shí)現(xiàn)方式并非利用第三方控件而是利用安卓原生對(duì)話框控件,不過樣式自定義了。

1.布局使用

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/white"android:orientation="vertical"><TextViewandroid:id="@+id/tv_test"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="?android:attr/selectableItemBackground"android:clickable="true"android:drawablePadding="16dp"android:gravity="center_vertical"android:padding="16dp"android:text="第一個(gè)選擇"android:textColor="#666666"android:textSize="14sp"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="?android:attr/selectableItemBackground"android:clickable="true"android:drawablePadding="16dp"android:gravity="center_vertical"android:padding="16dp"android:text="第二個(gè)選擇"android:textColor="#666666"android:textSize="14sp"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="?android:attr/selectableItemBackground"android:clickable="true"android:drawablePadding="16dp"android:gravity="center_vertical"android:padding="16dp"android:text="第三個(gè)選擇"android:textColor="#666666"android:textSize="14sp"/></LinearLayout>

2.代碼實(shí)現(xiàn)

package com.zc.testforpopupdialog;import android.app.Dialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn = (Button) findViewById(R.id.btn_test);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {showPopupDialog();}});}private void showPopupDialog() {Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);View contentView = LayoutInflater.from(this).inflate(R.layout.dialog, null);bottomDialog.setContentView(contentView);ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();layoutParams.width = getResources().getDisplayMetrics().widthPixels;contentView.setLayoutParams(layoutParams);bottomDialog.getWindow().setGravity(Gravity.BOTTOM);bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);bottomDialog.setCanceledOnTouchOutside(true);bottomDialog.show();// 這里一定要使用對(duì)話框的findViewByID,用Activity的將無法捕獲TextView tv = (TextView) bottomDialog.findViewById(R.id.tv_test);tv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this,"你點(diǎn)擊了第一個(gè)選項(xiàng)",Toast.LENGTH_LONG).show();}});} }

3.效果演示

?

注意:dialog上的控件必須使用該dialog調(diào)用findViewByID而不能使用Activity進(jìn)行綁定,因?yàn)檫@些控件本質(zhì)上是不存在Activity上的,而是暫存在dialog上。

項(xiàng)目GitHub地址:https://github.com/luanshiyinyang/TestForPopupDialog

總結(jié)

以上是生活随笔為你收集整理的安卓进阶系列-03上弹选择框(PopupDialog)的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。