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

首頁 > 學院 > 開發設計 > 正文

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

2019-11-09 15:15:45
字體:
來源:轉載
供稿:網友

因項目需要,需要一個彈出對話框,但對話框的布局和界面不是默認的,而是需要自己自定義的界面

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

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

參數: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="當前位置"          />      <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="經度:"                    />             <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="地點名稱:"          />         <EditText           android:id="@+id/addressNameText"          android:layout_width="300dp"          android:layout_height="wrap_content"          android:textSize="18sp"             />         </LinearLayout>             </RelativeLayout>

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

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

要是不明白參數可以百度一下

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

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

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽阳市| 吉木萨尔县| 马边| 桓仁| 张北县| 合阳县| 陆河县| 桐柏县| 错那县| 南平市| 神农架林区| 阿拉善右旗| 神池县| 赣榆县| 鄱阳县| 金门县| 台江县| 资阳市| 凌源市| 沧源| 和平县| 侯马市| 衡山县| 都昌县| 舟山市| 山阳县| 乌兰浩特市| 茶陵县| 信丰县| 滨海县| 通州区| 苏尼特左旗| 保靖县| 蒙山县| 铁力市| 桂阳县| 天全县| 横山县| 建宁县| 罗定市| 浠水县|