Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:限制用户选择自动完成建议以外的内容?_Android_Autocompletetextview - Fatal编程技术网

Android:限制用户选择自动完成建议以外的内容?

Android:限制用户选择自动完成建议以外的内容?,android,autocompletetextview,Android,Autocompletetextview,我提供了一个AutoCompleteTextView来向用户显示建议。根据用户选择的项目,我获取项目的ID并在数据库端使用它。现在,我的问题是强制用户仅从AutoCompleteTextView进行选择(即用户不应输入自己的文本)。这是客户的要求。如何做到这一点?好的,我假设您希望将用户的输入限制为建议框中列出的项目列表中包含的文本 例如,如果您有: 一个 两个 三 然后用户只能键入第一个字符“O”和“T”。 根据之前输入的文本等等 要实现这一点,您可以使用TextView的设置过滤器方法:

我提供了一个
AutoCompleteTextView
来向用户显示建议。根据用户选择的项目,我获取项目的ID并在数据库端使用它。现在,我的问题是强制用户仅从
AutoCompleteTextView
进行选择(即用户不应输入自己的文本)。这是客户的要求。如何做到这一点?

好的,我假设您希望将用户的输入限制为建议框中列出的项目列表中包含的文本

例如,如果您有:

  • 一个
  • 两个
然后用户只能键入第一个字符“O”和“T”。 根据之前输入的文本等等

要实现这一点,您可以使用TextView的设置过滤器方法:

此外,您可能需要文本更改侦听器和焦点侦听器在输入新字符时做出反应并更新过滤列表。。。加上更新过滤器

以下是我在一个项目中使用的十进制数字过滤器示例:

protected InputFilter[] getFilters()
    {
        InputFilter[] filters = new InputFilter[1];

        filters[0] = new InputFilter()
        {
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
            {
                // limit input to digits and decimal / thousand separator only
                // in case thousand separator is pressed, change it to decimal
                // separator instead
                String output = "";

                if (number.isFocused())
                {
                    for (int i = start; i < end; i++)
                    {
                        char c = source.charAt(i);

                        if (isDecimalOrThousandSeparator(c))
                        {
                            output = output + Character.toString(decimalSeparator);
                        }
                        else if (Character.isDigit(c))
                        {
                            output = output + Character.toString(c);
                        }
                    }

                    return output == "" ? null : output;
                }

                return null;
            }
        };

        return filters;
    }
受保护的InputFilter[]getFilters()
{
InputFilter[]过滤器=新的InputFilter[1];
筛选器[0]=新的InputFilter()
{
公共CharSequence筛选器(CharSequence源、int开始、int结束、跨区目标、int开始、int结束)
{
//仅将输入限制为数字和十进制/千位分隔符
//如果按下千位分隔符,则将其更改为十进制
//取而代之的是分离器
字符串输出=”;
if(number.isFocused())
{
for(int i=start;i
这里有一个非常简单的解决方案:

通过在
AutoCompleteTextView
中设置
setOnItemClickListener
,可以创建一个变量来存储所选值。然后,只要用户在字段中键入
TextWatcher
,您就可以
null
该值。最后,您可以在继续之前验证变量是否不为null

字符串my_var//跟踪!
AutoCompleteTextView tv=(AutoCompleteTextView)layout.findViewById(R.id.tv);
设置适配器(my_适配器);
tv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
my_var=my_adapter.getItem(position.toString();
}
});
/**
*每当用户键入时,取消设置var。验证将
*然后失败。这就是我们强制从列表中选择的方式。
*/
tv.addTextChangedListener(新的TextWatcher(){
@凌驾
public void beforeTextChanged(字符序列s、int start、int count、int after){}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
my_var=null;
}
@凌驾
公共无效后文本更改(可编辑的s){}
});

我正在从事的一个项目碰巧需要这样一个需求,我想我将与大家分享我实现所需需求的方式

我为自动完成文本视图添加了一个焦点更改侦听器,并在用户从自动完成更改焦点时进行了检查,并直接处理了这种情况

autoTextViewCountry.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if(!b) {
                // on focus off
                String str = autoTextViewCountry.getText().toString();

                ListAdapter listAdapter = autoTextViewCountry.getAdapter();
                for(int i = 0; i < listAdapter.getCount(); i++) {
                    String temp = listAdapter.getItem(i).toString();
                    if(str.compareTo(temp) == 0) {
                        return;
                    }
                }

                autoTextViewCountry.setText("");

            }
        }
    });
autoTextViewCountry.setOnFocusChangeListener(新视图.OnFocusChangeListener()){
@凌驾
public void onFocusChange(视图,布尔b){
如果(!b){
//对焦
字符串str=autoTextViewCountry.getText().toString();
ListAdapter ListAdapter=autoTextViewCountry.getAdapter();
对于(int i=0;i
因此,我的实现是:如果数组适配器中不存在键入的文本,则在焦点更改时清空文本视图,然后在继续下一阶段(如注册)时,检查此文本视图是否为空

希望这个方法能帮助别人


快乐编码。

通过调用
isSelectionFromPopup()

一个简单的解决方案是,只需检查当前输入是否是适配器中的项目之一,就可以检查是否从下拉式弹出窗口中进行了选择。您可以这样做:

val AutoCompleteTextView.isValid: Boolean
    get() {
        for (i in 0 until adapter.count) {
            if (adapter.getItem(i) == text.toString()) {
                return true
            }
        }

        return false
    }

下面是这个pro
AutoCompleteTextView.Validator的另一个解决方案,以确保有效值。当编辑文本失去焦点时,将调用验证程序

autoCompleteTextView.validator = object : AutoCompleteTextView.Validator {
        override fun isValid(text: CharSequence?): Boolean {
            return optionsList.contains(text.toString())
        }

        override fun fixText(invalidText: CharSequence?): CharSequence {
            return ""
        }
    }

其中,
选项列表
是有效值的列表。

我也有同样的要求,下面是我的实现:

autoCompleteTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autoCompleteTextView.showDropDown();
            }
        });
在xml集合中
focusable=false
focusableInTouchMode=false


快乐编码

为了拥有不可编辑的AutoCompleteTextView变体,您应该在AutoCompleteTextView中禁用用户输入。这可以通过在AutoCompleteTextView上设置android:inputType=“none”来实现。

只需将此属性添加到AutoCompleteTextView即可

android:focusable=“fal
autoCompleteTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                autoCompleteTextView.showDropDown();
            }
        });
        <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/menu"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Mode">

        <AutoCompleteTextView
            android:id="@+id/mode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            />

    </com.google.android.material.textfield.TextInputLayout>
AutoCompleteTextView mode = findViewById(R.id.mode);
   final List<String> modeList = new ArrayList();
    modeList.add("YEARLY");
    modeList.add("HALF-YEARLY");
    modeList.add("QUARTER-YEARLY");
    modeList.add("MONTHLY");
    mode.setAdapter(new ArrayAdapter(getApplicationContext(),R.layout.list_item,modeList));
mode.getText().toString()