Android自定義對(duì)話框Dialog
本文簡(jiǎn)單介紹自定義對(duì)話框Dialog的使用,代碼和結(jié)構(gòu)都非常簡(jiǎn)單,目的是能夠快速使用自定義對(duì)話框,在本文中不具體講解對(duì)話框的高級(jí)使用。
實(shí)現(xiàn)步驟
首先需要自己在我們的.xml文件中自己構(gòu)建布局
布局文件做好之后,我們可以在style文件下自己定義布局的樣式
前兩步都做好之后,我開始在寫java文件
具體實(shí)現(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代碼,里面用到了一些簡(jiǎn)單的組建,大家按自己的需求和風(fēng)格制作就行。部分組件中用到了style屬性,該屬性我們同樣是在res/value/style文件中構(gòu)建.
注意:所有組件的首字母都要大寫。
2. style
<!-- 自定義對(duì)話框樣式 -->
<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è)構(gòu)造方法在方法里面引用我們的style文件,接下來的就是我們一般套路啦。特別提示一下我在該類中使用dismiss();來銷毀對(duì)話框。在MainActivity.java中,只需要把這個(gè)類實(shí)例化一下,創(chuàng)建出對(duì)象,調(diào)用對(duì)象的show();方法就可以將對(duì)話框顯示出來。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Activity實(shí)現(xiàn)簡(jiǎn)單的可輸入對(duì)話框
大家在做彈出對(duì)話框效果的時(shí)候最容易想到的是用Dialog顯示,但其實(shí)彈出對(duì)話框的實(shí)現(xiàn)效果有兩種:Dialog和Activity,那么下面這篇文章就來給大家介紹了關(guān)于Android使用Activity如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的可輸入對(duì)話框的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10
基于Android實(shí)現(xiàn)轉(zhuǎn)盤按鈕代碼
這篇文章主要介紹了基于Android實(shí)現(xiàn)轉(zhuǎn)盤按鈕代碼的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android仿微信左右滑動(dòng)點(diǎn)擊切換頁(yè)面和圖標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android仿微信左右滑動(dòng)點(diǎn)擊切換頁(yè)面和圖標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
解決Android ListView數(shù)據(jù)為空及加載錯(cuò)誤的方法
這篇文章主要為大家提供了一個(gè)解決Android ListView數(shù)據(jù)為空及加載錯(cuò)誤的方法,感興趣的小伙伴們可以參考一下2016-02-02
Android使用RecyclerView實(shí)現(xiàn)投票系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android使用RecyclerView實(shí)現(xiàn)投票系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
android學(xué)習(xí)筆記之View的滑動(dòng)
Android開發(fā)中我們常常需要View滑動(dòng)實(shí)現(xiàn)一些絢麗的效果來優(yōu)化用戶體驗(yàn),下面這篇文章主要給大家介紹了關(guān)于android學(xué)習(xí)筆記之View滑動(dòng)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
Android實(shí)現(xiàn)動(dòng)態(tài)改變shape.xml中圖形的顏色
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)改變shape.xml中圖形的顏色,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android提高之模擬信號(hào)示波器的實(shí)現(xiàn)
這篇文章主要介紹了Android模擬信號(hào)示波器的實(shí)現(xiàn)方法,在Android項(xiàng)目開發(fā)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
Android仿IOS UIAlertView對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android仿IOS UIAlertView對(duì)話框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android實(shí)現(xiàn)網(wǎng)頁(yè)圖片瀏覽功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)頁(yè)圖片瀏覽功能,輸入圖片的url然后點(diǎn)擊按鈕加載出來圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

