当我使用autocompletetextview搜索并首先写一个空格或两个空格时,不会出现字符串的单词。如果我先写一个空格,怎么能用autocompletetextview独立搜索?
函数.trim()
为我服务?还是.replaceAll("\\s+$", "")
为我服务?
如何在我的代码中实现这一点?
这是我的代码:
package com.example.epson.buscadormep;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] values = new String[]{"Cilindros pequeños de oxigeno gaseoso o de aire, de uso medicinal",
"Dispositivos que contienen oxigeno liquido",
};
//final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,values);
final ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,R.layout.custom_autocomplete_item,R.id.autoCompleteItem,values);
final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.txtMercancias);
autoCompleteTextView.setThreshold(1);//will start working from second character
autoCompleteTextView.setAdapter(adapter); //setting the adapter data into the AutoCompleteTextView
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// LinearLayout ll = (LinearLayout) view;
//This will give you the string value of selected list item
//TextView listItem = (TextView) ll.getChildAt(0);
//You can do this or apply own logic to find the selected value case
if (autoCompleteTextView.getText().toString().equals("Cilindros pequeños de oxigeno gaseoso o de aire, de uso medicinal"))
{
startActivity(new Intent( MainActivity.this, Main2Activity.class));
}
if (autoCompleteTextView.getText().toString().equals("Dispositivos que contienen oxigeno liquido"))
{
startActivity(new Intent( MainActivity.this, Main3Activity.class));
}
}
}
});
button=(Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
autoCompleteTextView.setText("");
}
});
}
}
答案 0 :(得分:0)
.replaceAll("\\s","")
从字符串中删除所有空白字符。
您可以将它放在.toString()方法之后。
应该注意,这会从字符串中删除所有空格,因此您将从比较中删除所有空格。
如果要从字符串的开头和结尾删除空格,请使用.trim()。像正则表达式一样,将它放在.toString()方法之后。
答案 1 :(得分:0)
String的类trim()方法足以满足您的查询需求。这是你如何做到的:
final String text = " foo ";
System.out.println(text.trim());
// in addition to above code, below code of line only replaces all leading whitespaces with empty strings, but you should above trim() method so your search can continue even after trailing whitespaces
System.out.println(text.replaceFirst("\\s++$", ""));