Android使用Dialog实现贴键盘输入框(类似微博评论)
16lz
2021-01-23
效果图
实现输入框跟随键盘移动需要在清单文件中给activity设置一个属性
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
创建类继承Dialog
输入布局文件自己定义即可
public class InputDialogUtils extends Dialog { private Context mContext;//上下文对象 private EditText editText;//输入框对象 private InputDialogListener listener;//监听回调 public InputDialogUtils(@NonNull Context context) { super(context); this.mContext = context; initDialog(); } public InputDialogUtils(@NonNull Context context, int themeResId) { super(context, themeResId); this.mContext = context; initDialog(); } protected InputDialogUtils(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.mContext = context; initDialog(); } private void initDialog() { View view = LinearLayout.inflate(mContext, R.layout.pop_view_session_input, null); setContentView(view); //输入框 editText = view.findViewById(R.id.sessionInputEd); Window window = getWindow(); window.setGravity(Gravity.BOTTOM); //这里的动画效果自己创建 window.setWindowAnimations(R.style.BottomDialog_Animation); //添加发送点击事件 view.findViewById(R.id.sessionSendTxt).setOnClickListener(new ClickUtils.OnDebouncingClickListener(Config.IS_GLOBAL_CLICK, Config.MIN_CLICK_DELAY_TIME) { @Override public void onDebouncingClick(View v) { if (listener != null) { listener.getInputTxt(editText); editText.setText(""); } } }); //添加放大点击事件 view.findViewById(R.id.sessionInputBigImg).setOnClickListener(new ClickUtils.OnDebouncingClickListener(Config.IS_GLOBAL_CLICK, Config.MIN_CLICK_DELAY_TIME) { @Override public void onDebouncingClick(View v) { listener.onCancel(editText); } }); setCancelable(true); show();//保证自定义得布局文件充满屏幕宽度 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.width = (int) (ScreenUtils.getScreenWidth()); //设置宽度 getWindow().setAttributes(lp); getWindow().setAttributes(layoutParams); } }
接下来重写隐藏显示方法,进行逻辑与回显示键盘操作
@Override public void dismiss() { KeyboardUtils.hideSoftInput(editText); //与键盘解绑 editText.setFocusable(false); editText.setShowSoftInputOnFocus(false); editText.setFocusableInTouchMode(false); editText.requestFocus(); super.dismiss(); } @Override public void show() { //显示键盘 KeyboardUtils.showSoftInput(); //与键盘绑定 editText.setFocusable(true); editText.setShowSoftInputOnFocus(true); editText.setFocusableInTouchMode(true); editText.requestFocus(); super.show(); }
键盘回收方法
public static void hideSoftInput(@NonNull final View view) { InputMethodManager imm = (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) return; imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }
键盘显示方法
public static void showSoftInput() { InputMethodManager imm = (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) { return; } imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); }
style里面的代码
<style name="BottomDialog2" parent="@android:style/Theme.Holo.Dialog"> - "android:windowFrame"
>@null - "android:windowIsFloating">true
- "android:windowIsTranslucent">true
- "android:windowNoTitle">true
- "android:windowBackground">@color/colorTransparent
- "android:backgroundDimEnabled">true
style>
Activity使用方法
if (inputDialogUtils == null) { inputDialogUtils = new InputDialogUtils(this, R.style.BottomDialog2); } else { inputDialogUtils.show(); } inputDialogUtils.setListener(new InputDialogUtils.InputDialogListener() { @Override public void onConfirm(int type) { } @Override public void onCancel(EditText editText) { inputDialogUtils.cancel(); } @Override public void getInputTxt(EditText editText) { ToastUtils.showShort(editText.getText().toString()); inputDialogUtils.dismiss(); } });
以上为开发时的需求,如有问题,欢迎各位大佬指正。
开发与学习并进。
更多相关文章
- android软键盘的使用
- Android监听软键盘的方式
- Android启动模拟器出现:Failed to allocate memory: 8的解决方法
- ReactNative 在用react-navigation组件时,没有 navigator.getCurr
- Android Canvas drawArc方法介绍
- Android软键盘弹出时把布局顶上去,控件乱套解决方法
- Android运行main方法后java虚拟机停止运行