根据项目需求Dialog简单封装

时间:2022-09-23 21:55:20

创建一个类继承自Dialog,重写里面的构造方法

 public class OperationFileDialog extends Dialog {
/*上下文*/

private Context context;
/*标题*/
private TextView title;
/*内容*/
private LinearLayout content;
/*取消*/
private Button cancel;
/*确定*/
private Button ok;
/*编辑框*/
private EditText editText;
/*文本*/
private TextView textView;
/*点击事件方法*/
private OnClick onClick;

public OperationFileDialog(Context context) {
super(context);
this.context = context;
initView();
}

public OperationFileDialog(Context context, int themeResId) {
super(context, themeResId);
this.context = context;
initView();
}
protected OperationFileDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
this.context = context;
initView();
}

private void initView() {
//给整个window设置成透明色
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
//设置dialog布局文件
setContentView(R.layout.dialog_file_operation);
/*找到空间*/
title = (TextView) findViewById(R.id.dialog_title);
content = (LinearLayout) findViewById(R.id.dialog_content);
cancel = (Button) findViewById(R.id.dialog_cancel);
ok = (Button) findViewById(R.id.dialog_ok);
//取消的点击事件
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClick != null) {
//onClick是一个接口,调用接口中的抽象方法cancel,cancel中的具体实现在调用这个dialog的地方
onClick.cancel();
}
dismiss();
}
});
//确定的点击事件
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClick != null) {
String content = "";
if (editText != null) {
content = editText.getText().toString();
}
onClick.ok(content);
}
dismiss();
}
});
setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
if (editText != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
}
/*设置String标题*/
public OperationFileDialog setOpTitle(String text) {
title.setText(text);
return this;
}
/*设置int标题*/
public OperationFileDialog setOpTitle(int id) {
title.setText(id);
return this;
}
/*设置取消String内容*/
public OperationFileDialog setCancel(String text) {
cancel.setText(text);
return this;
}
/*设置取消int内容*/
public OperationFileDialog setCancel(int id) {
cancel.setText(id);
return this;
}
/*设置确定String内容*/
public OperationFileDialog setOk(String text) {
ok.setText(text);
return this;
}
/*设置确定intg内容*/

public OperationFileDialog setOk(int id) {
ok.setText(id);
return this;
}
/*设置EditText hint内容*/
public void addEditContentViewHint(String hint) {
//创建EditText
editText = new EditText(context);
editText.setHint(hint);
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
editText.setTextColor(getContext().getResources().getColor(R.color.c_666666));
editText.setGravity(Gravity.CENTER);
editText.setBackgroundDrawable(null);
//创建一个线性布局参数
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//位置居中
lp.gravity = Gravity.CENTER;
//把参数设置给EditText
editText.setLayoutParams(lp);
//移除布局中所有的视图
content.removeAllViews();
//把编辑框添加到线性布局中
content.addView(editText);
}
/*添加编辑框内容*/
public void addEditContentView(String string) {
editText = new EditText(context);
editText.setText(string);
int point = string.lastIndexOf(".");
//设置光标
if (point == -1) {
editText.setSelection(0,string.length());
} else {
editText.setSelection(0,point);
}
//获取焦点
editText.requestFocus();
//
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
editText.setTextColor(getContext().getResources().getColor(R.color.c_666666));
editText.setGravity(Gravity.CENTER);
editText.setBackgroundDrawable(null);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.CENTER;
editText.setLayoutParams(lp);
content.removeAllViews();
content.addView(editText);
}
//获取编辑内容
public String getEditContentView() {
if (editText != null) {
return editText.getEditableText().toString();
}
return "";
}
/*添加文本内容*/
public void addTextContentView(String text) {
textView = new TextView(context);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
textView.setTextColor(getContext().getResources().getColor(R.color.c_666666));
textView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.CENTER;
textView.setLayoutParams(lp);
textView.setText(text);
content.removeAllViews();
content.addView(textView);
}
/添加点击事件/
public void addOnClick(OnClick onClick) {
this.onClick = onClick;
}
//显示
@Override
public void show() {
super.show();
}

//

//点击事件接口,具体实现在子类中
public interface OnClick {
public void cancel();

public void ok(String content);
}

}

其中的xml布局为

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_5_white"
android:orientation="vertical">

<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@drawable/shape_5_gray"
android:gravity="center"
android:text=""
android:textColor="@color/c_333333"
android:textSize="@dimen/font_22" />

<LinearLayout
android:id="@+id/dialog_content"
android:layout_width="match_parent"
android:layout_height="85dp"
android:gravity="center"
android:orientation="horizontal" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">


<Button
android:id="@+id/dialog_cancel"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="13dp"
android:background="@drawable/shape_3_gray"
android:text="" />


<Button
android:id="@+id/dialog_ok"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginLeft="14dp"
android:background="@drawable/shape_3_blue"
android:text="" />


</LinearLayout>

</LinearLayout>

界面预览

根据项目需求Dialog简单封装

使用

//创建实例
OperationFileDialog fileDialog = new OperationFileDialog(this);
//设置标题
fileDialog.setOpTitle(R.string.create_folder).setCancel(R.string.op_cancel).setOk(R.string.op_create);
fileDialog.addEditContentView(getString(R.string.please_input_floder_name));
//添加点击事件
fileDialog.addOnClick(new OperationFileDialog.OnClick() {
@Override
public void cancel() {

}

@Override
public void ok(String content) {


}
});
//最重要的一步,显示
fileDialog.show();

项目中的dialog大概都是长这样,每次使用创建实例,设置不同的文本就行了,也可以在中间的线性布局中任意添加控件