android高级组件使用(一)

Android  /  houtizong 发布于 3年前   105

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代码;

  

//获得idCheckBox 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();}

 

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客