通用的Dialog自定义dialog

时间:2022-08-05 22:23:34

图样:

通用的Dialog自定义dialog通用的Dialog自定义dialog

1.自定义dialog的用法

YouMengDialog userMsgDialog=null;
private void initTipDialog() {
userMsgDialog = new YouMengDialog (this);
userMsgDialog.setTitle("系统提示!");
userMsgDialog.setMessage("提示的内容!");
userMsgDialog.setCanceledOnTouchOutside(false);
 userMsgDialog.setButton1("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {
userMsgDialog.dismiss();

}
});

userMsgDialog.setButton2("确定", new View.OnClickListener() {
@Override
public void onClick(View v) {
userMsgDialog.dismiss();

}
});
userMsgDialog.show();
}

 

2.dialog的java代码

public class YouMengDialog extends Dialog {
private View contentView;
public YouMengDialog(Context context){
super(context, R.style.tran_AlertDialog);
LayoutInflater inflater = LayoutInflater.from(context);
this.contentView = inflater.inflate(R.layout.dialog, null);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(contentView);
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
Display d = window.getWindowManager().getDefaultDisplay();
lp.width = (int) (d.getWidth() * 0.9F);
window.setAttributes(lp);
}

//设置提示框标题
public void setTitle(String title){
final TextView textView = (TextView) contentView.findViewById(R.id.title);
textView.setText(title);
}
//设置提示的内容
public void setMessage(String message){
final TextView textView = (TextView) contentView.findViewById(R.id.message);
textView.setText(message);
}
//对提示框的第一个按键(取消按钮)设置,title:按钮上的文字,onclicklistener :触发的事件
public void setButton(String title,View.OnClickListener onClickListener){
final Button button = (Button) contentView.findViewById(R.id.dialogButton);
button.setText(title);
button.setOnClickListener(onClickListener);
}

//当只需要一个确定键的时候让取消键隐藏
public void setButtonGone(){
Button button = (Button) contentView.findViewById(R.id.dialogButton);
button.setVisibility(View.GONE);

}
//对提示框的第二个按键(确定按钮)设置,title:按钮上的文字,onclicklistener :触发的事件

public void setButton2(String title,View.OnClickListener onClickListener){
final Button button = (Button) contentView.findViewById(R.id.dialogButton2);
button.setText(title);
button.setOnClickListener(onClickListener);
}
}


3.用到的样式表
<style name="tran_AlertDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowBackground">@drawable/gray_corners_border</item>
<!-- 自己想要的背景 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
</style>


4.dialog的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/gray_bottom_border"
android:padding="10dp"
android:gravity="center_vertical">
<ImageView
android:src="@drawable/jinggao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="25dp"
android:maxHeight="25dp"
android:scaleType="fitCenter"
android:layout_marginRight="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示信息"
android:id="@+id/title"
android:textColor="@color/black"
android:textSize="@dimen/txt_20"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示信息"
android:id="@+id/message"
android:textColor="@color/black"
android:textSize="@dimen/txt_20"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="取消"
android:background="@drawable/gray_corners_button"
android:id="@+id/dialogButton"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp" android:textSize="@dimen/txt_20"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="确认"
android:background="@drawable/green_corners_button"
android:id="@+id/dialogButton2"
android:textSize="@dimen/txt_20"
android:textColor="@color/white"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_weight="1" />
</LinearLayout></LinearLayout>