版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
【移動應(yīng)用開發(fā)技術(shù)】自定義Dialog
需要實現(xiàn)的效果圖:
一個項目所用到的對話框風(fēng)格必須統(tǒng)一,但顯示的文字、布局卻可以不同。如果每個對話框都要重新去創(chuàng)建的話,會增加代碼的冗余,浪費資源。所以可以寫個類MyDialog繼承Dailog。需要的時候直接調(diào)用MyDialog類。一、新建xml布局。<?xml
version="1.0"
encoding="utf-8"?>
<FrameLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:textSize="18dp"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center"
android:textColor="#000000"
android:textStyle="bold"
android:text="InPut"
/>
<LinearLayout
android:id="@+id/content"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_message"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="left|center"
android:textSize="16dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textColor="#333333"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:gravity="left|center"
>
<EditText
android:id="@+id/et_psw_message"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="left|center"
android:inputType="textPassword"
android:textColorHint="@color/gray"
android:visibility="gone"
android:maxLength="30"
android:textSize="@dimen/font_14"
android:background="@drawable/edit_back">
<requestFocus
/>
</EditText>
<ImageView
android:id="@+id/img_change_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:visibility="gone"
android:src="@drawable/img_change_name_style"
/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1.0px"
android:background="#ffd0d0d0"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:background="#ffd0d0d0"
android:gravity="center"
android:orientation="horizontal"
>
<Button
android:id="@+id/negativeButton"
android:textSize="17dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#ffffff"
android:layout_weight="1"
android:text="Cancel"
/>
<Button
android:id="@+id/positiveButton"
android:textSize="17dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="1px"
android:background="#ffffff"
android:gravity="center"
android:layout_weight="1"
android:text="Sure"
android:textStyle="bold"
android:textColor="#008ae7"
/>
</LinearLayout>
</LinearLayout>
</FrameLayout>xml效果圖如下:
盡量將對話框要用到的所有控件在xml中排好,部分不需要的可以在代碼setVisibility(View.GONE);讓其消失,省的新建多個xml文件。二、繼承Dailogpackage
com.dnake.evertalk.widgets;
import
com.dnake.evertalk.R;
import
android.app.Dialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.Window;
import
android.view.ViewGroup.LayoutParams;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.LinearLayout;
import
android.widget.TextView;
public
class
MyDialog
extends
Dialog
{
public
MyDialog(Context
context)
{
super(context);
}
public
MyDialog(Context
context,
int
theme)
{
super(context,
theme);
}
public
static
class
Builder
{
private
Context
context;
private
String
title;
private
String
message;
private
String
positiveButtonText;
private
String
negativeButtonText;
private
View
contentView;
private
DialogInterface.OnClickListener
positiveButtonClickListener;
private
DialogInterface.OnClickListener
negativeButtonClickListener;
public
Builder(Context
context)
{
this.context
=
context;
}
public
Builder
setMessage(String
message)
{
this.message
=
message;
return
this;
}
public
Builder
setMessage(int
message)
{//設(shè)置信息
this.message
=
(String)
context.getText(message);
return
this;
}
public
Builder
setTitle(int
title)
{
//設(shè)置標(biāo)題
this.title
=
(String)
context.getText(title);
return
this;
}
public
Builder
setTitle(String
title)
{//直接傳字符串
this.title
=
title;
return
this;
}
public
Builder
setContentView(View
v)
{
this.contentView
=
v;
return
this;
}
/**
*
Set
the
positive
button
resource
and
it's
listener
*/
public
Builder
setPositiveButton(int
positiveButtonText,DialogInterface.OnClickListener
listener)
{
this.positiveButtonText
=
(String)
context.getText(positiveButtonText);
this.positiveButtonClickListener
=
listener;
return
this;
}
public
Builder
setPositiveButton(String
positiveButtonText,DialogInterface.OnClickListener
listener)
{
this.positiveButtonText
=
positiveButtonText;
this.positiveButtonClickListener
=
listener;
return
this;
}
public
Builder
setNegativeButton(int
negativeButtonText,DialogInterface.OnClickListener
listener)
{
this.negativeButtonText
=
(String)
context.getText(negativeButtonText);
this.negativeButtonClickListener
=
listener;
return
this;
}
public
Builder
setNegativeButton(String
negativeButtonText,DialogInterface.OnClickListener
listener)
{
this.negativeButtonText
=
negativeButtonText;
this.negativeButtonClickListener
=
listener;
return
this;
}
public
void
create(View
layout)
{
final
MyDialog
dialog
=
new
MyDialog(context,R.style.dialog_theme);
dialog.addContentView(layout,
new
LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
((TextView)
layout.findViewById(R.id.title)).setText(title);
if
(positiveButtonText
!=
null)
{
((Button)
layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
if
(positiveButtonClickListener
!=
null)
{
((Button)
layout.findViewById(R.id.positiveButton)).setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
positiveButtonClickListener.onClick(dialog,DialogInterface.BUTTON_POSITIVE);
}
});
}
}
else
{
layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
}
if(message
!=
null)
{
TextView
hint
=
(TextView)
layout.findViewById(R.id.tv_message);
hint.setText(message);
}
if
(negativeButtonText
!=
null)
{
((Button)
layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
if
(negativeButtonClickListener
!=
null)
{
((Button)
layout.findViewById(R.id.negativeButton)).setOnClickListener(new
View.OnClickListener()
{
public
void
onClick(View
v)
{
negativeButtonClickListener.onClick(dialog,DialogInterface.BUTTON_NEGATIVE);
}
});
}
}
else
{
layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
}
dialog.setContentView(layout);
dialog.show();
}
}
}三、在Activity中創(chuàng)建dialog,設(shè)置標(biāo)題、信息以及按鈕的處理事件
private
void
showDialog()
{
LayoutInflater
inflater
=
(LayoutInfl
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度個人住宅水電安全檢測與維修服務(wù)合同4篇
- 2024年企業(yè)、公司經(jīng)營管理戰(zhàn)略方案及技巧知識考試題庫(附含答案)
- 2025版探礦權(quán)轉(zhuǎn)讓協(xié)議范本:礦產(chǎn)資源合作開發(fā)新策略3篇
- 2025版新能源產(chǎn)業(yè)園區(qū)土地合作開發(fā)協(xié)議書3篇
- 2025版施工安全協(xié)議書:高空作業(yè)安全協(xié)議范本3篇
- 二零二五年度車輛租賃合同車輛租賃保險條款4篇
- 合作式學(xué)習(xí)在小學(xué)數(shù)學(xué)課堂中的應(yīng)用案例
- 2025版文藝團體演出合作委托合同3篇
- 跨文化交流拓寬視野培養(yǎng)孩子獨立見解
- 甘肅2025年甘肅西北師范大學(xué)誠聘海內(nèi)外高層次人才160人筆試歷年參考題庫附帶答案詳解
- 醫(yī)院6s管理成果匯報護理課件
- 泵站運行管理現(xiàn)狀改善措施
- 2024屆武漢市部分學(xué)校中考一模數(shù)學(xué)試題含解析
- SYT 0447-2014《 埋地鋼制管道環(huán)氧煤瀝青防腐層技術(shù)標(biāo)準(zhǔn)》
- 第19章 一次函數(shù) 單元整體教學(xué)設(shè)計 【 學(xué)情分析指導(dǎo) 】 人教版八年級數(shù)學(xué)下冊
- 浙教版七年級下冊科學(xué)全冊課件
- 弧度制及弧度制與角度制的換算
- 瓦楞紙箱計算公式測量方法
- 江蘇省中等職業(yè)學(xué)校學(xué)業(yè)水平考試商務(wù)營銷類(營銷方向)技能考試測試題
- DB32-T 4004-2021水質(zhì) 17種全氟化合物的測定 高效液相色譜串聯(lián)質(zhì)譜法-(高清現(xiàn)行)
- DB15T 2724-2022 羊糞污收集處理技術(shù)規(guī)范
評論
0/150
提交評論