Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法解析方法';getWindow()和#x27;_Android - Fatal编程技术网

Android 无法解析方法';getWindow()和#x27;

Android 无法解析方法';getWindow()和#x27;,android,Android,下面代码行中的getWindow()方法 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入\状态\始终\隐藏) 导致以下错误 无法解析方法“getWindow()” 错误的代码行位于此方法的底部 private void setButtonListener(Button button){ button.setOnClickListener(new View.OnClickListener() {

下面代码行中的
getWindow()
方法

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入\状态\始终\隐藏)

导致以下错误

无法解析方法“getWindow()”

错误的代码行位于此方法的底部

private void setButtonListener(Button button){
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String textString = editText.getText().toString();
            textView.setText(textString);
            editText.getText().clear();


            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);

            this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }
    });
}
我希望我的
editText
在键盘关闭时失去焦点


我正在上活动课,所以我不确定问题出在哪里。
是否有
getWindow()
方法?

您可以像这样关闭软键盘

private void setButtonListener(Button button){
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String textString = editText.getText().toString();
            textView.setText(textString);
            editText.getText().clear();


            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);

        }
    });
}
如果你想切换

InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

您可以使用OnFocusChangeListener隐藏软键盘并失去焦点

EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);


private class MyFocusChangeListener implements OnFocusChangeListener {

    public void onFocusChange(View v, boolean hasFocus){

        if(v.getId() == R.id.textbox && !hasFocus) {

            InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

        }
    }
}
找到解决办法

在活动中:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
片段:

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

尝试此解决方案->仅之前添加类名称

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

使用
YourActivity.this.getWindow()
代替
this.getWindow()

试试这个:

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

在onClick内部,这指的是onClick侦听器。您需要改用MyTopLevelClassName.this。