我一整天都在思考这个问题。我有一个简单的 AutoCompleteTextview ,它的建议之一是 洛杉矶 市。如果用户在 Los Angeles 中输入两个单词之间有 1 个空格,则系统会正常工作,但如果他们在两个单词之间输入 2 个或更多空格,则建议会停止。这是我的代码
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));
}
}
我刚刚输入的粗略代码,只是为了给您一些启发。
关于java - Android AutoCompleteTextView 如何忽略多余的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32703561/