软键盘仅在按钮点击时显示在调用方法上

时间:2013-10-17 12:21:53

标签: android-softkeyboard android-button

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

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();

}

0 个答案:

没有答案