当我输入第一个字符时,AutoCompleteTextView不起作用

时间:2014-10-16 12:42:57

标签: android autocompletetextview

当我在文本框中输入第一个字符但在输入第二个字符时开始显示下拉列表时,我的AutoCompleteTextView不起作用。可能是什么原因?

<AutoCompleteTextView
    android:id="@+id/autocomplete_name"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="7"
    android:background="@drawable/edittextback"
    android:ems="10"
    android:textSize="15sp"
    android:hint="@string/codehint"
    android:textColorHint="@color/hintgrey"
    android:dropDownWidth="fill_parent"
    android:paddingRight="30dp"
    android:paddingLeft="10dp"
    android:singleLine="true"
    android:ellipsize="end"
/>

3 个答案:

答案 0 :(得分:45)

EASY:D

您需要将completionThreshold的{​​{1}}属性设置为1。

autoCompleteView

或者 通过代码使用动态地完成它

<AutoCompleteTextView 
    android:id="@+id/someID" 
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:completionThreshold="1" />

快乐编码:)

答案 1 :(得分:1)

使用我的java代码

autoComplete.setThreshold(1);

或 在xml中

android:completionThreshold="1"

答案 2 :(得分:0)

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity {

     private AutoCompleteTextView autoComplete;
     private MultiAutoCompleteTextView multiAutoComplete;
     private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get the defined string-array 
        String[] colors = getResources().getStringArray(R.array.colorList);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,colors);

        autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
        multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);

        // set adapter for the auto complete fields
        autoComplete.setAdapter(adapter);
        multiAutoComplete.setAdapter(adapter);

        // specify the minimum type of characters before drop-down list is shown
        autoComplete.setThreshold(1);
        multiAutoComplete.setThreshold(2);
        // comma to separate the different colors
        multiAutoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

        // when the user clicks an item of the drop-down list
        multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                 Toast.makeText(getBaseContext(), "MultiAutoComplete: " +
                            "you add color "+arg0.getItemAtPosition(arg2),
                            Toast.LENGTH_LONG).show();
            }
        });
    }

}