Android使用Dialog实现贴键盘输入框(类似微博评论)(转载)

实现输入框跟随键盘移动需要在清单文件中给activity设置一个属性

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

1
创建类继承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);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
接下来重写隐藏显示方法,进行逻辑与回显示键盘操作

@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();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
键盘回收方法

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);
}
1
2
3
4
5
6
键盘显示方法

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);
}
1
2
3
4
5
6
7
style里面的代码

<style name="BottomDialog2" parent="@android:style/Theme.Holo.Dialog">
    <item name="android:windowFrame">@null</item><!--边框-->
    <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
    <item name="android:windowIsTranslucent">true</item><!--半透明-->
    <item name="android:windowNoTitle">true</item><!--无标题-->
    <item name="android:windowBackground">@color/colorTransparent</item><!--背景透明-->
    <item name="android:backgroundDimEnabled">true</item><!--模糊-->
</style>

1
2
3
4
5
6
7
8
9
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();
                    }
                });

原文链接:https://blog.csdn.net/qq_44946212/article/details/107881874?utm_medium=distribute.pc_feed.none-task-blog-cf-2.nonecase&depth_1-utm_source=distribute.pc_feed.none-task-blog-cf-2.nonecase&request_id=5f4392d4cea070620e93ea45

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值