| | |
| | | package com.hdl.photovoltaic.widget; |
| | | |
| | | import android.app.Activity; |
| | | import android.app.Dialog; |
| | | import android.content.Context; |
| | | import android.os.Bundle; |
| | |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.appcompat.content.res.AppCompatResources; |
| | | import androidx.constraintlayout.widget.ConstraintSet; |
| | | |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.databinding.LoadingConfirmInputBinding; |
| | | import com.hdl.photovoltaic.base.BaseDialog; |
| | | import com.hdl.photovoltaic.databinding.DialogConfirmInputBinding; |
| | | import com.hdl.photovoltaic.utils.KeyboardStateObserverUtils; |
| | | |
| | | /** |
| | | * 输入确认框 |
| | | */ |
| | | public class ConfirmationInputDialog extends Dialog { |
| | | public class ConfirmationInputDialog extends BaseDialog { |
| | | |
| | | public ConfirmationInputDialog(@NonNull Context context) { |
| | | super(context, R.style.Custom_AlertDialog); |
| | | super(context, R.style.Custom_Dialog); |
| | | this.mContext = context; |
| | | } |
| | | |
| | | private final Context mContext; |
| | | private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器 |
| | | private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器 |
| | | private LoadingConfirmInputBinding viewBinding; |
| | | private String titleStr, yesStr, noStr; |
| | | |
| | | private DialogConfirmInputBinding viewBinding; |
| | | private String titleStr, yesStr, noStr,edit_content; |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | viewBinding = LoadingConfirmInputBinding.inflate(getLayoutInflater()); |
| | | viewBinding = DialogConfirmInputBinding.inflate(getLayoutInflater()); |
| | | setContentView(viewBinding.getRoot()); |
| | | // setCancelable(true);//系统后退可以取消 |
| | | //空白处不能取消动画 |
| | |
| | | initData(); |
| | | //初始化界面控件的事件 |
| | | initEvent(); |
| | | //在界面中使用 |
| | | KeyboardStateObserverUtils.getKeyboardStateObserver((Activity) mContext).setKeyboardVisibilityListener(new KeyboardStateObserverUtils.OnKeyboardVisibilityListener() { |
| | | @Override |
| | | public void onKeyboardShow(int h) { |
| | | ConstraintSet cs = new ConstraintSet(); |
| | | cs.clone(viewBinding.loadingParentCl); |
| | | // (viewBinding.loadingConfirmationRl.getTop()-dip2px(h)) |
| | | |
| | | cs.connect(R.id.loading_confirmation_rl, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, px2dip(h));// |
| | | //应用约束 |
| | | cs.applyTo(viewBinding.loadingParentCl); |
| | | |
| | | |
| | | // FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) viewBinding.loadingParentCl.getLayoutParams(); |
| | | // lp.bottomMargin = px2dip(h); |
| | | // viewBinding.loadingParentCl.setLayoutParams(lp); |
| | | |
| | | |
| | | } |
| | | |
| | | @Override |
| | | |
| | | public void onKeyboardHide(int h) { |
| | | ConstraintSet cs = new ConstraintSet(); |
| | | cs.clone(viewBinding.loadingParentCl); |
| | | cs.connect(R.id.loading_confirmation_rl, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, px2dip(h));// |
| | | //应用约束 |
| | | cs.applyTo(viewBinding.loadingParentCl); |
| | | } |
| | | |
| | | }); |
| | | |
| | | |
| | | } |
| | |
| | | return; |
| | | } |
| | | titleStr = title; |
| | | if (viewBinding != null) { |
| | | viewBinding.loadingConfirmationTitleTv.setText(titleStr); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 输入框内容 |
| | | * |
| | | * @param edit_content 输入框内容 |
| | | */ |
| | | public void setEditContent(String edit_content) { |
| | | if (TextUtils.isEmpty(edit_content)) { |
| | | return; |
| | | } |
| | | this.edit_content = edit_content; |
| | | if (viewBinding != null) { |
| | | viewBinding.loadingConfirmationInputEt.setText(edit_content); |
| | | } |
| | | |
| | | } |
| | | |
| | |
| | | return; |
| | | } |
| | | yesStr = confirm; |
| | | |
| | | if (viewBinding != null) { |
| | | viewBinding.dialogConfirmTv.setText(yesStr); |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | } |
| | | noStr = cancel; |
| | | |
| | | if (viewBinding != null) { |
| | | viewBinding.dialogCancelTv.setText(noStr); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | }); |
| | | |
| | | |
| | | viewBinding.loadingConfirmationClickTv.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | v.setSelected(!v.isSelected()); |
| | | if (v.isSelected()) { |
| | | viewBinding.loadingConfirmationInputEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); |
| | | viewBinding.loadingConfirmationHideIv.setImageDrawable(AppCompatResources.getDrawable(mContext, R.drawable.show)); |
| | | } else { |
| | | viewBinding.loadingConfirmationInputEt.setTransformationMethod(PasswordTransformationMethod.getInstance()); |
| | | viewBinding.loadingConfirmationHideIv.setImageDrawable(AppCompatResources.getDrawable(mContext, R.drawable.hide)); |
| | | } |
| | | //设置光标位置 |
| | | viewBinding.loadingConfirmationInputEt.setSelection(viewBinding.loadingConfirmationInputEt.getText().length()); |
| | | |
| | | //一键清空文本 |
| | | viewBinding.loadingConfirmationInputEt.setText(""); |
| | | } |
| | | }); |
| | | } |
| | |
| | | * 初始化界面控件的显示数据 |
| | | */ |
| | | private void initData() { |
| | | if (viewBinding == null) { |
| | | return; |
| | | } |
| | | //如果用户自定了title和message |
| | | if (!TextUtils.isEmpty(titleStr)) { |
| | | viewBinding.loadingConfirmationTitleTv.setText(titleStr); |
| | |
| | | } |
| | | if (!TextUtils.isEmpty(noStr)) { |
| | | viewBinding.dialogCancelTv.setText(noStr); |
| | | } |
| | | if (!TextUtils.isEmpty(edit_content)) { |
| | | viewBinding.loadingConfirmationInputEt.setText(edit_content); |
| | | } |
| | | } |
| | | |
| | |
| | | void Confirm(String str); |
| | | } |
| | | |
| | | /** |
| | | * 根据手机的分辨率从 dp 的单位 转成为 px(像素) |
| | | */ |
| | | private int dip2px(float dpValue) { |
| | | if (mContext == null) { |
| | | return 0; |
| | | } |
| | | final float scale = mContext.getResources().getDisplayMetrics().density; |
| | | return (int) (dpValue * scale + 0.5f); |
| | | } |
| | | |
| | | /** |
| | | * 根据手机的分辨率从 px(像素) 的单位 转成为 dp |
| | | */ |
| | | private int px2dip(float pxValue) { |
| | | final float scale = mContext.getResources().getDisplayMetrics().density; |
| | | return (int) (pxValue / scale + 0.5f); |
| | | } |
| | | |
| | | } |