如何强制用户仅输入自动填充建议列表项?

时间:2017-09-23 07:22:46

标签: android arraylist autocompletetextview

我正在使用Autocompltetextview,我希望用户只允许接受建议列表的值。用户不应输入其他值并提交。如果用户没有选择autocomplettextview的值,那么我想要显示吐司。任何人都可以帮我解决这个问题。检查我的代码片段并帮助我?

代码

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);

1 个答案:

答案 0 :(得分:0)

将 actv.getText().toString() 与适配器的值进行比较。

boolean isValid = false;
for (String suggestion : listSuggestion) {
  if (actv.getText().equalsIgnoreCase(suggestion))
    isValid = true;
}

如果 isValid = false,则显示 toast。