您的位置:首页 > 移动开发 > Android开发

自定义dialog

2015-09-18 08:52 501 查看
android的Alertdialog的7种形式http://www.oschina.net/question/54100_32486

根据今天的需求查阅资料写了自定义的dialog

几个步骤:

1、继承

2、添加构造

3、写静态类Builder(1.内部类拥有其外围类的所有元素的访问权。2.外部类必须指明内部类的对象类型,利用内部类对象访问内部类成员)

4、内部类主要实现自己需要用的方法,基本有settitle,setmessage,setpositivebuttontext,setpositivebuttonclick,setnegativebuttonclick,setnegativebuttonclick(这些只是名字,不喜欢可以取别的名字,在调用时对应即可)

先上张效果图



demo:

public class BaseDialog extends Dialog{

public BaseDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
}
public BaseDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

public static class Builder{
private String title;
private Context context;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentview;
private Boolean cancle;
private DialogInterface.OnClickListener positiveButtonClickListener;
private DialogInterface.OnClickListener negativeButtonClickListener;

public Builder(Context context){
this.context=context;
}

public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
public Builder setMessage(String message){
this.message=message;
return this;
}

public Builder setTitle(int title) {
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;
}

public Builder setCancelable(Boolean cancle){
this.cancle=cancle;
return this;
}

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;
}<pre name="code" class="java">	public BaseDialog create(){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final BaseDialog dialog=new BaseDialog(context, R.style.Dialog);
View layout=inflater.inflate(R.layout.dialog_normal_layout, null);
dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, 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 (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);
}
if (message != null) {
((TextView) layout.findViewById(R.id.message)).setText(message);
} else if (contentview != null) {
((LinearLayout) layout.findViewById(R.id.message))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.message)).addView(
contentview, new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}

if (cancle != null) {
dialog.setCancelable(cancle);
}
dialog.setContentView(layout);
return dialog;
}


</pre><pre name="code" class="java">dialog的布局文件:<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical"
>

<LinearLayout
android:layout_width="264dp"
android:layout_height="130dp"
android:layout_gravity="center"
android:orientation="vertical" >

<TextView
android:id="@+id/title"
style="@style/text_18_ffffff"
android:layout_width="fill_parent"
android:layout_height="32dp"
android:background="@drawable/dialog_out_bolder_top"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:visibility="visible" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="98dip"
android:background="@drawable/dialog_out_bolder_bottom" >

<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:paddingLeft="16.0dip"
android:paddingTop="15.0dip" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="32.0dip"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"
android:orientation="horizontal" >

<Button
android:id="@+id/negativeButton"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@drawable/dialog_back"
android:textSize="14sp"
/>

<Button
android:id="@+id/positiveButton"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:layout_marginLeft="8dp"
android:gravity="center"
android:background="@drawable/dialog_open"
android:textSize="14sp"
android:textColor="#ffffff"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
下面就是在activity中进行调用,就和调用Alertdialog是一样的。

BaseDialog.Builder builder = new BaseDialog.Builder(this);
builder.setMessage("自定义");
builder.setTitle("我是提示框");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();

}
});

builder.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

builder.create().show();


源码下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息