How can I set the focus (and display the keyboard) on my EditText programmatically

后端 未结 13 672
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 02:22

I have a layout which contains some views like this:






&         


        
相关标签:
13条回答
  • 2020-11-28 02:33
    editTxt.setOnFocusChangeListener { v, hasFocus ->
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                if (hasFocus) {
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
                } else {
                    imm.hideSoftInputFromWindow(v.windowToken, 0)
                }
            }
    
    0 讨论(0)
  • 2020-11-28 02:35

    Here is KeyboardHelper Class for hiding and showing keyboard

    import android.content.Context;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.EditText;
    
    /**
     * Created by khanhamza on 06-Mar-17.
     */
    
    public class KeyboardHelper {
    public static void hideSoftKeyboard(final Context context, final View view) {
        if (context == null) {
            return;
        }
        view.requestFocus();
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    assert imm != null;
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    }, 1000);
    }
    
    public static void hideSoftKeyboard(final Context context, final EditText editText) {
        editText.requestFocus();
        editText.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    assert imm != null;
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
    }, 1000);
    }
    
    
    public static void openSoftKeyboard(final Context context, final EditText editText) {
        editText.requestFocus();
        editText.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    assert imm != null;
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
    }, 1000);
    }
    }
    
    0 讨论(0)
  • 2020-11-28 02:39
    final EditText tb = new EditText(this);
    tb.requestFocus();
    tb.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(tb, InputMethodManager.SHOW_IMPLICIT);
        }
    }, 1000);
    
    0 讨论(0)
  • 2020-11-28 02:50

    Try this:

    EditText editText = (EditText) findViewById(R.id.myTextViewId);
    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    

    http://developer.android.com/reference/android/view/View.html#requestFocus()

    0 讨论(0)
  • 2020-11-28 02:50

    use:

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
    0 讨论(0)
  • 2020-11-28 02:54

    I recommend using a LifecycleObserver which is part of the Handling Lifecycles with Lifecycle-Aware Components of Android Jetpack.

    I want to open and close the Keyboard when the Fragment/Activity appears. Firstly, define two extension functions for the EditText. You can put them anywhere in your project:

    fun EditText.showKeyboard() {
        requestFocus()
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
    }
    
    fun EditText.hideKeyboard() {
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(this.windowToken, 0)
    }
    

    Then define a LifecycleObserver which opens and closes the keyboard when the Activity/Fragment reaches onResume() or onPause:

    class EditTextKeyboardLifecycleObserver(private val editText: WeakReference<EditText>) :
        LifecycleObserver {
    
        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        fun openKeyboard() {
            editText.get()?.postDelayed({ editText.get()?.showKeyboard() }, 100)
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        fun closeKeyboard() {
            editText.get()?.hideKeyboard()
        }
    }
    

    Then add the following line to any of your Fragments/Activities, you can reuse the LifecycleObserver any times. E.g. for a Fragment:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
        // inflate the Fragment layout
    
        lifecycle.addObserver(EditTextKeyboardLifecycleObserver(WeakReference(myEditText)))
    
        // do other stuff and return the view
    
    }
    
    0 讨论(0)
提交回复
热议问题