我的应用中有一个EditText
,我希望它有两行,显示ime按钮而不是输入键,并将太长的文本移动到下一行(如在短信应用中)。现在我有这样的事情:
<AutoCompleteTextView
android:id="@+id/name_field"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@null"
android:freezesText="true"
android:hint="@string/some_hint"
android:imeOptions="actionNext"
android:maxLength="100"
android:nextFocusDown="@null"
android:lines="2"
android:ellipsize="end"
android:inputType="textImeMultiLine"
android:selectAllOnFocus="true"
android:textColor="@android:color/black"
android:textSize="16sp" />
我提到了两个第一个属性,但我不记得任何允许我达到第三个属性的选项。
例如:如果EditText
中的一行有10个字符,我想显示文本“abc abcd abc abcdefghijk”,如下所示:
abc abcd
abc abc...
编辑:
问题出现在android:inputType="textImeMultiLine"
。当我将其更改为android:inputType="textMultiLine"
时,一切正常,但是......我输入按钮而不是IME按钮,我想避免使用。
答案 0 :(得分:0)
在您的xml android:maxLines="2"
而不是android:lines="2"
答案 1 :(得分:0)
试试这个..希望它会起作用!
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:background="@null"
android:freezesText="true"
android:imeOptions="actionNext"
android:maxLength="100"
android:nextFocusDown="@null"
android:lines="2"
android:ellipsize="end"
android:selectAllOnFocus="true"
android:textColor="@android:color/black"
android:textSize="16sp" />
答案 2 :(得分:0)
添加此行
机器人:的inputType =&#34; textMultiLine&#34;
答案 3 :(得分:0)
这些方法都不适合我。 谷歌搜索大约一个小时后,我发现了这段代码:
EditText editText = findViewById(R.id.editNote);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
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)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(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:
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editNote"
android:hint="Start Typing..."
android:inputType="textMultiLine"
android:gravity="start" />
我不记得我从哪里获得了这段代码,所以不能相信作者。 我根据需要进行了必要的更改。