国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

彈出對話框AlertDialog.Builder使用筆記

2019-11-09 16:31:19
字體:
供稿:網(wǎng)友

因項(xiàng)目需要,需要一個彈出對話框,但對話框的布局和界面不是默認(rèn)的,而是需要自己自定義的界面

(一)先實(shí)例化一個Builder對象

AlertDialog.Builder addDialog = new AlertDialog.Builder(Context);

參數(shù):Context是上下文

(二)自定義布局add_address_dialog.xml,這個布局就是你自己彈出對話框的布局,我這里就要我的

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffffff"     >        <TextView         android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="26sp"        android:text="新增地址"        />          <View style="@style/horizontal_layout"          android:id="@+id/titleLine"          android:layout_below="@+id/title"          android:layout_width="match_parent"           />                        <TextView           android:id="@+id/contentTitle"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_centerHorizontal="true"          android:layout_below="@+id/titleLine"          android:textSize="20sp"          android:text="當(dāng)前位置"          />      <LinearLayout            android:id="@+id/contentShow"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_centerHorizontal="true"           android:layout_below="@+id/contentTitle"           android:layout_marginTop="20dp"           android:orientation="horizontal"               >      <TextView           android:id="@+id/altitued"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:textSize="20sp"          android:text="經(jīng)度:"                    />             <TextView           android:id="@+id/altituedText"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:textSize="20sp"          android:text="NULL"          />            <TextView           android:id="@+id/longtitued"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_marginLeft="60dp"          android:textSize="20sp"          android:text="緯度:"          />             <TextView           android:id="@+id/longtituedText"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:textSize="20sp"          android:text="NULL"          />       </LinearLayout>                   <View style="@style/horizontal_layout"          android:id="@+id/contentLine"          android:layout_below="@+id/contentShow"          android:layout_width="match_parent"          android:layout_marginTop="20dp"           />                        <LinearLayout          android:id="@+id/addressEdit"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_below="@+id/contentLine"          android:layout_centerHorizontal="true"          android:orientation="horizontal"           >        <TextView           android:id="@+id/addressName"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_gravity="center_vertical"          android:textSize="18sp"          android:text="地點(diǎn)名稱:"          />         <EditText           android:id="@+id/addressNameText"          android:layout_width="300dp"          android:layout_height="wrap_content"          android:textSize="18sp"             />         </LinearLayout>             </RelativeLayout>

(三)將布局轉(zhuǎn)化為View控件

View view = View.inflate(MainActivity.context, R.layout.add_address_dialog, null);

要是不明白參數(shù)可以百度一下

(四)只要將轉(zhuǎn)化的View控件加載就可以了

  addDialog.setView(view);//加載自定義布局   addDialog.show();//顯示對話框

總結(jié):Builder易用而且還靈活,可以加載自己的布局,而且它還有兩個按鈕屬性addDialog.setPositiveButton(CharSequence text,OnClickListener listener)和addDialog.setNegativeButton(CharSequence text,OnClickListener listener),以我的理解前者是肯定按鈕,后者是否定按鈕。如果有什么不對的大家可以指正,大家相互交流學(xué)習(xí),哈哈,,,


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 晋宁县| 朝阳区| 东光县| 长白| 涪陵区| 静宁县| 凯里市| 汝城县| 湘潭市| 高青县| 长海县| 庆安县| 栖霞市| 合川市| 平顶山市| 乌鲁木齐市| 化州市| 札达县| 木里| 乌兰察布市| 灵宝市| 团风县| 南开区| 临朐县| 靖安县| 琼结县| 尚志市| 社旗县| 积石山| 通河县| 满洲里市| 泽普县| 望谟县| 平凉市| 武宁县| 紫金县| 旬阳县| 白沙| 高安市| 赣榆县| 宝山区|