問題描述
有沒有辦法在 Android 2.3 上顯示多行 EditText
并使用 IME 操作標(biāo)簽完成"?
Is there a way to have a Multi-Line EditText
present and use the IME Action Label "Done" on Android 2.3?
在 Android 2.2 中這不是問題,輸入按鈕顯示 IME 操作標(biāo)簽完成"(android:imeActionLabel="actionDone"
),并在單擊時(shí)關(guān)閉軟輸入.
In Android 2.2 this is not a problem, the enter button shows the IME Action Label "Done" (android:imeActionLabel="actionDone"
), and dismisses Soft Input when clicked.
在為多行配置 EditText
時(shí),Android 2.3 移除了為軟輸入鍵盤顯示完成"操作的功能.
When configuring an EditText
for multi-line, Android 2.3 removes the ability to show the "Done" action for the Soft Input keyboard.
我已經(jīng)設(shè)法通過使用 KeyListener
來改變軟輸入回車按鈕的行為,但是回車按鈕看起來仍然像回車鍵.
I have managed to alter the behaviour of the Soft Input enter button by using a KeyListener
, however the enter button still looks like an enter key.
這里是EditText
<EditText
android:id="@+id/Comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:lines="3"
android:maxLines="3"
android:minLines="3"
android:maxLength="60"
android:scrollHorizontally="false"
android:hint="hint"
android:gravity="top|left"
android:textColor="#888"
android:textSize="14dp"
/>
<!-- android:inputType="text" will kill the multiline on 2.3! -->
<!-- android:imeOptions="actionDone" switches to a "t9" like soft input -->
當(dāng)我在活動(dòng)中加載設(shè)置內(nèi)容視圖后檢查 inputType
值時(shí),它顯示為:
When I check the inputType
value after loading setting the content view in the activity, it shows up as:
inputType = 0x20001
這是:
- class =
TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_NORMAL
- 標(biāo)志 =
InputType.TYPE_TEXT_FLAG_MULTI_LINE
推薦答案
嗯,重新閱讀了 TextView
和 EditorInfo
文檔后,已經(jīng)清楚平臺(tái)將強(qiáng)制 IME_FLAG_NO_ENTER_ACTION
進(jìn)行多行文本視圖.
Well, after re-reading the TextView
and EditorInfo
docs, it has become clear that the platform is going to force IME_FLAG_NO_ENTER_ACTION
for multi-line text views.
請注意,TextView
會(huì)自動(dòng)在多行上為您設(shè)置此標(biāo)志文本視圖.
Note that
TextView
will automatically set this flag for you on multi-line text views.
我的解決方案是繼承 EditText
并在讓平臺(tái)配置它們后調(diào)整 IME 選項(xiàng):
My solution is to subclass EditText
and adjust the IME options after letting the platform configure them:
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
}
if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}
在上面,我也強(qiáng)制 IME_ACTION_DONE
,即使這可以通過繁瑣的布局配置來實(shí)現(xiàn).
In the above, I'm forcing IME_ACTION_DONE
too, even though that can be achieved through tedious layout configuration.
這篇關(guān)于2.3 上帶有完成 SoftInput 操作標(biāo)簽的多行 EditText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!