public class InputDialog extends Dialog {
private TextView title;
private TextView confirm, cancel;
private EditText result;
private Class resultType = String.class;
private InputDialogListener mDialogListener;
public InputDialog(Context context) {
super(context, R.style.InputDialog);
LayoutInflater factory = LayoutInflater.from(context);
final View view = factory.inflate(R.layout.dialog_input, null);
setContentView(view);
initView(view);
initListener();
}
public void initView(View view) {
title = (TextView) view.findViewById(R.id.input_dialog_title);
confirm = (TextView) view.findViewById(R.id.input_dialog_confirm);
cancel = (TextView) view.findViewById(R.id.input_dialog_cancel);
result = (EditText) view.findViewById(R.id.input_dialog_result);
}
public void initListener() {
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mDialogListener != null) {
mDialogListener.onCancel();
InputDialog.this.dismiss();
}
}
});
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mDialogListener != null) {
String resultStr = result.getText().toString().trim();
try {
if (resultType == String.class)
mDialogListener.onConfirm(resultStr);
else if (resultType == Integer.class)
mDialogListener.onConfirm(Integer.parseInt(resultStr));
else if (resultType == Float.class)
mDialogListener.onConfirm(Float.parseFloat(resultStr));
else
mDialogListener.onConfirm(Float.parseFloat(resultStr));
InputDialog.this.dismiss();
} catch (Exception e) {
ToastUtils.showShortToast("输入内容不合法,请重新输入", getContext());
}
}
}
});
}
public void setOnClickListener(InputDialogListener listener) {
this.mDialogListener = listener;
}
public void setHite(String text) {
result.setHint(text);
}
public void setTitle(String text) {
title.setText(text);
}
public void setConfirmText(String text) {
confirm.setText(text);
}
public void setCancelText(String text) {
cancel.setText(text);
}
public void setResultType(Class type) {
this.resultType = type;
}
public interface InputDialogListener<T> {
void onCancel();
void onConfirm(T result);
}
}
dialog_input.xml
<?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:background="@android:color/transparent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="280dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_input_dialog"
android:orientation="vertical">
<TextView
android:id="@+id/input_dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/default_8dp"
android:text="标题"
android:textSize="18sp"/>
<EditText
android:id="@+id/input_dialog_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="14dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
<TextView
android:id="@+id/input_dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textSize="18sp"/>
<TextView
android:id="@+id/input_dialog_confirm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="确定"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
bg_input_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#efefef" />
<corners
android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp" />
</shape>
注意 上面代码中的
public InputDialog(Context context) {
super(context, R.style.InputDialog);
LayoutInflater factory = LayoutInflater.from(context);
final View view = factory.inflate(R.layout.dialog_input, null);
setContentView(view);
initView(view);
initListener();
}
这里引用自定义样式否则就会出现 默认Dialog 的title一样的空白头