繁体   English   中英

无法为EditTextPreference隐藏软键盘

[英]Not able to hide Soft Keyboard for EditTextPreference

我有一个自定义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执行此操作以隐藏软键盘

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

可能有帮助的通用功能;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM