2012-09-13 62 views
1

我正在嘗試使用多行編輯文本上的下一個按鈕查看鍵盤。但它不適合我。使用IMEOption多行EditText DONE或NEXT不能正常工作

+1

它是如何「不工作」?如果按Next,它會做什麼?和一些代碼請? – josephus

+0

[在2.3上使用完成的SoftInput操作標籤的多行EditText]的可能重複(http://stackoverflow.com/questions/5014219/multiline-edittext-with-done-softinput-action-label-on-2-3) –

回答

2

由於用於下一個/完成/搜索/ ...操作的鍵盤按鈕必須是用於插入行的ENTER/RETURN按鈕,因此無法在多行編輯文本上設置IME操作打破。

+1

不是真正。谷歌保持應用程序做到這一點。 – Michael

+0

任何人都知道這個呢?我遇到了這個問題。我有一個多行字段,我需要一個actionDone按鈕。 – tree

0

不要害怕,它可以做到。儘管下面的代碼完美地工作,但不幸的是我不記得我從哪裏得到完整的代碼,所以不能給予作者應有的功勞。

////////////Code to Hide SoftKeyboard on Enter (DONE) Press/////////////// 
editText.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); 
editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE);    //Set Return Carriage as "DONE" 
editText.setImeOptions(EditorInfo.IME_ACTION_DONE); 

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    { 
       if (event == null) { 
        if (actionId == EditorInfo.IME_ACTION_DONE) { 
         // Capture soft enters in a singleLine EditText that is the last EditText 
         // This one is useful for the new list case, when there are no existing ListItems 
         editText.clearFocus(); 
         //hide SoftKeyboard 
         InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE); 
         inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0); 
        } 

        else if (actionId == EditorInfo.IME_ACTION_NEXT) { 
         // Capture soft enters in other singleLine EditTexts 
        } else if (actionId == EditorInfo.IME_ACTION_GO) { 
        } else { 
         // Let the system handle all other null KeyEvents 
         return false; 
        } 
       } 
     else if (actionId == EditorInfo.IME_NULL) { 
        // Capture most soft enters in multi-line EditTexts and all hard enters; 
        // They supply a zero actionId and a valid keyEvent rather than 
        // a non-zero actionId and a null event like the previous cases. 
        if (event.getAction() == KeyEvent.ACTION_DOWN) { 
         // We capture the event when the key is first pressed. 
        } else { 
         // We consume the event when the key is released. 
         return true; 
        } 
       } 
     else { 
        // We let the system handle it when the listener is triggered by something that 
        // wasn't an enter. 
        return false; 
       } 
       return true; 
     } 
}); 

用於傳遞聚焦到接着的EditText使用edit_text2.requestFocus()並刪除隱藏SoftKeyboard代碼。