安卓基础回顾3:常用控件

安卓常用的控件

1.TextView

• 显示文字。一般用来文本展示,继承自android.view.View,在android.widget包中。

常用属性设置见下图:

这里写图片描述

属性名称说明
android:text=“”文字显示
android:autoLink=””链接类型。Web网址,email邮件,phone电话,map地图。Linkify。
android:hint=”请输入数字!”当TextView中显示的内容为空时,显示该文本
android:textColor = “#ff8c00”字体颜色
android:textSize=”20dip”字体大小
android:layout_gravity=”center_vertical”设置控件显示的位置:默认top,这里居中显示,还有bottom

代码:

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:Layout_height="wrap_content"
    android:text="This is TextView"/>

2.EditText

输入框,可编辑,可设置软键盘方式。继承自android.widget.TextView,在android.widget包中。

• 常用属性设置:

属性名称说明
android:hint=”请输入用户名”输入框的提示文字
android:password=” “密码框
android:phoneNumber=”“True为电话框
android:digits设置允许输入哪些字符。如“1234567890.+-*/%\n()”
android:numeric=”“数字框。Integer正整数, signed整数(可带负号), decimal浮点数。

代码:

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:Layout_height="wrap_content"
    android:text="This is TextView"/>

3.Button与ImageButton**

Button是最常用的按钮,继承自android.widget.TextView,在android.widget包中。他的常用子类CheckBox,RadioButton, ToggleButton。

通常用法:

• super.findViewById(id)得到在layout中声明的Button的引用,setOnClickListener(View.OnClickListener)添加监听。然后再View.OnClickListener监听器中使用v.equals(View)方法判断哪个按钮被按下,进行分别处理。

ImageButton继承自ImageView类,与Button之间的最大区别在于ImageButton中没有text属性。ImageButton控件中设置按钮中显示的图片可以通过android:src属性来设置。也可以通过setImageResource(int)来设置。

代码:

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:Layout_height="wrap_content"
    android:text="Button"
    android:textAllCaps="false"/>

4、ImageView—图片控件

ImageView控件负责显示图片,其图片的来源可以是在资源文件中的id,也可以是Drawable对象或者位图对象。还可以是Content Provider的URI。

常用属性:

属性名称说明
Android:adjustViewBounds设置是否需要ImageView调整自己的边界,保证图片的显示比例
Android:maxHeight最大高度
Android:maxWidth最大宽度
Android:src图片路径
Android:scaleType调整或移动图片
<ImageView
    android:id="@+id/image_view"
    android:layout_width="match_parent"
    android:Layout_height="wrap_content"
    android:src="@drawable/img"/>

5.ProgressBar

在用户界面显示一个进度条

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="match_parent"
    android:Layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    android:max="100"/>

style属性可以设置进度条样式
android:max属性设置进度条最大值

6.RadioButton和CheckBox–单选与复选按钮

android.widget.CheckBox复选按钮,继承自android.widget.CompoundButton,在android.widget包中。

常用方法:

isChecked()检查是否被选中。

监听按钮状态更改,需要添加setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener);

android.widget. RadioButton单选按钮,继承自android.widget.CompoundButton,在android.widget包中。

通常用法:
单选按钮要声明在RadioGroup,RadioGroup是流式布局android.widget.LinearLayout的子类。

<CheckBox 
    android:id="@+id/cb1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="北京"
    android:textSize="30sp"
    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="30sp"
    android:textColor="#0000FF"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值