2012-08-17 259 views
4

如何在同一時間設置這些選項:如何在Android上爲EditText設置MultiLine和imeOptions =「actionDone」?

  • android:minLines="3"
  • android:inputType="textMultiLine"
  • android:imeOptions="actionDone"

它,只要我把android:inputType="textMultiLine"看來,鍵盤自動替換鍵OK由密鑰輸入。有誰知道是否有可能擁有兩把鑰匙?

注意:這answer是不是我所期待的。我想要兩把鑰匙。

+0

對我來說,我能夠編譯我自己的IME,但選項沒有通過,但它決定放在那裏。 – 2014-09-11 05:20:24

回答

0

唯一保證的是Android會將inputTypeimeOptions傳遞給IME。 IME與他們做什麼取決於實施。在某些IME 可能會決定在多線模式下有足夠的屏幕空間顯示兩個鍵時,不應該依賴該行爲。

2

嗨,我也面臨同樣的問題,最後我得到了解決方案。

變化

android:inputType="textMultiLine" 

android:inputType="text" 

的java文件訪問的EditText內部使用的ID

editText.setHorizontallyScrolling(false); 

editText.setMaxLines(3); 

而現在實現了電子的OnEditorAction ditText。

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if (actionId == 4) { //actionId 4 for actionDone And 6 for actionSend 


       //perform action what you want 

       return true; 
      } else 
       return false; 
     } 
    }); 
0

這裏寫的答案類似的問題:https://stackoverflow.com/a/42236407/7550472,它原來是救命之恩,我是沒有別的工作。爲了更容易訪問,我也會將代碼粘貼到像我這樣懶惰的人;)。

在你的Java代碼:

////////////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(); 
         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; 
     } 
}); 

minLines在XML定義將保持不變,而其他兩個屬性不要求其在Java代碼來處理。

0

如果您創建EditText的子類並插入此函數,它應該可以解決您的問題。

這個問題在https://stackoverflow.com/a/5037488/7403656回答,但我做了一點改動,它從xml中獲取imeOption,而不是將其設置爲完成選項。

@Override 
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 
    InputConnection connection = super.onCreateInputConnection(outAttrs); 
    int imeOptions = getImeOptions(); 
    int imeActions = outAttrs.imeOptions & EditorInfo.IME_MASK_ACTION; 
    if ((imeActions & imeOptions) != 0) { 
     // clear the existing action 
     outAttrs.imeOptions ^= imeActions; 
     // set the DONE action 
     outAttrs.imeOptions |= imeOptions; 
    } 
    if ((outAttrs.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) { 
     outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION; 
    } 
    return connection; 
} 
相關問題