0x01 EditText
0x02 AutoCompleteTextView
AutoCompleteTextView是一个自动提示内容的文本框,其是EditText的一个子类
android:completionThreshold="n"
其中n为输入n+个字符就显示匹配结果
通过ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strings);
和
autoCompleteTextView.setAdapter(arrayAdapter);
来绑定控件,其中strings
为字符串数组,是供匹配的数据源
XML文件
<AutoCompleteTextView
android:hint="input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:completionThreshold="2"
android:id="@+id/autoCompleteTextView1" />
Java代码
package com.feng.learn1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 0x01:初始化控件
* 0x02:需要一个适配器
* 0x03:初始化数据源
* 0x04:将adpter与控件绑定
*/
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
String []strings = {"Android sdk","Android studio","Ans","Java","Javac"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strings);
autoCompleteTextView.setAdapter(arrayAdapter);
}
}
AutoCompleteTextView.png
0x03 MultiAutoCompleteTextView
MultiAutoCompleteTextView可支持选择多个值(分别用分隔符隔开)并且在每个值选中的情况下可以根据当前输入匹配,如发邮箱……
XML文件(与AutoCompleteTextView类似)
<MultiAutoCompleteTextView
android:hint="input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:textSize="20sp"
android:completionThreshold="2"
android:id="@+id/multiAutoCompleteTextView1" />
Java代码
package com.feng.learn1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
private MultiAutoCompleteTextView multiAutoCompleteTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 0x01:初始化控件
* 0x02:需要一个适配器
* 0x03:初始化数据源
* 0x04:将adpter与控件绑定
* 0x05:设置分隔符(下例为逗号)
*/
multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
String []strings = {"Android sdk","Android studio","Ans","Java","Javac"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strings);
multiAutoCompleteTextView.setAdapter(arrayAdapter);
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
MultiAutoCompleteTextView.png
网友评论