178 lines
6.4 KiB
Java
178 lines
6.4 KiB
Java
|
|
package com.tfq.library.utils;
|
||
|
|
|
||
|
|
import static androidx.core.content.ContextCompat.getSystemService;
|
||
|
|
|
||
|
|
import android.annotation.SuppressLint;
|
||
|
|
import android.app.Activity;
|
||
|
|
import android.app.AlertDialog;
|
||
|
|
import android.content.Context;
|
||
|
|
import android.content.DialogInterface;
|
||
|
|
import android.os.Handler;
|
||
|
|
import android.text.InputFilter;
|
||
|
|
import android.text.InputType;
|
||
|
|
import android.text.TextUtils;
|
||
|
|
import android.view.LayoutInflater;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.inputmethod.InputMethodManager;
|
||
|
|
import android.widget.Button;
|
||
|
|
import android.widget.EditText;
|
||
|
|
|
||
|
|
import com.tfq.library.R;
|
||
|
|
|
||
|
|
public class AddTextDialog {
|
||
|
|
boolean isClick = false;
|
||
|
|
private String name;
|
||
|
|
private String title;
|
||
|
|
private String type;
|
||
|
|
private Context mContext;
|
||
|
|
private Listener listener;
|
||
|
|
private ListenerFail listenerFail;
|
||
|
|
private AlertDialog dialog;
|
||
|
|
|
||
|
|
public AddTextDialog(Context context, Listener listener) {
|
||
|
|
this.mContext = context;
|
||
|
|
this.listener = listener;
|
||
|
|
}
|
||
|
|
|
||
|
|
public AddTextDialog(Context context, ListenerFail listenerFail) {
|
||
|
|
this.mContext = context;
|
||
|
|
this.listenerFail = listenerFail;
|
||
|
|
}
|
||
|
|
|
||
|
|
public AddTextDialog(Context context, String name, Listener listener) {
|
||
|
|
this.mContext = context;
|
||
|
|
this.listener = listener;
|
||
|
|
this.name = name;
|
||
|
|
}
|
||
|
|
|
||
|
|
public AddTextDialog(Context context, String name, String title, String type, Listener listener) {
|
||
|
|
this.mContext = context;
|
||
|
|
this.listener = listener;
|
||
|
|
this.name = name;
|
||
|
|
this.title = title;
|
||
|
|
this.type = type;
|
||
|
|
}
|
||
|
|
|
||
|
|
@SuppressLint("ClickableViewAccessibility")
|
||
|
|
public void show() {
|
||
|
|
// 创建编辑框视图
|
||
|
|
LayoutInflater inflater = LayoutInflater.from(mContext);
|
||
|
|
View inputView = inflater.inflate(R.layout.dialog_add_text_layout, null);
|
||
|
|
EditText et_text = inputView.findViewById(R.id.et_name);
|
||
|
|
|
||
|
|
if (!TextUtils.isEmpty(name)) {
|
||
|
|
et_text.setText(name);
|
||
|
|
et_text.setSelection(et_text.getText().toString().length());
|
||
|
|
}
|
||
|
|
if (!TextUtils.isEmpty(type) && type.equals("budgets")){
|
||
|
|
// 设置输入类型为带小数点的数字
|
||
|
|
et_text.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
|
||
|
|
// 添加输入过滤器
|
||
|
|
et_text.setFilters(new InputFilter[]{new DecimalInputFilter()});
|
||
|
|
|
||
|
|
// 焦点丢失时格式化显示
|
||
|
|
et_text.setOnFocusChangeListener((v, hasFocus) -> {
|
||
|
|
if (!hasFocus) {
|
||
|
|
String text = et_text.getText().toString();
|
||
|
|
if (!text.isEmpty()) {
|
||
|
|
try {
|
||
|
|
double value = Double.parseDouble(text);
|
||
|
|
et_text.setText(String.format("%.2f", value));
|
||
|
|
} catch (NumberFormatException e) {
|
||
|
|
et_text.setText("");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}else if (!TextUtils.isEmpty(type) && type.equals("nickname")){
|
||
|
|
et_text.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
|
||
|
|
}
|
||
|
|
|
||
|
|
et_text.requestFocus();
|
||
|
|
|
||
|
|
et_text.postDelayed(() -> {
|
||
|
|
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||
|
|
// 强制显示软键盘
|
||
|
|
imm.showSoftInput(et_text, InputMethodManager.SHOW_FORCED);
|
||
|
|
}, 100); // 延迟200ms等待布局渲染完成
|
||
|
|
|
||
|
|
|
||
|
|
// 创建对话框
|
||
|
|
dialog = new AlertDialog.Builder(mContext)
|
||
|
|
.setTitle(TextUtils.isEmpty(title) ? "创建记账项目" : title)
|
||
|
|
.setView(inputView)
|
||
|
|
.setPositiveButton("创建", null)
|
||
|
|
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||
|
|
@Override
|
||
|
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||
|
|
AppUtil.closeKeyBoard(et_text);
|
||
|
|
if (listenerFail != null) {
|
||
|
|
listenerFail.callCancel();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
.create();
|
||
|
|
|
||
|
|
// 设置对话框所属的Activity
|
||
|
|
if (mContext instanceof Activity) {
|
||
|
|
dialog.setOwnerActivity((Activity) mContext);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 使用工具类设置模糊背景和圆角
|
||
|
|
DialogUtils.setBlurBackground(dialog);
|
||
|
|
|
||
|
|
dialog.show();
|
||
|
|
|
||
|
|
Button confirmButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
|
||
|
|
confirmButton.setOnClickListener(new View.OnClickListener() {
|
||
|
|
@Override
|
||
|
|
public void onClick(View view) {
|
||
|
|
String trim = et_text.getText().toString().trim();
|
||
|
|
if (TextUtils.isEmpty(trim)) {
|
||
|
|
if (TextUtils.isEmpty(type)) {
|
||
|
|
ToasterUtil.show("请先输入记账名称");
|
||
|
|
} else if (type.equals("budgets")){
|
||
|
|
ToasterUtil.show("请先输入本月预算金额");
|
||
|
|
}else if (type.equals("nickname")){
|
||
|
|
ToasterUtil.show("用户昵称不能为空");
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if (listener != null && !isClick) {
|
||
|
|
AppUtil.closeKeyBoard(et_text);
|
||
|
|
isClick = true;
|
||
|
|
listener.success(trim);
|
||
|
|
dialog.dismiss();
|
||
|
|
}
|
||
|
|
if (listenerFail != null && !isClick) {
|
||
|
|
AppUtil.closeKeyBoard(et_text);
|
||
|
|
isClick = true;
|
||
|
|
listenerFail.success(trim);
|
||
|
|
dialog.dismiss();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
dialog.setOnCancelListener(dialog -> {
|
||
|
|
new Handler().postDelayed(() -> {
|
||
|
|
InputMethodManager imm2 = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||
|
|
imm2.hideSoftInputFromWindow(((Activity)mContext).getWindow().getDecorView().getWindowToken(), 0);
|
||
|
|
}, 100); // 延迟100ms避免窗口令牌销毁
|
||
|
|
if (listenerFail != null) {
|
||
|
|
listenerFail.callCancel();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public interface Listener {
|
||
|
|
void success(String name);
|
||
|
|
}
|
||
|
|
|
||
|
|
public interface ListenerFail {
|
||
|
|
void success(String name);
|
||
|
|
|
||
|
|
void callCancel();
|
||
|
|
}
|
||
|
|
}
|