1、加载自定义的弹窗内容
private void showDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("弹窗标题");
LayoutInflater inflater = LayoutInflater.from(context);
//弹窗需要展示什么内容,就在 R.layout.my_dialog 自己添加
View root = inflater.inflate(R.layout.my_dialog, null);
builder.setView(root);
AlertDialog dialog = builder.create();
dialog.show();
}
2、自定义弹窗宽高(按屏幕总宽高的百分比来设置)要在 dialog.show() 之后调用
private static final float SCALE_WIDTH = 0.75f;//弹窗宽度百分比
private static final float SCALE_HEIGHT = 0.8f;//弹窗高度百分比
private void setDialogSize(Context context, AlertDialog dialog) {
Window window = dialog.getWindow();
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
WindowManager.LayoutParams dialogParems = window.getAttributes();
dialogParems.width = (int) (screenWidth * SCALE_WIDTH);//可以设置具体的值,比如500,单位是px
dialogParems.height = (int) (screenHeight * SCALE_HEIGHT);
window.setAttributes(dialogParems);
}
3、设置点击弹窗输入框时允许弹出输入法软键盘
mDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
4、设置弹窗是否可以消失(点击取消或确认时)
//canDismiss:如果传参true,则弹窗正常消失,如果传参false,则点击确认或取消按钮后弹窗不消失
private static void setDialogCanDismiss(boolean canDismiss) {
try {
Field field = mDialog.getClass().getSuperclass().getDeclaredField("mShowing");
field.setAccessible(true);
field.set(mDialog, canDismiss);
} catch (Exception e) {
Log.e("tag", Objects.requireNonNull(e.getMessage()));
}
}
//用法如下:
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//点击了确定按钮,此时自己做一些判断,看是否需要弹窗不消失继续显示
if (inNeedShowDialog) {
//设置弹窗不消失
setDialogCanDismiss(false);
} else {
//设置弹窗可以消失
setDialogCanDismiss(true);
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//设置弹窗可以消失
setDialogCanDismiss(true);
mDialog.dismiss();
}
});
5、弹窗返回数据的接口定义,接口名、函数名、参数名、数据类型都可以根据实际需求灵活自定义
interface IDialogResult {
void onGetResult(List<HashMap<String, String>> result);
}
6、弹窗自定义view常用的一些api
int childCount = parentView.getChildCount();//获取父控件中子控件的数量
View childView = parentView.getChildAt(position);//获取指定位置的子控件对象
parentView.removeView(parentView.getChildAt(position));//移除指定位置的子控件对象
parentView.removeView(childView);//移除指定的子控件对象
parentView.addView(childView);//插入子控件
7、其它:EditText控件监听焦点丢失事件以及软键盘点击回车键事件
private void addLostFocusListener(EditText editText, int position) {
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
//TODO
}
}
});
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
editText.clearFocus();
}
return true;
}
});
}