首页 > 代码库 > android的自动匹配输入

android的自动匹配输入

1.res-layout

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <AutoCompleteTextView
android:threshold="3" android:id
="@+id/autoCompleteTextView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="38dp" android:ems="10" android:hint="请输入相应的文本"> <requestFocus /> </AutoCompleteTextView> </RelativeLayout>

2.   .java文件

package com.example.text1;

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 AutoCompleteTextView at;
    private ArrayAdapter<String> adapter;
    //        初始化数据源
    private String[] res = {"mukewang ","jikexueyuan","51cto"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
//        初始化控件
        at = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

        
//        适配器 适配下拉列表的数据
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, res);
//        给适配器加载数据源
        at.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

android的自动匹配输入