003 Android之线性布局与基础控件
文章目录
- Android快速入门三步
- 布局介绍
- LinearLayout布局属性
- 代码示例
- 基础控件
- TextView和EditText
- 使用TextView与EditText
- ImageView
- ImageView属性
- 使用ImageView
- Button与ImageButton
- Button与ImageButton的使用
- RadioButton和CheckBox
- 四种按钮响应方式
- onClick事件
- 监听事件实现方式-内部类实现接口
- 监听事件实现方式-匿名内部类方式
- 监听事件实现方式-外部类方式
- 电话本小例子
- 界面设计
- 完成打电话功能
- 完成短信发送功能
- SmsManager
Android快速入门三步
- 画界面-操作的是资源(布局 字符串 控件等)
- 响应界面(写代码)-操作的是Android封装的类对象
- 打包运行(编译代码生成程序)-SDK工具包
布局介绍
- 当界面有多个控件时,需要按照需求将他们摆放
- 布局就是用来安排他们内部控件所在位置
- 这时布局也称为容器,可以称为父控件
- 布局内部的控件被称为布局的子控件
- 一个布局也可以称为另一个布局的子控件
Android基础布局
- LinearLayout(线性布局) 将内容按照一列或者一行摆放
- RelativeLayout(相对) 子控件相互依赖而决定位置
- FrameLayout(帧布局)默认不做加工
- TableLayout(表格) 包含若干列若干行组成
- GridLayout(网格) 如同单元格一样
- ConstraintLayout(约束布局)
对于安卓开发来说,以上的所有布局都需要能熟练掌握,但是对于安卓逆向来说,只需要会最简单的LinearLayout就足够了。
LinearLayout布局属性
设置方向
android:orientation="horizontal" 设置方向,horizontal代表水平,vertical代表垂直,默认值horizontal
设置宽度和高度
属性:layout_width="wrap_content" 内容多大就是多大 match_parent 与父控件一致
属性:layout_height="wrap_content" 内容多大就是多大 match_parent 与父控件一致
设置控件位置
android:gravity="right"
设置自身在父布局的权重
android:layout_weight="1"
内边距和外边距
padding指的是内边距:控件内部内容与控件的边距
margin指的是外边距 控件与子控件的边距
代码示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><Buttonandroid:id="@+id/btn1"android:text="开始游戏"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn2"android:text="游戏设置"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn3"android:text="结束游戏"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>
实际效果如图:
基础控件
TextView和EditText
TextView常见属性
属性 | 说明 |
---|---|
android:id | 控件的ID |
android:layout_width | 控件宽度 |
android:layout_height | 控件的高度 |
android:text | 文本内容 |
android:textSize | 文本大小 |
android:textColor | 文本颜色 |
android:background | 控件背景 |
EditText常见属性
属性 | 说明 |
---|---|
android:id | 控件的ID |
android:layout_width | 控件宽度 |
android:layout_height | 控件的高度 |
android:text | 文本内容 |
android:textSize | 文本大小 |
android:textColor | 文本颜色 |
android:background | 控件背景 |
android:hint | 输入提示的文本 |
android:inputType | 输入文本类型 |
使用TextView与EditText
- 新建一个工程
- 删除默认生成的activety_main.xml中的内容
- 新添加一个layout
- 新添加一个TextView和EditText并设置属性,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:orientation="horizontal"android:layout_margin="5dp"android:layout_height="match_parent"><TextViewandroid:id="@+id/tv1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="牛人:"android:textSize="18dp" /><EditTextandroid:id="@+id/edt1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="请在此输入您的大名"/></LinearLayout>
- 将入口类中默认加载的layout修改为新建的layout
实际效果如图:
ImageView
ImageView属性
属性 | 说明 |
---|---|
android:src="@drawable/ic_launcher" | ImageView的内容图像 |
android:background="@drawable/ic_launcher" | ImageView背景图片 |
android:background="#0000ff" | ImageView的RBG颜色 |
使用ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:layout_gravity="center"android:src="@mipmap/ic_launcher"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ImageViewandroid:layout_gravity="center"android:src="@mipmap/ic_launcher"android:background="@drawable/3"android:layout_width="wrap_content"android:layout_height="wrap_content" />
</LinearLayout>
Button与ImageButton
Button与ImageButton特征
共有特征:都可以作为一个按钮产生点击事件
不同点:Button有text属性,ImageButton没有;ImageButton有src属性,Button没有
Button与ImageButton的使用
<Buttonandroid:id="@+id/btn1"android:text="我是一个按钮"android:layout_width="match_parent"android:layout_height="wrap_content" /><ImageButtonandroid:src="@drawable/1"android:layout_width="match_parent"android:layout_height="wrap_content"/>
RadioButton和CheckBox
代码示例:
<RadioGroupandroid:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/rbtn1"android:text="单选框1"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:id="@+id/rbtn2"android:text="单选框2"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RadioGroup><CheckBoxandroid:id="@+id/cbtn1"android:text="复选框"android:layout_width="wrap_content"android:layout_height="wrap_content" />
响应事件
RadioButton rbtn1=findViewById(R.id.rbtn1);//rbtn1.setChecked(true);rbtn1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {Toast.makeText(MainActivity.this,"单选框改变",Toast.LENGTH_LONG).show();}});CheckBox chbox=findViewById(R.id.cbtn1);chbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {Toast.makeText(MainActivity.this,"复选框改变",Toast.LENGTH_LONG).show();}
四种按钮响应方式
了解了基础控件的使用,接下来还需要掌握四种按钮的响应事件
onClick事件
<Buttonandroid:id="@+id/btn1"android:onClick="onClick1"android:text="我是一个按钮"android:layout_width="match_parent"android:layout_height="wrap_content" />
添加一个onClick属性,绑定onClick1方法
按Alt+Enter键
会自动在MainAvtivity中创建响应事件,在函数里面编写响应代码即可
监听事件实现方式-内部类实现接口
首先创建按钮控件
<Buttonandroid:id="@+id/btn2"android:text="按钮2"android:layout_width="match_parent"android:layout_height="wrap_content" />
接着在入口类中编写代码,首先获取按钮对象
Button btn2=findViewById(R.id.btn2);
然后设置监听事件
//1.2 设置监听事件//参数是一个接口 传入的参数应该是实现了这个接口的对象btn2.setOnClickListener();
由于setOnClickListener的参数是一个接口,所以我们需要一个实现了这个接口的对象
创建类,实现View.OnClickListener接口
public class MyOnClickListener implements View.OnClickListener{@Overridepublic void onClick(View view) {//这里是响应代码Toast toast=Toast.makeText(MainActivity.this,"按钮2",Toast.LENGTH_LONG);}}
最后传入实现了接口的对象
btn2.setOnClickListener(new MyOnClickListener());
监听事件实现方式-匿名内部类方式
首先获取控件对象
Button btn3=findViewById(R.id.btn3);
接着设置监听事件
btn3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Toast toast=Toast.makeText(MainActivity.this,"按钮3",Toast.LENGTH_LONG);toast.show();}});
由于监听事件为匿名类,所以可以直接在括号内写实现代码
监听事件实现方式-外部类方式
获取按钮对象并设置响应事件
//3.外部类方式Button btn4=findViewById(R.id.btn4);btn4.setOnClickListener(this);
然后重写Onclick方法
@Overridepublic void onClick(View view) {}
即可绑定按钮事件
电话本小例子
界面设计
接着我们通过一个电话本的例子,深刻了解整个安卓开发过程。首先设置打电话的一个文本框和按钮
//文本框<EditTextandroid:id="@+id/edit1"android:inputType="phone"android:hint="请在此输入电话号码"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn1"android:text="打电话"android:onClick="OnCall"android:layout_width="match_parent"android:layout_height="wrap_content" />
接着设置发短信的文本框和按钮
<EditTextandroid:id="@+id/edit2"android:inputType="textMultiLine"android:minLines="6"android:layout_width="match_parent"android:layout_height="wrap_content" /><Buttonandroid:id="@+id/btn2"android:text="发短信"android:onClick="OnSendMsg"android:layout_width="match_parent"android:layout_height="wrap_content" />
并分别给两个按钮绑定OnClick事件,界面效果如图:
完成打电话功能
实现思路:
接着完成打电话的OnClick函数。程序本身无法做到打电话这个功能,需要向系统发出请求。android中与其他应用打交道,需要用到Intent。打电话这个操作,本质上就是与系统中的电话应用进行通信。
我们需要做的就是使用Intent对象完成打电话这个动作,并且将电话号码发送给电话应用,具体步骤如下:
- 创建一个Intent对象
- 设置打电话动作
- 打包数据,放入Intent
- 根据Intent对象启动对应的Activety
代码如下:
public void OnCall(View view) {//获取电话号码EditText text=findViewById(R.id.edit1);String phonenum=text.getText().toString();//创建Intent对象Intent intent=new Intent();//设置打电话动作intent.setAction(Intent.ACTION_CALL);//打包数据 放入IntentUri uri=Uri.parse("tel:"+phonenum);intent.setData(uri);//根据Intent启动ActivitystartActivity(intent);}
接着在Manifest.xml文件中加上电话权限相关代码
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
这样就完成了打电话的完整功能。
完成短信发送功能
实现思路:
编写短信发送和电话拨号有所不同,Android SDK提供了短信发送器用于操作短信
SmsManager类用于管理发送短信。
有了这个知识之后,我们可以把短信发送器分为几步:
- 设置按钮监听器,在监听器中调用发短信方法
- 发短信分为
- 获取号码和文本内容
- 获取短信管理器对象
- 拆分文本内容
- 发送短信
需要注意的是,发送短信也需要权限
SmsManager
SmsManager用于管理短信,发送短信,获取这个类的对象需要调用类的静态成员方法getDefault()
SmsManager类中其他几个方法:
方法名 | 功能 |
---|---|
devideMessage(String text) | 拆分短信,一条短信最大70个中文 |
getDefault() | 获取短信管理器对象 |
sendMultipartTextMessage | 发送多条短信 |
sendTextMessage | 发送短信 |
完成短信发送步骤如下:
- 首先获取电话号码和短信内容
//获取电话号码EditText text=findViewById(R.id.edit1);String phonenum=text.getText().toString();//获取短信内容EditText text2=findViewById(R.id.edit2);String content=text2.getText().toString();
- 获取SMS管理器对象
//获取SMS管理器对象
SmsManager sms=SmsManager.getDefault();
- 拆分短信
//拆分短信
ArrayList<String> str=sms.divideMessage(content);
- 发送短信
//发送短信
sms.sendMultipartTextMessage(phonenum,null,str,null,null);
- 获取权限
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
这样就完成了发短信的功能了,实际效果如图:
003 Android之线性布局与基础控件相关推荐
- Android Studio 线性布局Linearlayout的控件位置控制l属性Layout_margin失效问题解决
Android Studio 线性布局Linearlayout的控件位置控制l属性Layout_margin失效问题解决 问题:如layout_marginRight ="50dp" ...
- Android设置text按钮,安卓基础控件使用(TextView、Button、ImageView、EditText)
一.文本控件TextView 1.布局文件 android:text="@string/content" android:layout_width="wrap_conte ...
- Android CardView卡片布局 标签: 控件
CardView介绍 CardView是Android 5.0系统引入的控件,相当于FragmentLayout布局控件然后添加圆角及阴影的效果:CardView被包装为一种布局,并且经常在ListV ...
- Android自定义组合布局,Android 流式布局 + 自定义组合控件
自定义组合控件 package yanjupeng.bawei.com.day09.two; import android.content.Context; import android.util.A ...
- 【Android】线性布局(LinearLayout)最全解析
[Android]线性布局(LinearLayout)最全解析 一.LinearLayout概述 二.LinearLayout常用属性 2.1 orientation属性 2.2 gravity属性 ...
- Android中的基础控件TextView、Button、ImageView、EditText、ProgressBar
文章目录 1 Android中的基础控件 1.1 控件的通用属性 2 TextView 2.1 TextView的继承关系 2.2 TextView的常用属性 3 EditText 3.1 常用属性 ...
- Android:基础控件按键文本框输入框制作登录页面
基础控件之Button,TextView,EditText,ImageView Button:按键 TextView:文本框 EditText:输入框 ImageView:图片 那我们新建一个工程研究 ...
- android 3d布局轮播,android 图片/视频混合轮播控件banner
android 图片/视频混合轮播控件banner 在youth5201314的图片轮播控件做的修改 原作者github地址:https://github.com/youth5201314/banne ...
- android studio 画控件,Android Studio 基础控件使用
TextView android:gravity="center" //文字对其方式 top bottom left right center android:textColor= ...
最新文章
- Docker 学习资源整理
- 基带模型下基于高阶累积量的MFSK信号的调制识别(代码和参考文献见CSDN同名资源)
- 反沙箱——SetErrorMode
- python计算结果传给spark_Spark入门:流计算简介(Python版)
- Xavier上的第一个I2C驱动
- python怎么开始打代码_Python代码写好了怎么运行?
- jango web开发指南_Web前端“月薪过万”必读的一些入门书籍和网站!
- 支持Linux系统恢复工具,12款Linux系统恢复工具
- 存储过程和存储函数和触发器示例
- Linux中更换软件源以及更新软件过程中报错的解决方法
- vscode英文界面转换为中文界面
- 乐高创意机器人moc_深度乐高 篇一:大神带你玩转乐高 Speed 系列套内 MOC
- 学生个人消费系统设计C语言,个人消费管理系统C语言课程设计毕业设计word格式.doc...
- 88年计算机病毒,读短文回答问题电脑遭病毒侵袭的事件早在1988年就发生过。那时,美国的一个大学把一种叫做“蠕虫”的电脑病毒输入电脑,由于电脑往往是许多部门许多台联在一起而形成网-组卷网...
- 基于yolov5轻量级的学生上课姿势检测识别分析系统
- SSH安全策略–指定IP登陆
- for循环实现的一些例子
- jsp报错500解决办法
- 渗透工程师日常探测漏洞全流程 初学者必看
- 孙鑫Lesson2 grammer
热门文章
- 机器学习实战-数据探索(变量变换、生成)
- 7.1 TensorFlow笔记(基础篇):加载数据之预加载数据与填充数据
- hdu 3790 最短路径dijkstra(多重权值)
- ubuntu QT Creater 安装
- 细说Vue作用域插槽,匹配应用场景。
- G面经prepare: Set Intersection Set Difference
- 《英语语法新思维初级教程》学习笔记(二)名词
- Log4j配置文件位置+Spring数据源配置文件位置
- 显示并查询纯真官方最新IP地址的免费代码
- _stdcall调用