1、自动完成文本框(AutoCompleteTextView)
AutoCompleteTextView从EditText派生出来,实际上也是一个文本编辑框,但它比普通编辑框多一个功能:当用户输入一个字符后,自动完成文本框会显示一个下拉菜单,供用户从中选择,当用户选择某个菜单项之后,AutoCompleteTextView按用户选择自动填写该文本框。
使用AutoCompleteTextView很简单,只要为它设置一个Adapter,该Adapter封装了AutoCompleteTextView预设的提示文本。
xml代码;
<AutoCompleteTextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android代码:
// 获得下拉框对象
AutoCompleteTextView textview = (AutoCompleteTextView) this
.findViewById(R.id.textView);
//下拉框对象需要一个数据源,使用list队列添加数据
String[] array = { "设置", "wlan", "移动网络", "声音","显示","存储","电池","引用程序" };
/*第一个:上下文对象
第二个:布局的样式
第三个:数据*/
ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,array );
textview.setAdapter(arrayAdapter);
2、Spinner
Spinner其实就是一个列表选择框。不过Android的列表选择框不是需要显示下拉列表的,而是相当于弹出一个菜单供用户选择。
Spinner是ViewGroup的间接子类,因此它可以作为容器使用。
使用Spinner时已经可以确定下拉列表框里的列表项,则完全不需要编写代码,只要为Spinner指定android:entries属性既可实现一个下拉列表框。
对Spinner而言,它也许需要显示一个供用户选择的列表----至于这个列表是以数组的形式给出,还是以其它什么形式并不重要,重要的是Spinner知道它的每项应该显示什么。
为了让Spinner知道每项应该显示什么,我们可以提供一个内容Adapter。这个Adapter负责决定Spinner列表的每项显
xml代码;
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android代码:
// 获得下拉框对象
Spinner spinner = (Spinner) this.findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("网络");
list.add("zhangsanfeng");
list.add("zhang");
list.add("zhuo");
//使用List作为数据源
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, list);
spinner.setAdapter(adapter1);
3,星级评分条(RatingBar)
常用的XML属性如下:
android:isIndicator:设置该星级评分条是否允许用户改变(true为不允许修改)
android:numStars:设置星级评分条有多少个星级
android:rating:设置星级评分条的默认星级
android:stepSize:设置每次最少需要改变多少个星级
xml代码;
<RatingBar
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:numStars="5"
android:rating="3"
android:stepSize="0.5" />
android代码:
RatingBar rating = (RatingBar) this.findViewById(R.id.score);
float ret=rating.getRating();//获得评分
4,RadioGroup和RadioButton
xml代码;
<RadioGroup
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/men"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/women"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="女" />
</RadioGroup>
分别使用按钮和和按钮获得性别;两个方法获取
方法一; 按钮组获取代码;使用按钮组的监听器监听按下的按钮
RadioButton man = (RadioButton) this.findViewById(R.id.men);
RadioButton woman = (RadioButton) this.findViewById(R.id.women);
// 获得按钮组的,并添加监听器
group = (RadioGroup) this.findViewById(R.id.group);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
String s = "";
if (checkedId == R.id.men) {
s = "男";
} else if (checkedId == R.id.women) {
s = "女";
}
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
});
方法二;使用按钮监听器获得;
reg是注册按钮添加的监听器
RadioButton man = (RadioButton) this.findViewById(R.id.men);
RadioButton woman = (RadioButton) this.findViewById(R.id.women);
//按钮监听器
public void reg(View v){
String sex="男";
if(man.isChecked()){
sex="男";
}else if(woman.isChecked()){
sex="女";
}
}
5,CheckBox的使用
xml代码;
<CheckBox
android:id="@+id/sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动" />
<CheckBox
android:id="@+id/sing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="唱歌" />
android代码;
//获得id
CheckBox sport = (CheckBox) this.findViewById(R.id.sport);
CheckBox sing = (CheckBox) this.findViewById(R.id.sing);
//判断是否选中
if(sport.isChecked()){
str=str+": "+sport.getText();
}
if(sing.isChecked()){
str=str+" :"+sing.getText();
}