package l.l;
import l.l.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
*创建自定义对话框窗口为您的应用程序
*自定义对话框依靠自定义布局未允许你
*创建和使用自己的外观和感觉。
*/
public class CustomDialog extends Dialog {
public CustomDialog(Context context, int theme) {
super(context, theme);
}
public CustomDialog(Context context) {
super(context);
}
/**
* Helper类创建一个自定义对话框
*/
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,
negativeButtonClickListener;
public Builder(Context context) {
this.context = context;
}
/**
* 设置对话框消息从字符串
* @param title
* @return
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* 设置对话框信息资源
* @param title
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* 设置对话框的标题从资源
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* 设置对话框的标题从字符串
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
*设置一个自定义内容视图对话框。
*如果一个消息集,contentView不是
*添加到对话框……
* @param v
* @return
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* 设置资源和它的积极按钮侦听器
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* 设置按钮的文本和它的积极的侦听器
* @param positiveButtonText
* @param listener
* @return
*/
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* 设置负按钮的侦听器资源和它
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* 设置按钮的文本和它的负面的侦听器
* @param negativeButtonText
* @param listener
* @return
*/
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* 创建自定义对话框
*/
public CustomDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final CustomDialog dialog = new CustomDialog(context,
R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
((TextView) layout.findViewById(R.id.title)).setText(title);
// set the confirm button
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) {
positiveButtonClickListener.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) {
// 如果没有消息集 的对话框添加contentView body
((LinearLayout) layout.findViewById(R.id.content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.content))
.addView(contentView,
new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
androidAlertDialog.zip (42个子文件)
androidAlertDialog
bin
classes.dex 7KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
footer.png 109B
ic_launcher.png 4KB
header.png 129B
center.png 123B
drawable-mdpi
ic_launcher.png 2KB
classes
l
l
CustomDialog$Builder$2.class 1KB
R$layout.class 379B
R$id.class 507B
R$drawable.class 481B
CustomDialog.class 561B
R$attr.class 295B
R.class 459B
CustomDialog$Builder.class 4KB
R$string.class 382B
LoginActivity.class 2KB
R$style.class 422B
CustomDialog$Builder$1.class 1KB
LoginActivity$1.class 820B
resources.ap_ 21KB
androidAlertDialog.apk 27KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
footer.png 264B
ic_launcher.png 4KB
header.png 237B
center.png 217B
title.png 8KB
values
strings.xml 174B
styles.xml 630B
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 426B
dialog.xml 2KB
assets
gen
l
l
R.java 1KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 208B
src
l
l
LoginActivity.java 2KB
CustomDialog.java 8KB
.project 854B
.classpath 364B
project.properties 361B
AndroidManifest.xml 664B
共 42 条
- 1
lsc466
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页