Android常用控件

来源:互联网 发布:淘宝确认收货前退货 编辑:程序博客网 时间:2024/05/17 05:58

文本类控件

TextView--负责展示文本,非编辑
EditText--可编辑文本控件

按钮类控件

Button--按钮
ImageButton--图片按钮
RadioButton与RadioGroup--单选按钮
CheckBox--复选按钮

图片控件

ImageView--负责显示图片
进度条控件

ProgressBar--进度条

1.文本类控件TextView

TextView是 Android 程序开发中最常用的控件之一,主要功能是向用户展示文本的内容,它是不可编辑的 ,

只能通过初始化设置或在程序中修改。属性介绍:


2.文本类控件EditText

相比TextView, EditText是可以编辑的,可以用来与用户进行交互,其用法和TextView也是类似的。属性介绍:


3.按钮类控件Button

Button控件也是使用过程中用的最多的控件之一,所以需要好好掌握。用户可以通过单击 Button 来触发一系列事件,然后为 Button 注册监听器,

来实现 Button 的监听事件。属性介绍:


4.按钮类控件ImageButton

ImageButton和Button类似,是一个按钮,ImageButton可以实现我们任何想要的图片按钮的效果,比如我们租一个下载的按钮等等。

它要比button实现的要好看,并且体验要好很多, 不过它是以图片作为背景,没有文字。利用属性android:src="图片位置"来设置图片背景。

属性介绍:


5.按钮类控件RadioButton与RadioGroup

RadioButton(单选按钮)在 Android 平台上也比较常用,比如一些选择项会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在 RadioButton 没有 被选中时,用户通过单击来选中它。但是,在选中后,无法通过单击取消选中。

RadioGroup 是单选组合框,用于 将 RadioButton 框起来。在多个 RadioButton被 RadioGroup 包含的情况下,同一时刻只可以选择一个 RadioButton,并用 setOnCheckedChangeListener 来对 RadioGroup 进行监听。

属性介绍:

 <RadioGroup        android:id="@+id/radio_group"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        //设置RadioButton的排列方式,分为水平排列horizontal与垂直排列vertical        android:orientation="horizontal" >           <RadioButton                android:id="@+id/rd1"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               //设置单选后紧跟的文本提示文字               android:text="吃饭"               //设置文字的大小               android:textSize="25sp"               //设置文字的颜色               android:textColor="#0000ff"               //字体格式               android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal                />           <RadioButton                android:id="@+id/rd2"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:textSize="25sp"               android:text="睡觉" />      </RadioGroup>

6.按钮类控件CheckBox

CheckBox(复选按钮),顾名思义是一种可以进行多选的按钮,默认以矩形表示。与 RadioButton 相同,它也有选中或者不选中双状态。我们可以先在布局

文件中定义多选按钮, 然后对每一个多选按钮进行事件监听 setOnCheckedChangeListener,通过 isChecked 来判断 选项是否被选中,做出相应的事件响应。

<CheckBox     android:id="@+id/cb1"    android:layout_width="match_parent"    android:layout_height="wrap_content"    //设置复选按钮后紧跟的文本提示文字    android:text="吃饭"    //设置文字的大小    android:textSize="25sp"    //设置文字的颜色    android:textColor="#0000ff"    //字体格式    android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal/><CheckBox     android:id="@+id/cb2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="睡觉"    android:textSize="25sp"    android:textColor="#0000FF"/>


7.图片控件ImageView

ImageView 是一个图片控件,负责显示图片,图片的来源可以是系统提供的资源文件,也可以是 Drawable 对象,相对来说,图片空间还是比较好掌握的,

因为前面有讲过ImageButton, 很多属性都是相同的。


8.进度条控件ProgressBar

ProgressBar 用于在界面上显示一个进度条,表示我们的程序正在加载一些数据,运行程序,会看到屏幕中有一个圆形进度条正在加载。

<ProgressBar     android:id="@+id/pb_progressbar_bar"    android:layout_width="match_parent"    android:layout_height="wrap_content"    //默认是圆形进度条,可以知道样式设置为水平进度条    style="?android:attr/progressBarStyleHorizontal"/>    //指定成水平进度条后,我们还可以通过 android:max属性给进度条设置一个最大值,然后在代码中动态地更改进度条的进度    android:max="100"    />



原创粉丝点击