我有 autocompletetextview ,其中我只想要那些在我的原始资源文本文件中的文本,其中我存储了所有名称并显示在自动建议列表< /强> 如果用户尝试输入除列表以外的不同文字,则验证显示:“仅接受所选名称”
public class MainActivity extends AppCompatActivity {
Button submit;
TextView timerInfo;
TextView timertext;
SharedPreferences pref;
CountDownTimer countDownTimer;
Scanner scanner;
ArrayList<String> list;
ArrayAdapter<String> adapter;
AutoCompleteTextView autolist;
AccessibilityService.SoftKeyboardController keyboardController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanner= new Scanner(getResources().openRawResource(R.raw.names)).useDelimiter("\n");
list=new ArrayList<>();
getList();
autolist=(AutoCompleteTextView) findViewById(R.id.list_names);
autolist.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus && v.getId()==R.id.list_names){
autolist.setCursorVisible(true);
populate();
}
else{
((AutoCompleteTextView)v).performValidation();
autolist.setCursorVisible(false);
}
}
});
autolist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
autolist.setCursorVisible(true);
}
});
autolist.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
autolist.setCursorVisible(false);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(autolist.getWindowToken(), 0);
return true;
}
return false;
}
});
pref=getSharedPreferences("AppClose",MODE_PRIVATE);
String abc=pref.getString("demover","");
if(abc=="demover"){
finish();
}
submit=(Button) findViewById(R.id.timerStart);
timerInfo=(TextView) findViewById(R.id.timerInfo);
timertext=(TextView) findViewById(R.id.timertxt);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timertext.setText("Time Left :");
submit.setVisibility(View.INVISIBLE);
countDownTimer=new CountDownTimer(30000,1000){
@Override
public void onTick(long millisUntilFinished) {
timerInfo.setText(String.valueOf((int) (millisUntilFinished / 1000)));
}
@Override
public void onFinish() {
pref.edit().putString("demover","demover").apply();
finish();
}
}.start();
}
});
autolist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
isSelectedText = true;
}
});
autolist.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
isSelectedText = false;
}
@Override
public void afterTextChanged(Editable editable) {
if(isSelectedText){
Toast.makeText(MainActivity.this, "nice", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "not nice", Toast.LENGTH_SHORT).show();
}
}
});
}
class Validator implements AutoCompleteTextView.Validator {
@Override
public boolean isValid(CharSequence text) {
Log.v("Test", "Checking if valid: "+ text);
Arrays.sort(new InputStream[]{getResources().openRawResource(R.raw.names)});
if (Arrays.binarySearch(new InputStream[]{getResources().openRawResource(R.raw.names)}, text.toString()) > 0) {
return true;
}
return false;
}
@Override
public CharSequence fixText(CharSequence invalidText) {
Toast.makeText(MainActivity.this, "Fixed", Toast.LENGTH_SHORT).show();
/* I'm just returning an empty string here, so the field will be blanked,
* but you could put any kind of action here, like popping up a dialog?
*
* Whatever value you return here must be in the list of valid words.
*/
return "Digger";
}
}
public void getList(){
try{
while(scanner.hasNextLine()){
list.add(scanner.next());
}
}catch (Exception e){e.printStackTrace();}
}
public void populate(){
adapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,list);
autolist.setAdapter(adapter);
}
}
我在某些方面使用过调试器,它显示已挂起主题。
我想要的是什么:只有那些作为自动建议输入的文字
答案 0 :(得分:2)
- AutoCompleteTextView具有建议项目的侦听器。因此,如果单击该项目,则将状态设置为true,否则重新生成 假
- 用户可以选择并修改,以便添加TextWatcher,以便在文本更改时将状态更改为false。
- 使用isSelectedText
中的if / else执行所需操作 醇>
autoText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
isSelectedText = true;
}
});
autoText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
isSelectedText = false;
}
@Override
public void afterTextChanged(Editable editable) {
}
});
答案 1 :(得分:0)
您可以使用此而不是使用扫描仪。
try {
Resources res = getResources();
InputStream in = res.openRawResource(R.raw.names);
byte[] byte = new byte[in.available()];
in .read(btye);
list.add(new String(btye));
} catch (Exception e) {
e.printStackTrace();
}