Show soft keyboard when Activity starts

前端 未结 10 563
梦毁少年i
梦毁少年i 2020-12-13 08:28

I have 2 activities, A and B. When A starts, it checks for a condition and if true, it calls startActivityForResult() to start B. B only takes text input so it

相关标签:
10条回答
  • 2020-12-13 08:52

    I have got two way.

    Method 1. Use the following code inside the OnCreate method

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

    It will prevent popping up keyboard unless you click.

    or

    Method 2 You can move away the focus on other view like TextView by using "requestfocus" in the xml.

    <TextView
                android:id="@+id/year_birth_day"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="1991">            
               <requestFocus />
               </TextView>
    

    Method 3 ( I think it should be avoidable) Using the following code in the manifest-

    android:windowSoftInputMode="stateVisible"
    
    0 讨论(0)
  • 2020-12-13 08:54

    For me worked only this solutions: add in manifest for that activity:

    android:windowSoftInputMode="stateVisible|adjustPan"
    
    0 讨论(0)
  • 2020-12-13 08:57

    paste this after setContentView

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    
    0 讨论(0)
  • 2020-12-13 08:58

    Try showing the keyboard with some delay. Something similar to this:

    public void onResume() {
        super.onResume();
    
        TimerTask tt = new TimerTask() {
    
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(yourTextBox, InputMethodManager.SHOW_IMPLICIT);
            }
        };
    
        final Timer timer = new Timer();
        timer.schedule(tt, 200);
    }
    
    0 讨论(0)
提交回复
热议问题