Android AutoCompleteTextView 如何忽略多余的空格
Android AutoCompleteTextView how can I ignore extra spaces
我整天都在为这个问题而苦恼。我有一个简单的 AutoCompleteTextview,它的建议之一是 Los Angeles 的城市。如果用户在 Los Angeles 中输入 2 个单词之间有 1 space,则它有效,但如果他们在建议之间输入 2 space 或更多,则建议停止.这是我的代码
AutoCompleteTextView location= (AutoCompleteTextView)findViewById(R.id.location);
String[] arrays= {"Los Angeles"};
// Los Angeles: this works
// Los Angeles: this does not work notice the extra space in between
// I want to treat extra spaces as just 1 space
location.setThreshold(2);
ArrayAdapter<String> adapter= new ArrayAdapter<>(Purpose.this, android.R.layout.simple_selectable_list_item,arrays);
location.setAdapter(adapter);
这个我也试过了
String[] arrays= {"Los Angeles".replaceAll(" "," ")};
// I can not obviously use trim because it merges everything
我有很多组数据,但这是根本问题,如果我能解决这部分问题,那么它就解决了另一组数据的问题。
我不知道有什么可以做到这一点,我个人只会编写一个单独的方法来清理您的 String
。涉及 StringBuilder
的内容
for(int x = 0; x < YourString.length(); x++) {
if(YourString[x].equals(" ") && YourString[x-1].equals(" ") {
(StringBuilder.removeCharAt(x-1));
}
}
我刚刚输入的粗略代码,但只是为了给你一些灵感。
我整天都在为这个问题而苦恼。我有一个简单的 AutoCompleteTextview,它的建议之一是 Los Angeles 的城市。如果用户在 Los Angeles 中输入 2 个单词之间有 1 space,则它有效,但如果他们在建议之间输入 2 space 或更多,则建议停止.这是我的代码
AutoCompleteTextView location= (AutoCompleteTextView)findViewById(R.id.location);
String[] arrays= {"Los Angeles"};
// Los Angeles: this works
// Los Angeles: this does not work notice the extra space in between
// I want to treat extra spaces as just 1 space
location.setThreshold(2);
ArrayAdapter<String> adapter= new ArrayAdapter<>(Purpose.this, android.R.layout.simple_selectable_list_item,arrays);
location.setAdapter(adapter);
这个我也试过了
String[] arrays= {"Los Angeles".replaceAll(" "," ")};
// I can not obviously use trim because it merges everything
我有很多组数据,但这是根本问题,如果我能解决这部分问题,那么它就解决了另一组数据的问题。
我不知道有什么可以做到这一点,我个人只会编写一个单独的方法来清理您的 String
。涉及 StringBuilder
for(int x = 0; x < YourString.length(); x++) {
if(YourString[x].equals(" ") && YourString[x-1].equals(" ") {
(StringBuilder.removeCharAt(x-1));
}
}
我刚刚输入的粗略代码,但只是为了给你一些灵感。