Android Hide Soft Keyboard from EditText while not losing cursor

前端 未结 14 1905
日久生厌
日久生厌 2020-12-03 04:55

I\'ve come about as far as this which gets me halfway there, but not quite. I have a dialer Fragment that has all the usual Buttons to enter a numb

相关标签:
14条回答
  • 2020-12-03 05:22

    You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks or touch into an EditText Field. I tried lots of methods and codes from stackoverflow but didnt work any but this Works Perfectly for me!! Try this.. :)`

            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    0 讨论(0)
  • 2020-12-03 05:24

    This worked for me:

            // Update the EditText so it won't popup Android's own keyboard, since I have my own.
        EditText editText = (EditText)findViewById(R.id.edit_mine);
        editText.setOnTouchListener(new OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }                
                return true;
            }
        });
    
    0 讨论(0)
  • 2020-12-03 05:24

    This is what I did. First, in manifest inside activity

    android:windowSoftInputMode="stateAlwaysHidden|adjustNothing"
    

    Second, in onCreate if inside activity or onActivityCreated if inside fragment

    editText.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                hideSoftKeyboard(v);
            }
        });
    

    Do not forget to request focus to the editText

    editText.requestFocus();
    

    Then add the hideSoftKeyboard(v) method same as the other answer.

    private void hideSoftKeyboard(View v){
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    

    The key here is to requestFocus before clicking the EditText. If without focus, first click will make the keyboard show up(my experience). However, this is applied if you have a single EditText in an activity. With this, you still can type with custom keyboard(if any), can copy and paste, and cursor is still visible.

    0 讨论(0)
  • 2020-12-03 05:24

    EditText editText = (EditText)findViewById(R.id.edit_mine); editText.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }                
            return true;
        }
    });
    

    ha... this is the correct way of doing...this job done... this gonna work !

    0 讨论(0)
  • 2020-12-03 05:29

    I have finally found a (for me) working solution to this.

    First part (in onCreate):

    // Set to TYPE_NULL on all Android API versions
    mText.setInputType(InputType.TYPE_NULL);
    // for later than GB only
    if (android.os.Build.VERSION.SDK_INT >= 11) {
        // this fakes the TextView (which actually handles cursor drawing)
        // into drawing the cursor even though you've disabled soft input
        // with TYPE_NULL
        mText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    }
    

    In addition, android:textIsSelectable needs to be set to true (or set in onCreate) and the EditText must not be focused on initialization. If your EditText is the first focusable View (which it was in my case), you can work around this by putting this just above it:

    <LinearLayout
      android:layout_width="0px"
      android:layout_height="0px"
      android:focusable="true"
      android:focusableInTouchMode="true" >
        <requestFocus />
    </LinearLayout>
    

    You can see the results of this in the Grapher application, free and available in Google Play.

    0 讨论(0)
  • 2020-12-03 05:30

    use

    android:windowSoftInputMode="stateHidden" 
    

    in your manifest file instead of android:windowSoftInputMode="stateAlwaysHidden"

    0 讨论(0)
提交回复
热议问题