本文所用的知识点很简单,但很实用,非喜勿喷
目录
一、通过 AlertDialog ,快速使用自定义Dialog 。
二、自定义AlertDialog的宽度
重点:AlertDialog 要在主线程才可以显示/隐藏。但DialogFragment可以在子线程中显示 /隐藏
正文:
一、通过 AlertDialog ,快速使用自定义Dialog 。
包含输入框。(等待框,普通的消息展示dialog道理相同。一般一个项目写三个通用的Dialog:等待框,普通的消息,包含输入框)
1、使用方法
MyDialogInputBuilder builder=new MyDialogInputBuilder(this);
//注意方法 setCancelable :alertDialog是否可以点击外围消失
builder.setTitle("title").setMessage("message").setCancelable(false);
builder.setPositiveButton("确认", new MyDialogInputBuilder.MyInputOnClickListener() {
@Override
public void onClick(DialogInterface dialog,String str) {
//String str 的值为用户输入的值
Log.w("test","PositiveButton onClick");
Toast.makeText(LoginActivity.this,"点击了确认按钮 str="+str,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.setNegativeButton("cancle", null);
alertDialog = builder.create();
alertDialog.show();
2、原码
public class MyDialogInputBuilder {
private Context context;
private String title;
private String message;
private String strPositive;
private String strNegative;
private MyInputOnClickListener positivelistener;
private MyInputOnClickListener negativelistener;
private boolean mCancelable = true;
public MyDialogInputBuilder(Context context) {
this.context=context;
deleteDate();
}
private void deleteDate(){
title=null;
message=null;
strPositive=null;
strNegative=null;
positivelistener=null;
negativelistener=null;
}
public MyDialogInputBuilder setTitle(String title) {
this.title = title;
return this;
}
public MyDialogInputBuilder setMessage(String message) {
this.message = message;
return this;
}
/**
* alertDialog是否可以点击外围消失
* @param cancelable true 表示可以消失
* @return
*/
public MyDialogInputBuilder setCancelable(boolean cancelable) {
this.mCancelable = cancelable;
return this;
}
public MyDialogInputBuilder setPositiveButton(String text, MyInputOnClickListener listener) {
this.strPositive = text;
this.positivelistener = listener;
return this;
}
public MyDialogInputBuilder setNegativeButton(String text, MyInputOnClickListener listener) {
this.strNegative = text;
this.negativelistener = listener;
return this;
}
private EditText inputText;
public AlertDialog create() {
AlertDialog.Builder builder=new AlertDialog.Builder(this.context);
View mView = LayoutInflater.from(this.context).inflate(R.layout.common_dialog_input, null);
TextView tvTitle = (TextView)mView.findViewById(R.id.common_dialog_input_tips_titile);
inputText = (EditText)mView.findViewById(R.id.common_dialog_input_tips_input);
Button btnPositive =(Button) mView.findViewById(R.id.common_dialog_input_tips_btn_yes);
Button btnNegative =(Button) mView.findViewById(R.id.common_dialog_input_tips_btn_no);
if(isEmpty(title)){
tvTitle.setVisibility(View.GONE);
}else{
tvTitle.setVisibility(View.VISIBLE);
tvTitle.setText(title.trim());
}
if (isEmpty(strPositive)) {
btnPositive.setVisibility(View.GONE);
} else {
btnPositive.setText(strPositive.trim());
}
if (isEmpty(strNegative)) {
btnNegative.setVisibility(View.GONE);
} else {
btnNegative.setText(strNegative.trim());
}
inputText.setHint(message.trim());
//tvMessage.setText(message.trim());
builder.setView(mView).setCancelable(mCancelable);
final AlertDialog dialog=builder.create();
dialog.setCancelable(mCancelable);
if (mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
if(!isEmpty(strPositive) && positivelistener==null){
btnPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}else if(!isEmpty(strPositive) && positivelistener!=null){
btnPositive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positivelistener.onClick(dialog,inputText.getText().toString());
}
});
}
if(!isEmpty(strNegative) && negativelistener==null){
btnNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}else if(!isEmpty(strNegative) && negativelistener!=null){
btnNegative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativelistener.onClick(dialog,inputText.getText().toString());
}
});
}
return dialog;
}
private boolean isEmpty(String str){
if (str==null || str.trim().equals("")){
return true;
}else{
return false;
}
}
public interface MyInputOnClickListener{
void onClick(DialogInterface dialog,String inputStr);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/common_dialog_bg"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:gravity="center"
>
<ImageView
android:id="@+id/common_dialog_input_tips_title_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@android:drawable/ic_dialog_info"
/>
<TextView
android:id="@+id/common_dialog_input_tips_titile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="温馨提示"
android:textColor="@android:color/white"
android:textSize="18dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
<EditText
android:id="@+id/common_dialog_input_tips_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
android:gravity="left"
android:textColor="@android:color/white"
android:textSize="16dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/common_dialog_input_tips_btn_no"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="@android:color/holo_green_dark"
android:gravity="center"
android:text="否"
android:textColor="@android:color/white" />
<Button
android:id="@+id/common_dialog_input_tips_btn_transfer"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_dark"
android:gravity="center"
android:text="转移"
android:textColor="@android:color/white"
android:visibility="gone"
/>
<Button
android:id="@+id/common_dialog_input_tips_btn_yes"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="7dip"
android:layout_weight="1"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:text="是"
android:textColor="@android:color/white" />
<!-- android:background="@android:color/holo_red_dark"-->
</LinearLayout>
</LinearLayout>
二、自定义AlertDialog的宽度
1、方法1 alertDialog.getWindow().setAttributes(LayoutParams );
if (window != null) {
window.getDecorView().setPadding(0, 0, 0, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
window.getDecorView().setBackground(context.getResources().getDrawable(res));
}
int width=0;
if(context instanceof Activity){
WindowManager manager = ((Activity)context).getWindowManager();
Display defaultDisplay = manager.getDefaultDisplay();
Point point = new Point();
defaultDisplay.getSize(point);
//int x = point.x;
width=point.x * 5 / 6;
}else{
width= Tools.dipTopx(context,300);
}
android.view.WindowManager.LayoutParams lp = alertDialog.getWindow()
.getAttributes();
lp.gravity = Gravity.CENTER;
lp.width = width;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
alertDialog.getWindow().setAttributes(lp);
alertDialog.setCancelable(touchCancel);
alertDialog.setCanceledOnTouchOutside(touchCancel);
window.setWindowAnimations(R.style.alertDialogStyle01);
}
2、方法2 。
dialog.getWindow().setLayout(DensityUtil.dip2px(context,300), LinearLayout.LayoutParams.WRAP_CONTENT);
DensityUtil.dip2px是dp转px的工具类