问题
I have an AutoCompleteTextView where the user is to write in a single-line search, i.e. no line breaks are allowed. Using android:singleLine="true"
allows this to work properly.
However, I also wish to set a hint to display when no other text has been entered, and this text does require more than one line. My problem is that with Android 4+, the hint is not wrapped to several lines. With Android 2.3, however, the content does get wrapped and multiple lines are displayed. Users are still not able enter more than one line, so this is the way I want it.
Using for example android:maxLines="2"
does not help since that allows the user to insert a line break when searching. I've also tried android:scrollHorizontally="false"
and android:ellipsize="none"
and android:layout_weight=1
without success.
Any ideas on how I can get the hint to wrap multiple lines and still only accept a single line from users with Android 4 as well? Below is the code I'm using at the moment.
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:completionThreshold="3"
android:hint="This is the hint to be displayed. It's rather long and doesn't fit in one line"
android:singleLine="true" />
回答1:
Try changing singleLine to:
android:lines="1"
And now to disable "Enter":
EDIT_TEXT.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER)
{
return true;
}
return false;
}
});
回答2:
Try adding an OnFocusChangeListener to change the value of singleLine
or maxLines
:
autoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange (View v, boolean hasFocus) {
// This might do it on its own
((AutoCompleteTextView) v).setSingleLine(hasFocus);
// If setSingleLine() doesn't work try this
AutoCompleteTextView auto = (AutoCompleteTextView) v;
if(hasFocus)
auto.setMaxLines(1);
else
auto.setMaxLines(2); // or more if necessary
// Only keep the one that works, you obviously don't need both!
}
});
来源:https://stackoverflow.com/questions/12664764/how-to-word-wrap-the-hint-of-a-single-line-autocompletetextview