📜  Android AutoCompleteTextView示例

📅  最后修改于: 2020-09-29 02:22:09             🧑  作者: Mango

Android AutoCompleteTextView示例

Android AutoCompleteTextView基于保留的单词来完成单词,因此无需编写单词的所有字符。

Android AutoCompleteTextView是可编辑的文本字段,它在下拉菜单中显示建议列表,用户只能从中选择一个建议或值。

Android AutoCompleteTextView是EditText类的子类。 MultiAutoCompleteTextView是AutoCompleteTextView类的子类。

Android AutoCompleteTextView示例

在此示例中,我们在autocompletetextview中显示编程语言。所有的编程语言都存储在字符串数组中。我们正在使用ArrayAdapter类来显示数组内容。

让我们看看android中autocompletetextview的简单示例。

activity_main.xml

从面板上拖动AutoCompleteTextView和TextView,现在activity_main.xml文件将如下所示:




    

    


活动课

让我们编写AutoCompleteTextView的代码。

package example.javatpoint.com.autocompletetextview;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {
    String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Creating the instance of ArrayAdapter containing list of language names
        ArrayAdapter adapter = new ArrayAdapter
                (this,android.R.layout.select_dialog_item,language);
        //Getting the instance of AutoCompleteTextView
        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
        actv.setTextColor(Color.RED);
    }
}

输出: