AutoCompleteTextView(自动提示框)

package com.bw.yztc_AutoCompleteTextView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import com.bw.yztc_imageview.R;

/**
* 演示AutoCompleteTextView(自动提示框)
*/

public class Main3Activity extends Activity {

private AutoCompleteTextView autoview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    // 获取id
    autoview = (AutoCompleteTextView) findViewById(R.id.auto);
    // 1.准备数据源
    String[] countrys = getResources().getStringArray(R.array.country);
    // 2.将数据源数据加载到适配器中
    ArrayAdapter adapter = new ArrayAdapter(
            Main3Activity.this, android.R.layout.simple_list_item_1,
            countrys);
    // 3.将适配器数据加载到控件中
    autoview.setAdapter(adapter);
    // 表示当AutoCompleteTextView控件中某一项被点击的监听事件
    autoview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View view,
                int position, long id) {
            // 根据下标获取点击item的信息
            String str = parent.getItemAtPosition(position).toString();
            Toast.makeText(Main3Activity.this, "当前选中了" + str, 0).show();
            }
        });
    }
}

布局







vaules-strings.xml下创一个数组

  
    Afghanistan
    Alanian
    Algeria
    American
    Andorra
    Anguilla
  

你可能感兴趣的:(AutoCompleteTextView(自动提示框))