Android学习之文本编辑控件的使用
在android开发中我们是要为用户提供一些区域让用于可以输入属于自己的一些个人信息的,最经常看到的就是咱们平时登录、注册时,用户输入自己的个人信息及账号和密码,android中提供了一个非常简单易用的控件,就是EditText,将单词拆开来读就是可编辑的文本框。
我们可以用EditText和TextView来使用我们已经学习过的相对布局(RelativeLayout)来完成用户的登录注册页面。
我们如果想使用EditText这个控件,首先就要把它写到我们的布局文件当中activity_main.xml当前代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/edit_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入您的账号"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
在当前代码中我们为EditText设置了id为edit_user,宽高的值为wrap_content即内容包裹,也就是控件会随着内容的大小多少而变化。android:hint="请输入您的账号"这段代码的作用是,展示用户提示信息,当用户选用EditText进行文本编辑时,"请输入您的账号"这段文本内容将消失。左图为用户未开始编辑时控件显示效果,右图为开始编辑。如图:
但是到了这一步还不够,我们自己用的应用,可编辑的文本框下面是没有一条蓝色的线条的,在展示给用户的时候我们如何让蓝色线条消失呢?很简单,利用EditText的属性background来引用一个xml文件。这个XML可以让EditText蓝色底部边框消失,即自定义EditText的边框:
选中Drawable目录新建xml file,如图:
选择shape选项:在Flie栏目将文件名称命名为edit_bg点击Finish按钮一个名为edit_bg.xml的文件就会创建。文件代码:可通过百度搜索关键词:EditText边框 得到。(学习学的不是死的知识而是方法)
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- 填充的颜色:这里设置背景透明 -->
<solid android:color="@android:color/transparent" />
<!-- 边框的颜色 :不能和窗口背景色一样-->
<stroke
android:width="3dp"
android:color="#ffffff" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="5dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
接下来activity_main.xml代码更新如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/edit_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/eidt_bg"
android:hint="请输入您的账号" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
这时EditText控件不管事最初展示,还是用户选中编辑时都不会有蓝色底部边框,我们甚至还可对edit_bg.xml代码进行更改让EditText拥有不同的边框。
接下来,我们来实现下,一开始说说的登录注册的ui界面。
通过已学只是完成如图所示界面效果:
代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33DEDBD7"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@drawable/relative_bg" >
<EditText
android:id="@+id/edit_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/eidt_bg"
android:ems="10"
android:hint="手机号码" >
</EditText>
<EditText
android:id="@+id/edit_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/edit_user"
android:background="@drawable/eidt_bg"
android:ems="10"
android:hint="密码" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignLeft="@+id/edit_user"
android:layout_below="@+id/edit_user"
android:background="#DEDBD7" />
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignBottom="@+id/edit_pass"
android:layout_alignParentLeft="true"
android:background="#DEDBD7" />
<ImageView
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView01"
android:layout_below="@+id/edit_pass"
android:src="@drawable/login" />
</RelativeLayout>
</RelativeLayout>
EditText常用属性如下:
SingleLines限定单行
Numric限定接受的数字类型
Android学习之文本编辑控件的使用相关推荐
- Android学习:常用控件
Android学习:常用控件 学习要点:掌握文本框.编辑框.按钮.单选按钮.复选框等基本控件的用法 控件是Android应用程序中用户界面的重要组成元素,正是因为Android应用程序提供了很多具有不 ...
- Android学习之Textview控件字体颜色设置及颜色色值
在Textview中设置字体的颜色和大小共有两种方式: 1.在.xml中设置 android:text="这里是文字" android:textSize="字体大小&qu ...
- 在应用了皮肤的程序中制作透明的文本编辑控件(如:TcxMemo)
我要在程序中显示一段信息,因为需要让文字的背景透明,我首先使用静态文本控件来实现 遇到需要进行编辑的时候,静态文本控件就不能满足要求了,这就使我想到了可不可以制作一个透明的文本编辑控件 首先上网查了一 ...
- Android游戏开发系统控件-CheckBox
Android游戏开发系统控件-CheckBox 2012/5/11 星期五 CheckBox是Android系统最普通的UI控件,继承了Button按钮 下面通过一个实例来学习 作者:wwj 功能: ...
- Android游戏开发系统控件-Dialog
Android游戏开发系统控件-Dialog Dialog(对话框)在Android应用开发中经常用到,下面是学习<Android游戏编程从零开始>一书,关于Dialog的初步学习. 创建 ...
- 【Android】自己定义控件——仿天猫Indicator
今天来说说类似天猫的Banner中的小圆点是怎么做的(图中绿圈部分) 在学习自己定义控件之前,我用的是很二的方法,直接在布局中放入多个ImageView,然后代码中依据Pager切换来改变图片.这样的 ...
- android自定义刻度线,Android自定义控件之刻度尺控件
今天我做的是一个自定义刻度尺控件,由于项目需求需要使用刻度尺那样滑动选择,由于对自定义控件的认识还不够深入,于是花了一周多时间才把这个控件给整出来,也是呕心沥血的经历啊,也让我对自定义控件有了自己的认 ...
- 视频教程-Android Material Design 新控件-Android
Android Material Design 新控件 刘志远,北京邮电大学硕士研究生, 北京育华志远科技有限公司创始人, 育华志远教育品牌负责人,育华志远课程体系打造者. 率领团队为互联网行业培训千 ...
- android线性布局设置控件固定在底部,Android UI组件LinearLayout线性布局详解
LinearLayout 线性布局,该布局的继承关系: 1. 什么是线性布局 通俗的说感觉起来和线有关,参照线的特点,有么是横向的,要么是竖向的. LinearLayout是线性布局控件,它包含的子控 ...
- android什么控件能够输入多行文字,Android开发:文本控件详解——EditText(一)基本属性...
一.简单实例: EditText输入的文字样式部分的属性,基本都是和TextView中的属性一样. 除此之外,EditText还有自己独有的属性. 二.基本属性: hint 输入框显示的提示文本 te ...
最新文章
- KDD'20 Workshop 来了!学术竞赛,发表Paper,针对疫情设计隔离策略
- fork()与pid
- T511K表之工资常量SIINS的用途
- 认仕医生接入云信,医友交流随时随地
- npm安装报错Error: EPERM: operation not permitted解决方案
- java8默认内存收集器_使用正确的垃圾收集器将Java内存使用量降至最低
- raspberry pi_使用Raspberry Pi和pi-hole阻止网络上的广告
- python 保留的类标识符
- 一题多解 —— 同时找到序列的最大值最小值
- 通过phpmyadmin确认此版本的php支持mssql函数,Php5.6.4+IIS8.5+mysql5.6.22+phpmyadmin4.3.6的配置...
- 配置多个git账号_Git ssh配置(Mac)
- 拓端tecdat|R语言Bass模型进行销售预测
- 大学课程 | 《微机原理与接口技术》知识点总结
- 天地图矢量mxd版标准地图
- CI24R1低成本国产2.4G无线收发器芯片替代XN297精简版SI24R1
- python处理wps表格数据匹配_wps2个excel表格数据匹配-Wps表格怎么自动匹配我需要的数值...
- SQL 常用英文单词
- Batch Normalization论文翻译——中英文对照
- 若依分离版部署遇到的问题
- 基于JSP的酒店登记预定系统的设计与实现
热门文章
- 外卖点餐列表滑动 微信小程序_点餐小程序购物车效果实现,点餐小程序购物车列表的实现...
- 创建Vue实例对象基础语法模板
- SpringBoot 静态资源版本管理
- 宏观经济的基本指标及其衡量
- RIP、OSPF、ISIS协议的区别
- Windows10系统删除文件夹“找不到该项目”强删方法
- testbed常见问题及处理(1)
- 【NLP】形式语言与自动机
- 中科院2020计算机sci分区,2020年中科院最新SCI分区表.pdf
- intel h61 linux驱动下载,intel h61主板驱动