如何在AutoCompleteTextView上设置文本

时间:2017-11-23 08:39:02

标签: java android autocompletetextview

我无法在AutoCompleteTextView上设置默认值。 首先,我尝试了setText("default value")。 然后,我搜索并尝试了这个:

autoText.postDelayed(new Runnable() {
    @Override
    public void run() {
        autoText.showDropDown();
    }
},500);
autoText.setText("chi");
autoText.setSelection(tvNewRestoAddress.getText().length());

但它仍然无效。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

试试这个

  AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.edt);
    String[] array = {"abc", "bcd", "cde", "def", "efg", "fgh", "PREM"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.select_dialog_item, array);

    autoCompleteTextView.setAdapter(adapter);
    autoCompleteTextView.postDelayed(new Runnable() {
        @Override
        public void run() {
            autoCompleteTextView.setText("PREM");
            autoCompleteTextView.showDropDown();

        }
    }, 10);

答案 1 :(得分:0)

 String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};
     ArrayAdapter<String> adapter = new ArrayAdapter<String>
                    (this, android.R.layout.select_dialog_item, fruits);
     AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
            actv.setThreshold(1);//will start working from first character
            actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView