用Builder模式重新打造一个dialog,案例中有两种Builder,分别是CommonBuilder和MDBuilder,如果还想实现其他的通用dialog,继承自FRBaseDialogBuilder即可。
private void showCommonDialog() {
final FRDialog dialog = new FRDialog.CommonBuilder(this)
.setContentView(R.layout.dialog_common)
.setText(R.id.dcu_tv_cancel, "否")
.setText(R.id.dcu_tv_confirm, "是")
.setText(R.id.dcu_tv_title, "温馨提示")
.setText(R.id.dcu_tv_content, "1.文字文字我是文字文字文字我是文字文字文字我是文字!\n2.文字文字文字文字文字\n3.文字文字文字文字文字")
.setDefaultAnim()
.show();
dialog.setText(R.id.dcu_tv_confirm, "确定");
dialog.setOnClickListener(R.id.dcu_tv_cancel, new FRDialogClickListener() {
@Override
public boolean onDialogClick(View view) {
Toast.makeText(MainActivity.this, "点击了否", Toast.LENGTH_SHORT).show();
return true;
}
});
dialog.setOnClickListener(R.id.dcu_tv_confirm, new FRDialogClickListener() {
@Override
public boolean onDialogClick(View v) {
Toast.makeText(MainActivity.this, "点击了是", Toast.LENGTH_SHORT).show();
return false;
}
});
}
private void showMDDialog() {
FRDialog dialog = new FRDialog.MDBuilder(this)
.setMessage("1.文字文字我是文字文字文字我是文字文字文字我是文字!\n2.文字文字文字文字文字\n3.文字文字文字文字文字")
.setTitle("温馨提示")
.setNegativeAndPositive("否", "是")
.setPositiveListener(new FRDialogClickListener() {
@Override
public boolean onDialogClick(View view) {
return true;
}
}).show();
}
继承所有dialog的设置,同时还可以自定义以下设置
//设置宽度全屏
dialog.setFullWidth()
//设置从底部弹出
dialog.setFromBottom()
//设置弹出动画
dialog.setAnimation(int anim)
将mWidth改成mWidthOffset,不让用户设置一个具体的宽度,而是让用户去设置一个宽度比例,然后通过改变window的LayoutParams来设置dialog的宽高:
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = (int) (baseBuilder.mContext.getResources().getDisplayMetrics().widthPixels * baseBuilder.mWidthOffset);
lp.height = baseBuilder.mHeight;
window.setAttributes(lp);
用法还是和之前一样:
dialog.setWidthOffset(0——1) 默认是0.9
新增点击dialog中非EditText区域隐藏软键盘
重写dispatchTouchEvent方法进行拦截:
/**
* 点击dialog中除EditText以外的区域隐藏软键盘
*
* @param ev
* @return
*/
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
FRInputMethodManager.autoHideSoftInput(this, ev);
return super.dispatchTouchEvent(ev);
}
//核心方法
public static boolean isAutoHideSoftInput(View view, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN) {
return false;
}
if (!(view instanceof EditText)) {
return false;
}
float x = event.getX();
float y = event.getY();
int[] location = {0, 0};
view.getLocationInWindow(location);
int left = location[0];
int top = location[1];
int bottom = top + view.getHeight();
int right = left + view.getWidth();
if (left <= x && x < right && top <= y && y < bottom) {
// 点击事件在EditText的区域里
return false;
}
return true;
}
用法不变。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有