当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Not able to hide Soft Keyboard for EditTextPreference

问题描述:

I have an EditText in a custom xml layout which get loaded dynamically(setView) in an EditTextPreference. Everything works well. Now when the preference is clicked and the editPreference dialog shows up, so does the soft keyboard. I dont want the soft keyboard to show up by default!

This is what I have tried. Should have worked :(!

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

}

}

网友答案:

Do this for your EditText to hide Soft-Keyboard

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

i think more better way to do this is below Code :

    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;
        }
    });
网友答案:

generic function that may help;

 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());
        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: