2016-10-23 51 views
0

我正在做一個計算項目。所以,我有我的EditText箱的工作,我想隱藏softkeyboard如果用戶點擊完成(或任何其他類型的按鈕,GO,NEXT等)和EditText上框爲空Android - 如何隱藏軟鍵盤,如果在按鍵時EditText爲空?

像這樣:

的EditText爲空 - >用戶單擊按鈕 - > softkeyboard隱藏

我有這樣的一段代碼,我管理使用教程和指南在互聯網上寫

我不知道它是管理聽衆的按鈕在softkeyboard

TextView.OnEditorActionListener mEditor = new TextView.OnEditorActionListener() 
{ 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    { 
     if (actionId == EditorInfo.IME_ACTION_DONE) 
     { 
      //Calculations method 
     } 

     return false; 
    } 
}; 

所以,我的問題是:當EditText爲空並且沒有得到錯誤時,如何管理偵聽器?

回答

1

可以使用,例如:

TextView.OnEditorActionListener mEditor = new TextView.OnEditorActionListener() 
{ 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    { 
     if (actionId == EditorInfo.IME_ACTION_DONE) 
     { 
      if (!TextUtils.isEmpty(v.getText().toString())){ 
       // you calculations method 
      } else { 
       InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
       Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 
       InputMethodManager.HIDE_NOT_ALWAYS); 
      } 
     } 

     return false; 
    } 
};