How to hide Soft Keyboard when activity starts

2019-01-03 08:26发布

I have an Edittext with android:windowSoftInputMode="stateVisible" in Manifest. Now the keyboard will be shown when I start the activity. How to hide it? I cannot use android:windowSoftInputMode="stateHidden because when keyboard is visible then minimize the app and resume it the keyboard should be visible. I tried with

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

but it did not work.

19条回答
叼着烟拽天下
2楼-- · 2019-01-03 08:43

Add the following text to your xml file.

<!--Dummy layout that gain focus -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:orientation="vertical" >
            </LinearLayout>
查看更多
Evening l夕情丶
3楼-- · 2019-01-03 08:43

Put this code your java file and pass the argument for object on edittext,

private void setHideSoftKeyboard(EditText editText){
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
查看更多
别忘想泡老子
4楼-- · 2019-01-03 08:43

You can set config on AndroidManifest.xml

Example:

<activity
    android:name="Activity"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@*android:style/Theme.NoTitleBar"
    android:launchMode="singleTop"
    android:windowSoftInputMode="stateHidden"/>
查看更多
beautiful°
5楼-- · 2019-01-03 08:43
Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);

Ed_Cat_Search.setInputType(InputType.TYPE_NULL);

Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
        Ed_Cat_Search.onTouchEvent(event); // call native handler
        return true; // consume touch even
    }
});

this one worked for me
查看更多
乱世女痞
6楼-- · 2019-01-03 08:44

Put this in the manifest inside the Activity tag

  android:windowSoftInputMode="stateHidden"  
查看更多
叛逆
7楼-- · 2019-01-03 08:44

This is what I did:

yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup
        yourEditText.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);   // handle the event first
                InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {

                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  // hide the soft keyboard
                    yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext
                }
                return true;
            }
        });
查看更多
登录 后发表回答