AndroidはDialogを使用してキーボード入力ボックス(微博コメントのような)を実装

25880 ワード

効果図
入力ボックスがキーボードに従って移動するには、リストファイルに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 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();
                        }
                    });

以上は開発時のニーズであり、問題があれば、皆さんのご指摘を歓迎します.開発と学習を並行する.