当前位置:操作系统 > 安卓/Android >>

AutoCompleteTextView数据绑定

首先,在布局文件main.xml中添加AutoCompleteTextView组件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<AutoCompleteTextView
    android:id="@+id/auto_complete"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"
    />
</LinearLayout>
然后在MainActivity.java中定义需要绑定的数据,并将其绑定到组件上。代码如下:
public class MainActivity extends Activity { 
    // 定义一组需要绑定的数据 
    private static final String[] COUNTRIES = new String[] { "China", "Russia", 
            "Germany", "USA", "Chinal", "Germany1", "China12" }; 
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        // 进行数据绑定 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                android.R.layout.易做图_dropdown_item_1line, COUNTRIES); 
        AutoCompleteTextView textview = (AutoCompleteTextView) findViewById(R.id.auto_complete); 
        textview.setAdapter(adapter); 
    } 
}
 效果图如下:

 
本文出自 “Android学习心得” 博客

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,