1.编辑activity_main.xml文件,添加MultiAutoCompleteTextView组件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button" />
<!--
android:singleLine="true"设置单行显示,否则会自动换行
android:ellipsize="marquee"之后三个设置都是必须的
android:focusable="true"
android:focusableInTouchMode="true"
-->
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"/>
<!-- 新添加的内容 -->
<com.zyy.myapp.MyTextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"/>
<com.zyy.myapp.MyTextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"/>
<!--新添加的内容-->
<!--
android:completionThreshold="1"输入一个字符之后自动匹配
-->
<AutoCompleteTextView
android:completionThreshold="1"
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint" >
<requestFocus />
</AutoCompleteTextView>
<!--新添加的内容-->
<!--
android:completionThreshold="1"输入一个字符之后自动匹配
-->
<MultiAutoCompleteTextView
android:completionThreshold="1"
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint" />
</LinearLayout>
2.编辑MainActivity.java文件
package com.zyy.myapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
//创建Button对象引用变量
private Button loginButton;
//创建AutoCompleteTextView对象引用变量
private AutoCompleteTextView actv;
//创建MultiAutoCompleteTextView对象引用变量
private MultiAutoCompleteTextView mactv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过id获取对象,该对象是view类型,view是所有组件的父类,转化为Button对象需要进行强转
loginButton = (Button)findViewById(R.id.button1);
//添加事件监听
/*loginButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//此处不能只用this
Toast.makeText(MainActivity.this, "哎呀,我被点了一下!", Toast.LENGTH_SHORT).show();
}
});*/
/*loginButton.setOnClickListener(new MyOnClickListener());*/
loginButton.setOnClickListener(this);
//---AutoCompleteTextView组件---
//获取AutoCompleteTextView对象
actv = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
//创建数组适配器对象
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
R.layout.support_simple_spinner_dropdown_item,
new String[]{
"张玉尧", "张秀琼", "张馨月", "吴杰"
});
//向AutoCompleteTextView对象添加数组适配器
actv.setAdapter(adapter);
//---MultiAutoCompleteTextView组件---
//获取MultiAutoCompleteTextView对象
mactv = (MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
//向MultiAutoCompleteTextView对象添加数组适配器
mactv.setAdapter(adapter);
//设置分割标志,实现多次自动补全
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "哎呀,怎么老是点我啊!", Toast.LENGTH_SHORT).show();
}
}
/*class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
//这个地方出现问题,最后用v.getContext()解决
Toast.makeText(v.getContext(), "哎呀,我又被点了一下!", Toast.LENGTH_SHORT).show();
}
}
*/