摸鱼学Android 十六(列表选项框+自动完成文本框)
UI控件之六
1 Spinner(列表选项框)
1.1 常用属性
- dropDownHorizontalOffset:设置列表框的水平偏移距离
- dropDownVerticalOffset:设置列表框的水平竖直距离
- dropDownSelector:列表框被选中时的背景
- dropDownWidth:设置下拉列表框的宽度
- gravity:设置里面组件的对其方式
- popupBackground:设置列表框的背景
- prompt:设置对话框模式的列表框的提示信息(标题),只能够引用string.xml 中的资源id,而不能直接写字符串
- spinnerMode:列表框的模式,有两个可选值: dialog:对话框风格的窗口 dropdown:下拉菜单风格的窗口(默认)
- entries:使用数组资源设置下拉列表框的列表项目
1.2 实例
复用上一节的代码
- 新建布局spinner_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/logo" />
<TextView
android:id="@+id/txt_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/img_icon"
android:layout_marginTop="30dp"
android:text="文本"
android:textSize="18sp" />
</LinearLayout>
- activity_main.xml加入spinner
<Spinner
android:id="@+id/spin"
android:layout_width="200dp"
android:layout_height="64dp" />
- MainActivity.java
public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private ArrayList<Icon> mData = null;
private BaseAdapter myAdadpter = null;
private Context mContext;
//判断是否为刚进去时触发onItemSelected的标志
private boolean selected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
mData = new ArrayList<>();
bindViews();
}
private void bindViews() {
spinner = findViewById(R.id.spin);
mData.add(new Icon(R.drawable.chat,"聊天"));
mData.add(new Icon(R.drawable.game,"游戏"));
mData.add(new Icon(R.drawable.mike,"录音"));
mData.add(new Icon(R.drawable.music,"音乐"));
mData.add(new Icon(R.drawable.printer,"打印机"));
mData.add(new Icon(R.drawable.location,"定位"));
mData.add(new Icon(R.drawable.torch,"手电筒"));
myAdadpter = new MyAdapter<Icon>(mData, R.layout.spinner_item) {
@Override
public void bindView(ViewHolder holder, Icon obj) {
holder.setImageResource(R.id.icon_img, obj.getiId());
holder.setText(R.id.icon_text, obj.getiName());
}
};
spinner.setAdapter(myAdadpter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//重复选不提示
if(selected){
TextView txt_name = view.findViewById(R.id.icon_text);
Toast.makeText(mContext,"你选择了:" + txt_name.getText().toString(),
Toast.LENGTH_SHORT).show();
}else {
selected = true;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
显示:
2 AutoCompleteTextView(自动完成文本框)
2.1 常用属性
- completionHint:设置下拉菜单中的提示标题
- completionHintView:定义提示视图中显示下拉菜单
- completionThreshold:指定用户至少输入多少个字符才会显示提示
- dropDownAnchor:设置下拉菜单的定位"锚点"组件,如果没有指定改属性, 将使用该TextView作为定位"锚点"组件
- dropDownHeight:设置下拉菜单的高度
- dropDownWidth:设置下拉菜单的宽度
- dropDownHorizontalOffset:指定下拉菜单与文本之间的水平间距
- dropDownVerticalOffset:指定下拉菜单与文本之间的竖直间距
- dropDownSelector:设置下拉菜单点击效果
- popupBackground:设置下拉菜单的背景
2.2 实例
- 加入布局
- values/arrays.xml定义字符串数组
<string-array name="myArray">
<item>who are you</item>
<item>where are you</item>
<item>when you go</item>
<item>what are you doing</item>
<item>how are you</item>
</string-array>
- MainActivity.java
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView atv_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
atv_content = (AutoCompleteTextView) findViewById(R.id.atv_content);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.
this, android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.myArray));
atv_content.setAdapter(adapter);
}
}
- 输入w,显示4个文本,没有how
3 MultiAutoCompleteTextView(多提示项的自动完成文本框)
3.1 基本属性
与AutoCompleteTextView属性相同
3.2 实例
复用AutoCompleteTextView的实例
- 修改activity_main.xml,AutoCompleteTextView改为MultiAutoCompleteTextView
<MultiAutoCompleteTextView
android:id="@+id/atv_content"
android:layout_width="match_parent"
android:layout_height="48dp"
android:completionHint="请输入搜索内容"
android:completionThreshold="1"/>
- 修改MainActivity.java
public class MainActivity extends AppCompatActivity {
private MultiAutoCompleteTextView atv_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
atv_content = findViewById(R.id.atv_content);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.
this, android.R.layout.simple_dropdown_item_1line, getResources().getStringArray(R.array.myArray));
atv_content.setAdapter(adapter);
atv_content.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
}
- 逗号之后再输入w,同样会提示,AutoCompleteTextView不会