您的位置:首页 > 移动开发 > Android开发

android AutoCompleteTextView自动完成文本

2015-05-05 11:08 357 查看
输入两个汉字,自动给出提示,选中至文本框后,点击按钮,Toast给出提示

1.main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

<AutoCompleteTextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="7"

android:text=""

android:id="@+id/autocompletetextview1"

android:completionThreshold="2"

android:completionHint="搜索输入内容"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="搜索"

android:id="@+id/button1"

android:layout_marginLeft="10dp"

/>

</LinearLayout>

2.MainActivity

AutoCompleteTextView mTextView;

private static final String[] COUNTRIES = new String[] {"明日科技",

"明日科技有限公司", "吉林省明日科技有限公司", "明日编程词典", "明日"};

ArrayAdapter<String> adapter;

Button mButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mTextView = (AutoCompleteTextView) findViewById(R.id.autocompletetextview1);

adapter = new ArrayAdapter<String> (this,

android.R.layout.simple_dropdown_item_1line, COUNTRIES);

mTextView.setAdapter(adapter);

mButton = (Button) findViewById(R.id.button1);

mButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(MainActivity.this, mTextView.getText().toString(),

Toast.LENGTH_SHORT).show();

}

});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: