以前一直不大喜欢android系统的对话框,这两天上班比较空闲,就试着模仿一下QQ浏览器的对话框。
这个对话框的布局由标题部分,内容部分,和底部按钮部分组成,最多3个按钮,需要哪些按钮,只要设置这些按钮的listener就可以显示出来;内容部分,可以设置为自定义的View,也可以是简单的一个TextView(通过setMessage方法)。布局如下dialog.xml:
测试界面,主要就2个按钮,主要看看不同的2个dialog,第一个显示3个按钮,中间内容是一个TextView;第二个显示一个按钮,中间是自定义的View:
本来以前想写个类继承Dialog,但受水平限制,没有实现。主要代码如下:
public class CustomAlertDialog
{
private AlertDialog dialog;
private LayoutInflater inflater;
private Button leftBtn, midBtn, rightBtn;
private LinearLayout titleLay, bottomLay;
private ScrollView contentLay;
private TextView title, contentMsg;
private Context context;
private View dialogView;
public CustomAlertDialog(Context context)
{
this.context = context;
inflater = LayoutInflater.from(context);
dialogView = inflater.inflate(R.layout.dialog, null);
bottomLay = (LinearLayout) dialogView.findViewById(R.id.dialog_btns);
contentLay = (ScrollView) dialogView.findViewById(R.id.dialog_content_lay);
dialog = new AlertDialog.Builder(context).create();
}
public void show() {
dialog.show();
dialog.getWindow().setContentView(dialogView);
}
public void dismiss() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
public boolean isShowing() {
if (dialog != null)
return dialog.isShowing();
return false;
}
public void setCancelable(boolean bool) {
dialog.setCancelable(bool);
}
public void setContentView(View view) {
contentLay.removeAllViews();
contentLay.addView(view);
}
public void setMessage(String msg) {
contentMsg = (TextView) dialogView.findViewById(R.id.dialog_content);
contentMsg.setText(msg);
contentMsg.setVisibility(View.VISIBLE);
}
public void setTitle(String title) {
titleLay = (LinearLayout) dialogView.findViewById(R.id.dialog_title_lay);
this.title = (TextView) dialogView.findViewById(R.id.dialog_title);
titleLay.setVisibility(View.VISIBLE);
this.title.setText(title);
}
public void setOnPositiveButton(String str, View.OnClickListener listener) {
bottomLay.setVisibility(View.VISIBLE);
leftBtn = (Button) dialogView.findViewById(R.id.dialog_left_btn);
leftBtn.setVisibility(View.VISIBLE);
leftBtn.setText(str);
leftBtn.setOnClickListener(listener);
}
public void setOnNegativeButton(String str, View.OnClickListener listener) {
bottomLay.setVisibility(View.VISIBLE);
rightBtn = (Button) dialogView.findViewById(R.id.dialog_right_btn);
rightBtn.setVisibility(View.VISIBLE);
rightBtn.setText(str);
rightBtn.setOnClickListener(listener);
}
public void setOnNeutralButton(String str, View.OnClickListener listener) {
bottomLay.setVisibility(View.VISIBLE);
midBtn = (Button) dialogView.findViewById(R.id.dialog_mid_btn);
midBtn.setVisibility(View.VISIBLE);
midBtn.setText(str);
midBtn.setOnClickListener(listener);
}
}
这个对话框的布局由标题部分,内容部分,和底部按钮部分组成,最多3个按钮,需要哪些按钮,只要设置这些按钮的listener就可以显示出来;内容部分,可以设置为自定义的View,也可以是简单的一个TextView(通过setMessage方法)。布局如下dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="180dip" android:layout_height="160dip" >
<LinearLayout android:id="@+id/dialog_title_lay" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/alertdlg_title_bg" android:gravity="center_vertical" android:visibility="gone">
<TextView android:id="@+id/dialog_title" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="标题" android:textSize="18sp" android:textColor="#000000" android:textStyle="bold"
android:layout_marginLeft="10dip" android:layout_marginRight="10dip"/>
</LinearLayout>
<ScrollView android:id="@+id/dialog_content_lay" android:layout_width="fill_parent" android:layout_height="100dip"
android:background="@drawable/alertdlg_content_bg" android:gravity="center"
android:layout_below="@id/dialog_title_lay">
<TextView android:id="@+id/dialog_content" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="内容" android:textSize="16sp" android:textColor="#000000" android:visibility="gone"
android:layout_marginLeft="10dip" android:layout_marginRight="10dip" android:layout_marginTop="10dip"/>
</ScrollView>
<LinearLayout android:id="@+id/dialog_btns" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/alertdlg_bottom_bg" android:layout_below="@id/dialog_content_lay"
android:gravity="center" android:visibility="gone">
<Button android:id="@+id/dialog_left_btn" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="左按钮" android:background="@drawable/button_blue_normal" android:layout_marginRight="10dip"
android:layout_marginLeft="10dip" android:layout_weight="1" android:visibility="gone"/>
<Button android:id="@+id/dialog_mid_btn" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="中按钮" android:background="@drawable/button_blue_normal" android:layout_marginRight="10dip"
android:layout_marginLeft="10dip" android:layout_weight="1" android:visibility="gone"/>
<Button android:id="@+id/dialog_right_btn" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="右按钮" android:background="@drawable/button_blue_pressed" android:layout_marginRight="10dip"
android:layout_marginLeft="10dip" android:layout_weight="1" android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
测试界面,主要就2个按钮,主要看看不同的2个dialog,第一个显示3个按钮,中间内容是一个TextView;第二个显示一个按钮,中间是自定义的View:
public class TestCustomDialogActivity extends Activity {
Button button1, button2;
LayoutInflater inflater;
CustomAlertDialog customAd;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
inflater = LayoutInflater.from(this);
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
customAd = new CustomAlertDialog(TestCustomDialogActivity.this);
customAd.setTitle("密码被盗");
customAd.setMessage("互联网用户数据泄密事件继续发酵。继CSDN、天涯、新浪等互联网公司后," +
"京东商城、网易公司、支付宝也被卷入“泄密门”。昨日又有网友爆料称,交通银行和民生银行用户资料也遭外泄。" +
"昨日中午,有爆料称交通银行7000万客户、民生银行3500万用户以及工行用户资料外泄," +
"泄露数据包括用户的姓名、卡号、密码等敏感信息,并发布信息截图。" +
"对此,三家银行昨天均发布声明否认有用户资料外泄。业内专家也指出,银行卡交易系统不可能存在明文密码。");
customAd.setOnPositiveButton("确定", new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(TestCustomDialogActivity.this, "左按钮", Toast.LENGTH_SHORT).show();
customAd.dismiss();
}
});
customAd.setOnNeutralButton("其他", new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(TestCustomDialogActivity.this, "中按钮", Toast.LENGTH_SHORT).show();
customAd.dismiss();
}
});
customAd.setOnNegativeButton("取消", new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(TestCustomDialogActivity.this, "右按钮", Toast.LENGTH_SHORT).show();
customAd.dismiss();
}
});
customAd.setCancelable(false);
customAd.show();
}
});
button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
customAd = new CustomAlertDialog(TestCustomDialogActivity.this);
View view = inflater.inflate(R.layout.dialog_content, null);
customAd.setContentView(view);
customAd.setTitle("退出");
customAd.setOnPositiveButton("确定", new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(TestCustomDialogActivity.this, "确定退出", Toast.LENGTH_SHORT).show();
customAd.dismiss();
}
});
customAd.setCancelable(true);
customAd.show();
}
});
}
}
![](http://hi.csdn.net/attachment/201112/31/0_13253299008QMW.gif)