【问题标题】:Not able to hide Soft Keyboard for EditTextPreference无法隐藏 EditTextPreference 的软键盘
【发布时间】:2023-04-03 07:05:01
【问题描述】:

我在自定义 xml 布局中有一个 EditText,它在 EditTextPreference 中动态加载(setView)。一切正常。现在,当单击首选项并显示 editPreference 对话框时,软键盘也会显示。我不希望软键盘默认显示!

这是我尝试过的。应该工作:(!

public class ReportBugPreference extends EditTextPreference {


        @Override
        protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
            super.onPrepareDialogBuilder(builder);  

            View viewBugReport = LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug,null);
            builder.setView(viewBugReport);

            EditText edttxtBugDesc = (EditText) viewBugReport.findViewById(R.id.bug_description_edittext);
            //edttxtBugDesc.clearFocus();

            InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(edttxtBugDesc.getApplicationWindowToken(), 0);

        }

}

【问题讨论】:

  • 你的意思是当用户点击 EditText 时软键盘会显示 ?am i right?
  • 不! EditText 默认有焦点,因此软键盘会自动显示
  • 所以你不希望软键盘默认打开。
  • 您没有在 XML 文件中尝试android:focusable 属性和android:focusableInTouchMode 吗?如果我正确理解了这个问题,我认为您可以通过 XML 文件而不是硬编码来实现您想要的。

标签: android android-edittext android-softkeyboard preferenceactivity


【解决方案1】:

为您的 EditText 执行此操作以隐藏软键盘

       mEditText.requestFocus();
       mEditText.postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager keyboard = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
                    keyboard.hideSoftInputFromWindow(ettext.
                                                       getWindowToken(), 0);
                }
            },200);

我认为更好的方法是在代码下面:

    mEditText.setInputType(InputType.TYPE_NULL);
    mEditText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
             mEditText.setInputType(InputType.TYPE_CLASS_TEXT);
            return false;
        }
    });

【讨论】:

  • 有效,但解决方法是糟糕的用户体验。键盘出现然后隐藏起来。看起来不整齐!尽管如此+1。谢谢!
【解决方案2】:

可能有帮助的通用函数;

 public static void hideSoftKeyboard (Context context, View view) {
        try {
            InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
        }
        catch (Exception ex) {
            Log.w(TAG, "hideSoftKeyboard->"+ex.toString());
        }
    }

【讨论】:

    猜你喜欢
    • 2011-02-17
    • 2016-05-04
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2012-05-18
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多