1. 程式人生 > >自動文本框提示

自動文本框提示

public sim logs threshold reat bsp n) ken vertical

技術分享

activity_main.xml布局

<LinearLayout 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:orientation="vertical" >

<!--

默認輸2個字符才能有提示

completionThreshold表示只輸入1個字符後,就有提示

requestFocus表示界面展開時焦點直接在第二個文本框

-->

<AutoCompleteTextView

android:id="@+id/myTextView01"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:completionThreshold="1" />

<MultiAutoCompleteTextView

android:id="@+id/myTextView02"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:completionThreshold="1">

<requestFocus />

</MultiAutoCompleteTextView>

</LinearLayout>

代碼實現
public class MainActivity extends Activity {

  private AutoCompleteTextView myTextView01;
  private MultiAutoCompleteTextView myTextView02;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myTextView01 = (AutoCompleteTextView) findViewById(R.id.myTextView01);
    myTextView02 = (MultiAutoCompleteTextView) findViewById(R.id.myTextView02);

    String[] str={"xiaohe","xiaowang","xiaoli","zhanghe","zhangmin","zhaojun","lihe","daming"};
    /*
    * 創建適配器
    * 參數一:上下文
    * 參數二:提示下位框的樣式,不喜歡可以換android.R.layout.*
    * 參數三:下拉框中備選的內容
    */
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(
    this,
    android.R.layout.simple_dropdown_item_1line,
    str);

    //將Adapter設置到AutoCompleteTextView中
    myTextView01.setAdapter(adapter);

    myTextView02.setAdapter(adapter);
    //以","作為分隔符
    myTextView02.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
  }
}

自動文本框提示