Android学习第八天--autocompletetextview
autocompletetextview 自动填充文本框组件
该组件使用起来也是比较简单的,只需要定义一个数组,然后将数组放入到arrayAdapter中就可以实现自动填充文本框内容的功能
以下是xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <AutoCompleteTextView android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/autotextview" android:completionThreshold="1" android:hint="请输入字符"/> <Button android:layout_weight="17" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="确定"/> </LinearLayout>
android:layout_weight="17"这段代码,主要的功能是将屏幕分成几份的功能,上面两边加起来是18份,所以将屏幕左右分成18份,而这个button占了17份
以下是实现自动填充的java代码:
import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends Activity { private String [] mContent={"aaaa","bbbb","aaabbb"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AutoCompleteTextView mAutoCompleteTextView=(AutoCompleteTextView)findViewById(R.id.autotextview); ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mContent); mAutoCompleteTextView.setAdapter(mArrayAdapter); } }
ArrayAdapter<String> mArrayAdapter = new ArrayAdatper<String>(this,android.R.layout.simple_list_item_1,mContent);
其中的android.R.layout.simple_list_item_1表示的是布局文件,该布局文件是系统自带的布局文件。