requestFocus not working

前端 未结 5 1619
悲哀的现实
悲哀的现实 2021-01-02 06:41


        
相关标签:
5条回答
  • 2021-01-02 07:23

    To do this in code, in your Activity:

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    
    0 讨论(0)
  • 2021-01-02 07:26

    I usually use the following to set the focus: Add following attributs to your xml-layout

    <AutoCompleteTextView 
        android:focusable="true" 
        android:focusableInTouchMode="true"> 
    </AutoCompleteTextView>
    

    and set focus programmatically like

    ((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();
    

    f.e. in onResume or onWindowChanged

    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    
        if (hasFocus) {
            ((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();
        }
    }
    
    0 讨论(0)
  • 2021-01-02 07:34

    Solved it! In manifest I added following to the activity:

    android:windowSoftInputMode="stateAlwaysVisible"
    
    0 讨论(0)
  • 2021-01-02 07:34

    None of the above worked for me... this is what I used

    txtView.getParent().requestChildFocus(txtView,txtView);
    
    0 讨论(0)
  • 2021-01-02 07:39

    You can try this in the code if you want to forcefully show the keyboard.

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    

    then you can to use this code to close the keyboard:

    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autocomplete_zone.getWindowToken(), 0);
    
    0 讨论(0)
提交回复
热议问题