How to stop Soft keyboard showing automatically when focus is changed (OnStart event)

前端 未结 4 1418
有刺的猬
有刺的猬 2020-12-11 00:11

I am developing for a tablet using Android 2.2.

I have a form which I am using for new and editable instances. When editable, I want to stop the user from editing c

相关标签:
4条回答
  • 2020-12-11 00:28

    We can Hide the device Keybord by following ways which totally depends on the need of situation_

    First Way_

    EditText userId;
        /*
         * When you want that Keybord not shown untill user clicks on one of the EditText Field.
         */
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    

    Second Way_ InputMethodManager

        /*
         * When you want to use service of 'InputMethodManager' to hide/show keyboard
         */
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    
        inputManager.hideSoftInputFromWindow(userId.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    

    Third Way_

        /*
         * Touch event Listener
         * When you not want to open Device Keybord even when user clicks on concern EditText Field.
         */
        userId.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int inType = userId.getInputType(); // backup the input type
                userId.setInputType(InputType.TYPE_NULL); // disable soft input
                userId.onTouchEvent(event); // call native handler
                userId.setInputType(inType); // restore input type
                userId.setFocusable(true);
                return true; // consume touch even
            }
        });
    

    Behalf of all of the above ways_ you can also define windowSoftInputMode in applicationsmanifest` file_

    <manifest ... >
     <application ... >
        <activity 
            android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
            ... >
            <intent-filter>
               ...
            </intent-filter>
        </activity>
    
     </application>
    </manifest>
    

    I hope this will help all!

    0 讨论(0)
  • 2020-12-11 00:44

    If you want this for a particular EditText this will work.

    editText.setShowSoftInputOnFocus(false);

    0 讨论(0)
  • 2020-12-11 00:48

    Maybe this will help you:

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    

    Put this method in the onCreate() method of your activity when the activity is first created or called.

    0 讨论(0)
  • 2020-12-11 00:51

    this is the answer: In your AndroidManifest.xml add an attribute to the Activity:

            <activity android:name=".MyActivity" android:windowSoftInputMode="adjustPan"> </activity>
    
    0 讨论(0)
提交回复
热议问题