本文介绍了如何强制用户仅输入自动完成建议列表项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Autocompltetextview,我希望用户只允许接受建议列表的值.用户不应输入其他值并提交.如果用户未选择autocomplettextview的值,那么我想显示吐司.谁能帮我解决这个问题.检查我的代码段代码并为我提供帮助?
I am using Autocompltetextview and I want user to allow only to accept the value of suggested list. User should not enter other value and submit. if user did not select the value of autocomplettextview then i want to show toast. can any one help me to solve this . check my snippet code and help me?
代码
actv = (AutoCompleteTextView)rootView.findViewById(R.id.search_donor_area);
actv.setThreshold(1);//will start working from first character
materialDesignSpinner.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
sel_blood_group=materialDesignSpinner.getText().toString();
Log.d("sel_blood_group",sel_blood_group);
}
});
citymaterialDesignSpinner = (MaterialBetterSpinner)
rootView.findViewById(R.id.search_donor_city);
citymaterialDesignSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Log.d("POSITION", CITY_SPINNERLIST[position]);
sel_city=citymaterialDesignSpinner.getText().toString();
Log.d("search_sel_city",sel_city);
if (position == 0) {
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> areasadapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.select_dialog_item, AREA_DONOR);
actv.setAdapter(areasadapter);//setting the adapter data into the AutoCompleteTextView
} else if (position == 1) {
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> areasadapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.select_dialog_item, AREA_DONOR_RAJKOT);
actv.setAdapter(areasadapter);//setting the adapter data into the AutoCompleteTextView
}
else if (position == 2) {
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> areasadapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.select_dialog_item, AREA_DONOR_SURAT);
actv.setAdapter(areasadapter);//setting the adapter data into the AutoCompleteTextView
}
else if (position == 3) {
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> areasadapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.select_dialog_item, AREA_DONOR_BARODA);
actv.setAdapter(areasadapter);//setting the adapter data into the AutoCompleteTextView
}
}
});
ArrayAdapter<String> cityarrayAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_dropdown_item_1line, CITY_SPINNERLIST);
citymaterialDesignSpinner.setAdapter(cityarrayAdapter);
actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("========>>", actv.getText().toString());
sel_area=actv.getText().toString();
}
});
search_button = (Button)rootView.findViewById(R.id.search_button);
推荐答案
将actv.getText().toString()与适配器的值进行比较.
Compare actv.getText().toString() with your adapter's values.
boolean isValid = false;
for (String suggestion : listSuggestion) {
if (actv.getText().equalsIgnoreCase(suggestion))
isValid = true;
}
如果isValid = false,请显示吐司.
If isValid = false, show toast.
这篇关于如何强制用户仅输入自动完成建议列表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!