无法为EditTextPreference隐藏软键盘

时间:2012-04-25 10:49:08

标签: android android-edittext android-softkeyboard preferenceactivity

我在自定义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);

        }

}

2 个答案:

答案 0 :(得分: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 :(得分:0)

可能有帮助的通用功能;

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