0

当单击按钮加载布局时,我以编程方式显示软键盘。仅当文本字段具有焦点时,我才显示软键盘。它工作正常。但是当我在代码中的另一个位置调用相同的方法(不是单击按钮)时,软键盘不会出现。下面是我的代码。请指出我哪里出错了。

public void showNewView() {
    setContentView(R.layout.activity_main);

    this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    isRegisterScreen = true;
    final EditText text1 = (EditText) findViewById(R.id.label1);
    final EditText text2 = (EditText) findViewById(R.id.label2);

    final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

    text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if(hasFocus){
                inputManager.showSoftInput(labelText, InputMethodManager.SHOW_IMPLICIT);
            }else{
                inputManager.hideSoftInputFromWindow(text1.getWindowToken(), 0);
            }
        }
    });

    text2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if(hasFocus){
                inputManager.showSoftInput(text2, InputMethodManager.SHOW_IMPLICIT);
            }else{
                inputManager.hideSoftInputFromWindow(phoneText.getWindowToken(), 0);
            }
        }
    });

    text1.requestFocus();

}

4

0 回答 0