2014-07-21 74 views
0

當用戶在鍵盤上按下Enter時,我將焦點從一個AutoCompleteTextView切換到另一個。問題在於,當下一個AutoCompleteTextView獲得焦點時,鍵盤始終隱藏。有什麼辦法可以防止這種情況發生? 下面是我用它來切換焦點代碼:Android - requestFocus開關鍵盤關閉

field1.setOnKeyListener(new View.OnKeyListener() { 
       @Override 
       public boolean onKey(View v, int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_ENTER) { 
         field1.dismissDropDown(); 
         field2.requestFocus(); 

         return true; 
        } 
        return false; 
       } 
      }); 

我沒有用在該聲明FIELD1和FIELD2 XML文件的任何imeOptions。

回答

0

嘗試後requestfocus()

InputMethodManager keyboard = (InputMethodManager) 
       getSystemService(Context.INPUT_METHOD_SERVICE); 
//to show keyboard 
keyboard.showSoftInput(field2, 0); 

//to hide keyboard 
imm.hideSoftInputFromWindow(field2.getWindowToken(), 0); 
1

編程顯示鍵盤是,將焦點切換使鍵盤消失。一個快速的解決將是編程告訴鍵盤與保持可見:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
0

據AutoCompleteTextView的源代碼:)

@Override 
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 
     super.onFocusChanged(focused, direction, previouslyFocusedRect); 
     // Perform validation if the view is losing focus. 
     if (!focused) { 
      performValidation(); 
     } 
     if (!focused && !mPopup.isDropDownAlwaysVisible()) { 
      dismissDropDown(); 
     } 
    } 

我想你可以刪除#field1.dismissDropDown(線 - 也許它會幫助你。

此外,我認爲你應該設置OnEditorActionListener而不是OnKeyListener。我建議你試試上面的代碼:

field1.setOnEditorActionListener(new AutoCompleteTextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if (actionId == EditorInfo.IME_ACTION_ENTER) { 
     field2.requestFocus(); 
     return true; 
     } 
     return false; 
    } 
    }); 
0

強行平倉:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 

要關閉:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);