[英]Android AutoCompleteTextView how can I ignore extra spaces
我整天都在努力解決這個問題。 我有一個簡單的AutoCompleteTextview,它的建議之一是洛杉磯市 。 如果用戶在洛杉磯輸入的兩個單詞之間有1個空格,則可以使用,但如果用戶在2個單詞之間輸入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));
}
}
我剛剛輸入的原始代碼只是為了給您一些啟發。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.