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

首頁 > 系統(tǒng) > Android > 正文

Android自定義對話框Dialog

2019-12-12 03:10:24
字體:
供稿:網(wǎng)友

本文簡單介紹自定義對話框Dialog的使用,代碼和結(jié)構(gòu)都非常簡單,目的是能夠快速使用自定義對話框,在本文中不具體講解對話框的高級使用。

實現(xiàn)步驟

首先需要自己在我們的.xml文件中自己構(gòu)建布局
布局文件做好之后,我們可以在style文件下自己定義布局的樣式
前兩步都做好之后,我開始在寫java文件

具體實現(xiàn)過程

1.   xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="300dp"  android:layout_height="180dp"  android:gravity="center"  android:orientation="vertical">  <LinearLayout    android:layout_width="match_parent"    android:layout_height="40dp"    android:background="@android:color/holo_green_light">    <TextView      android:layout_width="match_parent"      android:layout_height="match_parent"      android:gravity="center_vertical"      android:text="IP設(shè)置"      android:textColor="#fff"      android:textSize="24sp" />  </LinearLayout>  <LinearLayout    android:layout_width="match_parent"    android:layout_height="0dp"    android:layout_weight="1"    android:background="#fff"    android:gravity="center"    android:orientation="horizontal"    android:padding="5dp">    <EditText      android:id="@+id/et_ip1"      style="@style/textview14sp"      android:layout_weight="1"      android:inputType="phone"      android:maxLength="3"      android:textColor="@color/colorPrimary" />    <EditText      android:id="@+id/et_ip2"      style="@style/textview14sp"      android:layout_weight="1"      android:inputType="phone"      android:maxLength="3"      android:textColor="@color/colorPrimary" />    <EditText      android:id="@+id/et_ip3"      style="@style/textview14sp"      android:layout_weight="1"      android:inputType="phone"      android:maxLength="3"      android:textColor="@color/colorPrimary" />    <EditText      android:id="@+id/et_ip4"      style="@style/textview14sp"      android:layout_weight="1"      android:inputType="phone"      android:maxLength="3"      android:textColor="@color/colorPrimary" />  </LinearLayout>  <LinearLayout    android:layout_width="match_parent"    android:layout_height="45dp"    android:orientation="horizontal">    <Button      android:id="@+id/btn_ipok"      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_weight="1"      android:background="@android:color/holo_green_light"      android:text="確認(rèn)"      android:textColor="#fff"      android:textSize="30sp" />    <View      android:layout_width="1dp"      android:layout_height="match_parent"      android:background="#fff" />    <Button      android:id="@+id/btn_ipcancle"      android:layout_width="0dp"      android:layout_height="match_parent"      android:layout_weight="1"      android:background="@android:color/holo_green_light"      android:text="取消"      android:textColor="#fff"      android:textSize="30sp" />  </LinearLayout></LinearLayout>

以上是我的xml代碼,里面用到了一些簡單的組建,大家按自己的需求和風(fēng)格制作就行。部分組件中用到了style屬性,該屬性我們同樣是在res/value/style文件中構(gòu)建.
注意:所有組件的首字母都要大寫。

2.  style

<!-- 自定義對話框樣式 -->  <style name="dialog_custom" parent="android:style/Theme.Dialog">    <item name="android:windowFrame">@null</item>    <item name="android:windowNoTitle">true</item>    <item name="android:background">#00000000</item>    <item name="android:windowBackground">@android:color/transparent</item>  </style>

3.  class文件

public class IP_dialog extends Dialog {  private Button btnOk, btnCancle;  private EditText ip1, ip2, ip3, ip4;  public static String ip = "";  public IP_dialog(Context context) {    super(context, R.style.dialog_custom);  }  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.dialog);    initView();    initEvet();  }  /*初始化組件*/  private void initView() {    btnOk = (Button) findViewById(R.id.btn_ipok);    btnCancle = (Button) findViewById(R.id.btn_ipcancle);    ip1 = (EditText) findViewById(R.id.et_ip1);    ip2 = (EditText) findViewById(R.id.et_ip2);    ip3 = (EditText) findViewById(R.id.et_ip3);    ip4 = (EditText) findViewById(R.id.et_ip4);  }  /*監(jiān)聽事件*/  private void initEvet() {    btnOk.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        ip = getIP();        Log.e("IP--->", ip);        dismiss();      }    });    btnCancle.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        dismiss();      }    });  }  /*獲取輸入的IP值*/  private String getIP() {    String ip = ip1.getText().toString().trim() + "."        + ip2.getText().toString().trim() + "."        + ip3.getText().toString().trim() + "."        + ip4.getText().toString().trim();    return ip;  }}

該類繼承Dialog,在該類中我們需要有一個構(gòu)造方法在方法里面引用我們的style文件,接下來的就是我們一般套路啦。特別提示一下我在該類中使用dismiss();來銷毀對話框。在MainActivity.java中,只需要把這個類實例化一下,創(chuàng)建出對象,調(diào)用對象的show();方法就可以將對話框顯示出來。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 望城县| 蒙城县| 苗栗县| 兴安盟| 黄大仙区| 龙山县| 马尔康县| 田阳县| 康定县| 兴宁市| 安西县| 威远县| 合阳县| 辽中县| 福鼎市| 米脂县| 东城区| 龙游县| 华蓥市| 迁安市| 洞头县| 东海县| 文登市| 隆安县| 酒泉市| 五指山市| 武定县| 古丈县| 驻马店市| 博兴县| 桃园市| 淮阳县| 靖江市| 射洪县| 瓦房店市| 大城县| 凤山县| 新邵县| 嘉黎县| 邯郸县| 夏河县|