EditText 限制输入,自定义样式,监听输入的字符,自动换行
自动获取焦点
<!-- 添加:<requestFocus /> 会自动获取焦点 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_horizontal"
android:hint="自动获取焦点">
<requestFocus />
</EditText>
限制输入的字符
<!-- android:digits="1234567890.+-*/%\n()" 限制输入的字符类型 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="只能输入1234567890.+-*/%\n()"
android:digits="1234567890.+-*/%\n()" />
<!-- android:phoneNumber="true"被inputType替换了,现在用inputType来限制输入字符的类型 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="限制输入的字符类型为numberPassword"
android:inputType="numberPassword" />
设定颜色
<!-- android:textColorHint="#FF0000"设定输入后的文字颜色 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="默认显示的字符"
android:textColorHint="#FF0000"
android:textColor="#00ff00"
android:ems="10" />
监听输入的字符
<EditText
android:id="@+id/editText_id"
android:imeOptions="actionSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="实时监听输入的字符"
android:ems="10" />
package com.kale.edittext;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast; import com.kale.edittext.R; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); EditText eT = (EditText)findViewById(R.id.editText_id); eT.addTextChangedListener(new TextWatcher() { @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO 输入过程中,还在内存里,没到屏幕上 } @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO 在输入之前会触发的 } @Override
public void afterTextChanged(Editable s) {
// TODO 输入完将要显示到屏幕上时会触发
Toast.makeText(MainActivity.this, s.toString(), 0).show();
}
}); /*阻止一进入Activity,editText就获得焦点弹出输入法对话框,
* 只需要在AndroidManifest.xml相应的activity标签中加入下面这句话即可实现。
android:windowSoftInputMode="stateHidden|adjustResize"
<activity
android:name=".booking.FlightOrderInfoActivity"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden|adjustResize"/>*/
}
}
自定义风格
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="自定义风格"
android:layout_gravity="center"
android:gravity="center"
style="@style/my_edittext_style"
android:ems="10" />
<!-- 先继承系统的editText风格,自己重写 -->
<style name="my_edittext_style" parent="@android:style/Widget.EditText">
<item name="android:background">@drawable/input_box_bg</item>
</style>
设定点击效果,点上去后边框变黑。这里没用图片,是自己画的圆角
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="50dp"
android:textColor="#FFFAFA"
android:hint="设定点击效果,点上去边框变黑"
android:background=<strong>"@drawable/bg_edittext" </strong>
android:ems="10" />
bg_edittext_focused.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 获得焦点的时候 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="3dip"/>
<stroke
android:width="1dip"
android:color="#728ea3" />
</shape>
bg_edittext_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 没有被选中的时候的背景图 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="3dip"/>
<stroke
android:width="1dip"
android:color="#BDC7D8" />
</shape>
bg_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:drawable="@drawable/bg_edittext_normal" />
<item
android:state_focused="true"
android:drawable="@drawable/bg_edittext_focused" />
</selector>
自动换行
<!-- 我们只要确保singleLine为false的话,并且设置宽度一定,就可以自动换行,注意在这里不要设置inputType -->
<EditText
android:layout_width="400dp"
android:layout_height="wrap_content"
android:hint="自动换行,有的地方需要用到多行的文本输入框,但EditText在默认的情况下是单选的,且不能进行换行。"
android:textSize="30sp"
android:singleLine="false"
android:ems="10" />
源码下载:http://download.csdn.net/detail/shark0017/7593127
EditText 限制输入,自定义样式,监听输入的字符,自动换行的更多相关文章
- 在EditText中限制输入,自定义样式,监听输入的字符,自动换行
自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...
- Android EditText截获与监听输入事件
Android EditText截获与监听输入事件共有2种方法: 1.第一种方法:使用setOnKeyListener(),不过这种方式只能监听硬键盘事件. edittext.setOnKeyLi ...
- EditTextUtil 监听输入字数
package com.toge.ta.utils; import android.text.Editable;import android.text.Selection;import android ...
- 用jquery监听输入数字的变化
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 使用TextView/EditText应该注意的地方,监听EditText,addTextChangedListener
http://blog.csdn.net/huichengongzi/article/details/7818676 监听 EditText 控件: addTextChangedListener(ne ...
- [问题贴]mui.openWindow+自定义事件监听操作让alert()执行两次
仔细看,Alert函数执行了两次 共两个页面:index.html和detail.html, detail.html为按钮设置了自定义事件监听(newsId),触发alert. 在index.html ...
- input, textarea,监听输入事件
IE使用'propertychange'事件监听,其它浏览器使用'input'事件测试了IE7-10, Chrome, FF, 输入没有问题.♥但在IE9下, 删除, 回退, Ctrl+X 没有 ...
- 移动端 input光标问题 以及 监听输入
1. input 框光标问题: input框 在ios上显示的与Android是不一样的 显示是这样的 而且在输入的时候 光标位置变化了 是这样的 为了达到一致的效果 在行高加上\9 如:l ...
- jquery tagsinput监听输入、修改、删除事件
个人博客 地址:http://www.wenhaofan.com/article/20181118192458 由于度娘上的根本搜不到对应的操作,连该插件对应的文档介绍都没有,不得已debug了源码才 ...
随机推荐
- ASP.Net1
一.Web应用程序与传统桌面应用程序的不同: 1.产品级的Web应用程序总是包括至少两台联网的机器:一台承载网站,另一台在Web浏览器中查看数据. 即:我们通过自己的电脑浏览Web程序,这个程序会向服 ...
- 017 jquery中对样式的操作
1.样式操作 2.css-dom操作 3.程序 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- Ubuntu16.04 14.04 配置caffe(CPU only)
1.安装依赖 sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-seria ...
- 001.FTP简介及相关文件
一 FTP简介 FTP(File Transfer Protocol)文件传输协议,用于Internet上控制文件的双向传输. 下载:远程主机拷贝文件至本地: 上传:本地主机拷贝文件至远程. 二 FT ...
- grid网格系统布局
grid布局: 基于网格的2维布局方法1:display: grid | inline-grid | subgrid作用:启用网格grid容器 grid:定义一个块级的网格容器 inline-grid ...
- 分类器评估方法:ROC曲线
注:本文是人工智能研究网的学习笔记 ROC是什么 二元分类器(binary classifier)的分类结果 ROC空间 最好的预测模型在左上角,代表100%的灵敏度和0%的虚警率,被称为完美分类器. ...
- bzoj3628: [JLOI2014]天天酷跑
题目链接 bzoj3628: [JLOI2014]天天酷跑 题解 开始读错题目了,尴尬 由于题目说的跳跃次数和高度是在一开始设定的. 发现枚举一下记忆化搜索就可以过了 要注意,跳到最高点是可以不下坠继 ...
- Codeforces 666E Forensic Examination SAM+权值线段树
第一次做这种$SAM$带权值线段树合并的题 然而$zjq$神犇看完题一顿狂码就做出来了 $Orz$ 首先把所有串当成一个串建$SAM$ 我们对$SAM$上每个点 建一棵权值线段树 每个叶子节点表示一个 ...
- ueditor上传图片设置的简单实例
0.前言:我用过ckeditor,kingeditor还是感觉ueditor最好用,功能强大,经常更新.之前因为升级了struts2到2.5的了,原本的kingeditor已经不能共存,于是找到了ud ...
- Delphi 的链式代码
有了一系列的 Helper, Delphi 也可以使用链式代码了. //譬如要把 3.1415926 中的 141 提取为一个整数: var num: Integer; begin num := Pi ...