android基本控件学习-----EditText
EditText的讲解
一、《实例一》:用户登录
<?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"android:background="#ffffff"android:gravity="center"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/txt_radiuborder"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:orientation="vertical"android:paddingLeft="20dp"android:paddingRight="20dp"><EditTextandroid:id="@+id/edit1"android:layout_width="match_parent"android:layout_height="100dp"android:drawableLeft="@mipmap/ic_account"android:drawablePadding="60px"android:padding="5px"android:singleLine="true"android:hint="用户名"android:textColorHint="#98f5ff"android:textSize="30sp"/><EditTextandroid:id="@+id/edit2"android:layout_width="match_parent"android:layout_height="100dp"android:drawableLeft="@mipmap/ic_password"android:drawablePadding="60px"android:padding="5px"android:singleLine="true"android:hint="密码"android:textColorHint="#98f5ff"android:textSize="30sp"/></LinearLayout><Buttonandroid:layout_width="match_parent"android:layout_height="80dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="30dp"android:text="登录"android:textSize="30sp"android:textColor="#000000"android:textStyle="bold"android:background="@drawable/txt_radiuborder"/> </LinearLayout>
属性说明:很多属性和TextView相同就不做过多的解释,这里就对这两个属性进行说明:
android:hint="默认提示文本" android:textColorHint="#95A1AA",第一个属性设置提示信息,第二个属性设置提示信息的颜色
二、常用的属性详解
(1)获得焦点后选择所有的文本内容android:selectAllOnFocus="true"
(2)限制EditText的输入类型android:inputType="phone"
类型描述:文本型
android:inputType="none" android:inputType="text" android:inputType="textCapCharacters" android:inputType="textCapWords" android:inputType="textCapSentences" android:inputType="textAutoCorrect" android:inputType="textAutoComplete" android:inputType="textMultiLine" android:inputType="textImeMultiLine" android:inputType="textNoSuggestions" android:inputType="textUri" android:inputType="textEmailAddress" android:inputType="textEmailSubject" android:inputType="textShortMessage" android:inputType="textLongMessage" android:inputType="textPersonName" android:inputType="textPostalAddress" android:inputType="textPassword" android:inputType="textVisiblePassword" android:inputType="textWebEditText" android:inputType="textFilter" android:inputType="textPhonetic"数字型:android:inputType="number" android:inputType="numberSigned" android:inputType="numberDecimal" android:inputType="phone"//拨号键盘 android:inputType="datetime" android:inputType="date"//日期键盘 android:inputType="time"//时间键盘(3)设置最小行,最大行,单行,多行,自动换行最小行的行数:android:minLines="3"最大的行数:android:maxLines="3"设置字与字的水平间隔:android:textScaleX="1.5" 设置字与字的垂直间隔:android:textScaleY="1.5"设置英文字母大写类型的属性:android:capitalize 默认none,还有三个可选项
- sentences:仅第一个字母大写
- words:每一个单词首字母大小,用空格区分单词
- characters:每一个英文字母都大写
(4)EditText四周的间隔距离与内部文字与边框间的距离:和TextView一样padding
(5)EditText的光标控制:在Java代码中使用setSelection(int index)或者setSelection(int start,int end),其中一个参数的是设置光标位置的,两个参数的是设置起始位置与结束位置的中间括的部分,即部分选中!
当然我们也可以调用setSelectAllOnFocus(true);让EditText获得焦点时选中全部文本!
另外我们还可以调用setCursorVisible(false);设置光标不显示
还可以调用getSelectionStart()和getSelectionEnd获得当前光标的前后位置
三、实例二:带有表情文本输入框
布局文件
<?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"android:background="#ffffff"android:gravity="center"><Buttonandroid:id="@+id/btn1"android:layout_width="match_parent"android:layout_height="80dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="30dp"android:text="添加表情"android:textSize="30sp"android:textColor="#000000"android:textStyle="bold"android:background="@drawable/txt_radiuborder"/><EditTextandroid:id="@+id/edit1"android:layout_width="match_parent"android:layout_height="100dp"android:padding="5px"android:singleLine="true"/></LinearLayout>
Java文件:
import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; import android.text.style.ImageSpan; import android.view.View; import android.widget.Button; import android.widget.EditText;public class MainActivity extends Activity {private EditText editText1;private Button btn1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText1 = (EditText) findViewById(R.id.edit1);btn1 = (Button) findViewById(R.id.btn1); // 设置监听事件btn1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {SpannableString ss = new SpannableString("img");Drawable drawable = MainActivity.this.getResources().getDrawable(R.mipmap.ic_launcher);drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());ImageSpan imageSpan = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);ss.setSpan(imageSpan,0,3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);int cursor = editText1.getSelectionStart();editText1.getText().insert(cursor,ss);}});} }
效果图:
四、实例三:带有删除按钮的文本输入框(自定义输入框)
自定义组件的Java代码:
package com.example.test3;import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.EditText;/*** Created by coder-tu on 2016/1/3.* 自定义带有删除按钮的EditText*/ public class EditTextWithDel extends EditText {private final static String TAG = "EditTextWithDel";// 定义删除图标private Drawable drawable;private Context mContext;public EditTextWithDel(Context context) {super(context);mContext = context;init();}public EditTextWithDel(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;init();}public EditTextWithDel(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mContext = context;init();}private void init() {drawable = mContext.getResources().getDrawable(R.mipmap.delete);addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@Overridepublic void afterTextChanged(Editable editable) {setDrawable();}});setDrawable();}/*** 设置删除图片*/private void setDrawable() {if (length() < 1) {setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);} else {setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);}}/*** 处理删除事件*/@Overridepublic boolean onTouchEvent(MotionEvent event) {if(drawable != null && event.getAction() == MotionEvent.ACTION_UP){int eventX = (int) event.getRawX();int eventY = (int) event.getRawY();Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);Rect rect = new Rect();getGlobalVisibleRect(rect);rect.left = rect.right - 100;if (rect.contains(eventX, eventY))setText("");}return super.onTouchEvent(event);}@Overrideprotected void finalize() throws Throwable {super.finalize();} }
在布局文件中使用
<?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"android:background="#ffffff"android:gravity="center"><com.example.test3.EditTextWithDelandroid:layout_width="match_parent"android:layout_height="50dp"android:hint="带有删除按钮的EditText"/></LinearLayout>
效果图:
转载于:https://www.cnblogs.com/huolan/p/5095659.html
android基本控件学习-----EditText相关推荐
- Android学习--02(猜猜我的星座App源码+Android常用控件TextView+EditText+Button+ImangeView+DatePicker+App间通信+跳转页面)
猜猜我的星座App 1 Android常用控件 1.1 TextView控件 1.1.1 简介 1.1.2属性 1.1.3 扩展属性 1.1.4 TextView的使用方法 1.1.5总结 1.2 E ...
- android基本控件学习-----ProgressBar
ProgressBar(进度条)讲解 一.常用属性和基础使用实例 (1)常用属性: android:max:进度条的最大值 android:progress:进度条已完成进度值 android:pro ...
- Android提供了哪些主要UI控件,Android必备:Android UI控件的了解与学习
因为工做须要,最近一段时间,须要进行Android App开发的学习,以前简单的进行过Android的了解,对于基本的Android环境的搭建等已经有过整理,一个Android App是由一个或多个A ...
- android的属性如何使用方法,Android第二大控件,EditText的属性和使用方法
原标题:Android第二大控件,EditText的属性和使用方法 EditText与TextView非常相似,它甚至与TextView 共用了绝大部分XML属性和方法.EditText与TextVi ...
- android 输入文本,Android控件之EditText(输入文本框控件)
一.EditText控件概述 EditText是一个非常重要的组件,可以说他是用户和Android应用进行数据传输窗口 有了他就等于有了一扇和Android应用传输的门,通过他用户可以把数据传输给An ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
- android控件使用大全,Android常见控件使用详解
本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下 1.TextView 主要用于界面上显示一段文本信息 2.Button 用于和用户交互的一个按钮控件 //为Butt ...
- Android 原生控件之一 TextView
Android 原生控件之一 TextView 前言 来源 开始 XML属性 1.android:allowUndo 2.android:autoLink 3.android:autoSizeMaxT ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
最新文章
- CNN已老,GNN来了:重磅论文讲述深度学习的因果推理
- 中兴被禁在全球芯片市场掀起的波澜
- 四种常见的MapReduce设计模式
- 03-JavaScript
- 进程之父子进程的关系
- 徐昊:运用四色建模法进行领域分析
- ZZULIOJ 1114: 逆序
- linux使用find命令_如何在Linux中使用FIND
- 1.9 GRU 单元
- 苹果手机壳_潮牌刺绣苹果8 Plus手机壳,让爱机焕然一新
- 高程3总结#第20章JSON
- 第七次全国人口普查公报[1](第五号) ——人口年龄构成情况
- Maya2018安装报错(错误代码1612)
- is 简写 缩写_为什么e.g.是for example的缩写?它和i.e.是什么关系?
- 电脑主板详细介绍(细图!!)
- 《Loy解说Eureka服务端源码(一)》
- 猫耳FM导出音频转换为音频格式(m4a/mp3)
- MFC(CFile)文件操作
- 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(二十七)具体单板的LED驱动程序
- 你还不了解的OKRs-E是什么?
热门文章
- linux声明语言变量,C语言中用extern声明外部变量
- php中的class的用法,PHP get_class_vars() 函数用法及示例
- 前端构建工具与应用程序测试
- Python3 GUI编程: 自带图形库 tkinter 学习教程
- java.servlet js,调用servlet方法
- JavaScript数据类型之赋值运算符(10)
- a form 出口享惠情况_次磷酸8类危险品海运出口
- activiti 设置候选人_中标 | 河南移动公示无源波分复用设备集采中标候选人名单:3家厂商上榜...
- Ubuntu进入pycharm创建的虚拟环境的方法(以及如果你安装了anaconda等其它修改了环境变量的东西该怎么进)
- leetcode 93.复原IP地址 dfs解法