2017-04-20 148 views
0

在下面的代碼行的getWindow()方法無法解析方法 'getWindow()'

this.getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)。

導致以下錯誤

無法解析方法 '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失去焦點。

我在活動班,所以我不確定問題出在哪裏。 this有一個getWindow()方法嗎?

+0

裏面的onClick,這指的是OnClickListener。您需要使用MyTopLevelClassName.this。 –

回答

1

你可以關閉這樣

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

     } 
    } 
} 
0

找到解決方案

在活動:

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

在片段:

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

嘗試這種解決方案 - >只需加提前名。

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

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

試試這個:

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