--------------------------------------------Layout activity_main.xml--------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/view_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1"
android:background="#000000" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入:" android:padding="10dp"/>
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
--------------------------------------------MainActivity.java-----------------------------------
package com.ch12;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.widget.TextView;
/**
*
* 项目名称:com.ch12
* 类名称:MainActivity
* 类描述:Spinner、AutocompletionTextView
* 创建人:方勇
* 创建时间:2012-11-19 下午11:45:04
* Copyright (c) 方勇-版权所有
*/
public class MainActivity extends Activity {
/* 显示所选城市 */
private TextView view_city;
/* 城市下拉列表 */
private Spinner spinner;
/* 自动提示 */
private AutoCompleteTextView autoCompleteTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setListeners();
init();
}
/* 初始化UI */
private void findViews() {
view_city = (TextView) findViewById(R.id.view_city);
spinner = (Spinner) findViewById(R.id.spinner);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete);
}
/* 设置监听器 */
private void setListeners() {
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
/* 初始化对象,设置数据 */
private void init() {
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, getData()));
autoCompleteTextView
.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, getData()));
}
/* 自定义选择事件类 */
class MyOnItemSelectedListener implements Spinner.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
view_city.setText(parent.getItemAtPosition(position).toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
/* 设置数据集合 */
private List<String> getData() {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add("fy_" + i);
}
return list;
}
}
--------------------------------------------效果图-----------------------------------------------
<!--EndFragment-->