可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have an Edittext in my android application. I don't want to allow user to enter first space character..but after entering other charecter user can enter space also..I used
<EditText
android:id="@+id/editText1_in_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789">
but in this case user can not enter space.
I have also used Text Watcher but I need not to allow user at the time of entering text as
android:digits works.
回答1:
final EditText editText = (EditText)findViewById(R.id.editText1_in_row);
InputFilter filter = new InputFilter() {
boolean canEnterSpace = false;
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if(editText.getText().toString().equals(""))
{
canEnterSpace = false;
}
StringBuilder builder = new StringBuilder();
for (int i = start; i < end; i++) {
char currentChar = source.charAt(i);
if (Character.isLetterOrDigit(currentChar) || currentChar == '_') {
builder.append(currentChar);
canEnterSpace = true;
}
if(Character.isWhitespace(currentChar) && canEnterSpace) {
builder.append(currentChar);
}
}
return builder.toString();
}
};
editText.setFilters(new InputFilter[]{filter});
and remove this property from your EditText
android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789"
This code works exactly according to your needs.
回答2:
Using InputFilter easy to handle enter first white space character ignore
First setFilters() method on editText
editText.setFilters(new InputFilter[]{ignoreFirstWhiteSpace()});
Make InputFilter
// ignore enter First space on edittext
public InputFilter ignoreFirstWhiteSpace() {
return new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isWhitespace(source.charAt(i))) {
if (dstart == 0)
return "";
}
}
return null;
}
};
}
No need to write android:digits property on XML
remove this line
android:digits="_,qwertzuiopasdfghjklyxcvbnm,QWERTYUIOPASDFGHJKLZXCVBNM,0123456789"
回答3:
why cant you use editText.getText().trim(); function while using the EditText data
回答4:
If you want to filter input characters in your EditText, you need to use InputFilter. Here is example.
//Allow only letters or digits
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
EditText text = (EditText)findViewById(R.id.edittext1);
text.setFilters(new InputFilter[]{filter});
For details look here
回答5:
Use this. If the character at starting Position is a space, set textView Text To blank
editText1_in_row.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length()>0 && s.subSequence(0, 1).toString().equalsIgnoreCase(" ")) {
editText1_in_row.setText(""); }
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
回答6:
A slight variation on https://stackoverflow.com/users/2868352/abhishek-v answer.
public class NoInitialSpaceFilter implements InputFilter {
@Override
public CharSequence filter(final CharSequence source, final int start, final int end, final Spanned dest, final int dstart, final int dend) {
if (dstart == 0) {
for (int i = start; i < end; i++) {
if (Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
}
return null;
}
}
Usage:
editText.setFilters(new InputFilter[]{new NoInitialSpaceFilter});
回答7:
Simply restrict the user to type space as others said on start only:
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = createPL.getText().toString();
//restrict space for first char
if (text.startsWith(" ")) {
edittext.setText(text.trim());
}
}
回答8:
This works for me
android:inputType="textPersonName"
android:digits= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-!@#$%^*()"
回答9:
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,_-!@#$()+=><:;?"