对Android基本控件和Activity的基本应用总结

本文详细介绍了Android开发中常见的基本控件,包括TextView、EditText、RadioGroup、RadioButton、CheckBox、Button和ImageView的使用方法和属性设置,以及Activity的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android常用的基本控件

TextView

这是一个文本控件,它主要是为了显示一些文本信息。

属性:

1.android:id(给控件设置ID,当你在Activity中需要使用到这个控件时,必须通过ID来查找这个控件)

2.android:text(文本信息,你需要该控件显示的文本内容)

3.android:textSize(设置字体大小,官方给出的单位是sp,但是我建议大家用dp,应为设置sp该文字会随着系统字体的变化而变化大小,dp则不会,可以根据具体情况来使用)

4.android:textColor(设置字体颜色,建议把颜色色值写在values下的color.xml中来进行引用)

5.android:textStyle(设置字体样式,blod(粗体),italic(斜体),normal(常规))

6.android:gravity(当textView是指定宽高或者match-parent时,用次属性来控制文字显示的位置)

7.android:drawableLeft(一般是图片或者drawable资源文件,让其在文字的左边显示)

8.android:drawableTop(一般是图片或者drawable资源文件,让其在文字的上方显示)

9.android:drawableBottom(一般是图片或者drawable资源文件,让其在文字的下方显示)

10.android:drawableRight(一般是图片或者drawable资源文件,让其在文字的右方显示)

11.android:drawablePadding(图片和文字之间的间距)

在XML文件中的代码如下:

 <TextView
 //控件id
 android:id = "@+id/xxx"  //@+id/xxx表示新增控件命名为xxx
 //宽度与高度
 android:layout_width="wrap_content"  //wrap_content或者match_parent
 android:layout_height="wrap_content"  //wrap_content或者match_parent
 //文本文字 
 android:text="@string/hello_world"//两种方式,直接具体文本或者
 引用values下面的string.xml里面的元素
 //字体大小
 android:textSize="24sp"  //以sp为单位
 //字体颜色
 android:textColor="#0000FF"  //RGB颜色
 //字体格式
 android:textStyle="normal"//normal,bold,italic分别为正常,加粗以及斜体,
 默认为normal
 //文本显示位置
 android:gravity="center"//来指定文字的对齐方式,可选值有top、bottom、left、
 right、center 等
 //是否只在一行内显示全部内容
 android:singleLine="true"  //true或者false,默认为false 
复制代码

EditText

EditText程序用于和用户进行交互的另一个重要特性,它允许用户在控件里输入和编辑内容,同样,它可配置的属性和TextView是差不多的,这里是简单列举几个常用的属性:

1.android:hint(这个属性指定了一段提示性的文本,当用户输入任何内容时,这段文本就会自动消失)

2.android:maxLine(指定EditText的最大行数为两行,这样当输入的内容超过两行时,文本就会向上滚动,而EditText则不会继续拉伸)

3.android:inputType(输入文字的限制(数字,字母,密码))

在XML文件中的代码如下:

 //控件id
 android:id = "@+id/xxx" // @+id/xxx表示新增控件命名为xxx
 //宽度与高度
 android:layout_width="wrap_content"  //wrap_content或者match_parent
 android:layout_height="wrap_content"  //wrap_content或者match_parent
 //文本文字 
 android:text="@string/hello_world"//两种方式,直接具体文本或者
 引用values下面的string.xml里面的元素
 //文本提示内容
 android:hint="hello_world"//android:text和android:hint区别是
 后者只是提示作用,真正需要输入的时候提示的内容会消失
 //字体大小
 android:textSize="24sp"  //以sp为单位
 //字体颜色
 android:textColor="#0000FF"  //RGB颜色
 //字体格式
 android:textStyle="normal"//normal,bold,italic分别为正常,加粗以及斜体,
 默认为normal
 //文本显示位置
 android:gravity="center"//来指定文字的对齐方式,可选值有top、bottom、left、right、
 center 等
 //是否只在一行内显示全部内容
 android:singleLine="true"  //true或者false,默认为false
 //输入内容设置为password类型
 android:password="true"  //输入的内容会变成······
 //输入内容设置为phoneNumber类型
 android:phoneNumber="true"  //只能输入数字
 //设定光标为显示/隐藏
 android:cursorVisible = "false" //true或者false,默认为true显示
复制代码

RadioGroup和RadioButton

RadioGroup是单选组合框,它用于将RadioButton框起来,在没有RadioGroup的情况下,RadioButton可以全部选中;而在多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个,也就实现了单选的效果。

在XML文件中的代码如下:

 <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="30sp"
 //设置文字的颜色
 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="30sp"
 android:text="上海" />
 </RadioGroup>
复制代码

单选按钮要声明在RadioGroup,是要给他的RadioGroup添加: setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)监听器。

CheckBox

CheckBox复选按钮,

isChecked() :检查是否被选中

监听状态修改,需要添加: setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener);

在XML文件中的代码如下:

 <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"/>
复制代码

Button

这是一个按钮的控件,给大家提供一个基础的按钮样式,大家可以根据属性来改变样式。

属性:

1.android:id(给控件设置ID,当你在Activity中需要使用到这个控件时,必须通过ID来查找这个控件)

2.android:background(按钮的背景颜色)

3.android:text(控件中显示的文字内容)

ImageView

这是一个显示图片的控件,图片可以是网络图片,可以是资源文件图片,所谓资源文件图片就是你把图片复制到项目的drawable或者是mipmap中来引用。

属性:

1.android:id(给控件设置ID,当你在Activity中需要使用到这个控件时,必须通过ID来查找这个控件)

2.app:srcCompat(应用资源文件来显示图片)

3.android:scaleType(这个是给图片设置显示的类型)

这些是主要的属性,还有就是宽高的设定。

Activity的基本应用总结

Activity直观理解就是手机屏幕上的一个界面,Activity主要作用是将界面呈现出来,Activity是Android系统中的四大组件之一,可以用于显示View可视控件。Activity是一个与用户交互的系统模块,几乎所有的Activity都是和用户进行交互的。交互的具体作用:一是显示;二是人机互动。

创建自己的Activity

第一步:在相应的布局文件中定义显示布局;

第二步:定义Activity类是,继承Activity,并求重写onCreate()方法

  • 找到对应的xml布局文件:setContentView(R.layout.main)
  • 通过findViewById找到相应控件对象:btn=(Button)this.findViewById(R.id.button01);

第三部:在AndroidManifest.xml中注册。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值