android开发步步为营之12:AutoCompleteTextView和MultiAutoCompleteTextView

一、理论学习

public class
AutoCompleteTextView

extends EditText
implements Filter.FilterListener
java.lang.Object

android.view.View

android.widget.TextView

android.widget.EditText

android.widget.AutoCompleteTextView
Known Direct Subclasses
MultiAutoCompleteTextView
MultiAutoCompleteTextView

An editable text view, extending AutoCompleteTextView, that can show completion suggestions for the substring of the text where the user is typing instead of necessarily for the entire thing.

ClassOverview

Aneditable text view that shows completion suggestions automatically while theuser is typing. The list of suggestions is displayed in a drop down menu fromwhich the user can choose an item to replace the content of the edit box with.
Thedrop down can be dismissed at any time by pressing the back key or, if no itemis selected in the drop down, by pressing the enter/dpad center key.
Thelist of suggestions is obtained from a data adapter and appears only after agiven number of characters defined by the threshold.

public class
MultiAutoCompleteTextView

extends AutoCompleteTextView
java.lang.Object

android.view.View

android.widget.TextView

android.widget.EditText

android.widget.AutoCompleteTextView

android.widget.MultiAutoCompleteTextView
ClassOverview

Aneditable text view, extending AutoCompleteTextView,that can show completion suggestions for the substring of the text where theuser is typing instead of necessarily for the entire thing.
Youmust must provide a MultiAutoCompleteTextView.Tokenizer to distinguish the various substrings.

 

二、实践

1、创建AutoCompleteTextViewActivity


packagecom.figo.helloworld;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.widget.ArrayAdapter;

importandroid.widget.AutoCompleteTextView;

importandroid.widget.MultiAutoCompleteTextView;

 

/**

 * @author zhuzhifei

 * @版权所有

 */

publicclass AutoCompleteTextViewActivity extends Activity {

       private static final String[] cities =new String[] { "北京","上海",

                     "广州", "深圳","香港", "台北", "重庆","南京",

                     "南昌", "南宁"};

       private AutoCompleteTextViewautoCompleteTextView = null;
       private MultiAutoCompleteTextViewmultiAutoCompleteTextView = null;
       @Override
       protected void onCreate(BundlesavedInstanceState) {

              // TODO Auto-generated method stub

              super.onCreate(savedInstanceState);
              super.onCreate(savedInstanceState);
              setContentView(R.layout.autocompletetextviewtest);
              autoCompleteTextView =(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
              multiAutoCompleteTextView =(MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
              // 创建适配器
              ArrayAdapter<String> adapter= new ArrayAdapter<String>(
              this,android.R.layout.simple_dropdown_item_1line, cities);
              autoCompleteTextView.setAdapter(adapter);
              // 设置输入多少字符后提示,默认值为1
              autoCompleteTextView.setThreshold(1);
              multiAutoCompleteTextView.setAdapter(adapter);
              multiAutoCompleteTextView.setThreshold(1);
              // 用户必须提供一个MultiAutoCompleteTextView.Tokenizer用来区分不同的子串。
              multiAutoCompleteTextView

                            .setTokenizer(newMultiAutoCompleteTextView.CommaTokenizer());
       }
}

2、运行效果
AutoCompleteTextView:

MultiAutoCompleteTextView:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值