可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
possible duplicate : android-singleline-true-not-working-for-edittext
<EditText
android:id="@+id/searchbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:drawablePadding="10dp"
android:background="@drawable/edittext"
android:drawableLeft="@drawable/folder_full"
android:drawableRight="@drawable/search"
android:paddingLeft="15dp"
android:hint="search...">
</EditText>
I want to make the above EditText
to have only single line. Even if the user presses "enter" the cursor should not get down to the second line. Can anybody help me doing that?
回答1:
Use android:maxLines="1"
and android:inputType="text"
You forgot the android:maxLines attribute. And refer for android:inputType With your example, below will give this result:
<EditText
android:id="@+id/searchbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:drawablePadding="10dp"
android:background="@drawable/edittext"
android:drawableLeft="@drawable/folder_full"
android:drawableRight="@drawable/search"
android:paddingLeft="15dp"
android:hint="search...">
</EditText>
回答2:
Using android:singleLine="true"
is deprecated.
Just add your input type and set maxline to 1 and everything will work fine
android:inputType="text"
android:maxLines="1"
回答3:
android:singleLine
is now deprecated. From the documentation:
This constant was deprecated in API level 3.
This attribute is deprecated. Use maxLines
instead to change the layout of a static text, and use the textMultiLine
flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).
So you could just set android:inputType="textEmailSubject"
or any other value that matches the content of the field.
回答4:
You must add this line in your EditText in xml:
android:maxLines="1"
回答5:
android:maxLines="1"
android:inputType="text"
Add the above code to have a single line in EditText tag in your layout.
android:singleLine="true" is deprecated
This constant was deprecated in API level 3.
This attribute is deprecated. Use maxLines instead to change the
layout of a static text, and use the textMultiLine flag in the
inputType attribute instead for editable text views (if both
singleLine and inputType are supplied, the inputType flags will
override the value of singleLine).
回答6:
Now android:singleLine
attribute is deprecated. Please add these attributes to your EditText
for an EditText
to be single line.
android:inputType="text"
android:imeOptions="actionNext"
android:maxLines="1"
回答7:
I have done this in the past with android:singleLine="true"
and then adding a listener for the "enter" key:
((EditText)this.findViewById(R.id.mytext)).setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
//if the enter key was pressed, then hide the keyboard and do whatever needs doing.
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
//do what you need on your enter key press here
return true;
}
return false;
}
});
回答8:
This will work for the EditText:
android:inputType="text"
Then I would set a max length for the input text:
android:maxLines="1"
Those are the 2 that are needed now.
回答9:
Include android:singleLine="true"
回答10:
Everyone's showing the XML way, except one person showed calling EditText's setMaxLines method. However, when I did that, it didn't work. One thing that did work for me was setting the input type.
EditText editText = new EditText(this);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
This allows A-Z, a-z, 0-9, and special characters, but does not allow enter to be pressed. When you press enter, it'll go to the next GUI component, if applicable in your application.
You may also want to set the maximum number of characters that can be put into that EditText, or else it'll push whatever's to the right of it off the screen, or just start trailing off the screen itself. You can do this like this:
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(8);
editText.setFilters(filters);
This sets the max characters to 8 in that EditText. Hope all this helps you.
回答11:
Programatically:
textView.setMaxLines(1);
回答12:
use
android:ellipsize="end"
android:maxLines="1"
回答13:
please try this code
android:inputType="textPersonName"
回答14:
The @Aleks G OnKeyListener() works really well, but I ran it from MainActivity and so had to modify it slightly:
EditText searchBox = (EditText) findViewById(R.id.searchbox);
searchBox.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
//if the enter key was pressed, then hide the keyboard and do whatever needs doing.
InputMethodManager imm = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchBox.getApplicationWindowToken(), 0);
//do what you need on your enter key press here
return true;
}
return false;
}
});
I hope this helps anyone trying to do the same.
回答15:
In order to restrict you just need to set the single line option on "true".
android:singleLine="true"
回答16:
Also it's important to pay attention if the property android:inputType is textMultiLine. If so, the properties android:singleLine, android:imeOptions, android:ellipsize and android maxLines will be ignored and your EditText will accepted MultiLines anyway.
回答17:
Add this line to your edittext
android:inputType="text"
回答18:
As android:singleLine="true"
is now Depreciated so use
android:maxLines="1"
Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).
回答19:
Use Below Code instead of your code
<EditText
android:id="@+id/searchbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:scrollHorizontally="true"
android:ellipsize="end"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:drawablePadding="10dp"
android:background="@drawable/edittext"
android:drawableLeft="@drawable/folder_full"
android:drawableRight="@drawable/search"
android:paddingLeft="15dp"
android:hint="search..."/>
回答20:
in XML File add this properties in Edit Text
android:maxLines="1"
回答21:
android:imeOptions="actionDone"
worked for me.
回答22:
You must add this line in your EditText
android:maxLines="1"
and another thing, don't forget set android:inputType
(whatever you want, text, phone .., but you must set it)
回答23:
At XML file. Just add
android:maxLines="1"