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了源码才 ...
随机推荐
- oracle11g数据库升级数据库升级
Oracle对自己产品也一样,对于自己的产品在不同的时期,支持的强度是不一样的.大体分来,支持的强度分为三个级别:Premier Support(最高优先级的支持),Extended Support( ...
- 028 -bash-4.1$ 出现故障的原理及解决办法?
最近在搭建分布式的时候,出现了这个问题,很不爽.下面是我的解决方式. 1.在用户下删除bash rm -rf /home/beifeng/.bash* 2.拷贝 cp /etc/skel/.bash* ...
- 004.Autofs自动挂载
一 安装autofs [root@imxhy data]# yum -y install autofs 二 编辑自动挂载相关配置 2.1 修改master [root@imxhy ~]# vi /et ...
- jqplot导入包小结
对于jqplot画图的导入包,总结起来就是两种,一种是每个jsp文件都是导入一样的js或css包!这些包可以另新建一个文件存放,有如下这些包! <link rel="styleshee ...
- 【WIN10】Segoe MDL2 Assets
APP下載地址:https://www.microsoft.com/store/apps/9nblggh5k2hf 最近使用文本圖標Segoe MDL2 Assets時,使用字符映射表看,那個圖標真的 ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem B. Travelling Camera Problem set贪心
Problem B. Travelling Camera Problem 题目连接: http://www.codeforces.com/gym/100253 Description Programm ...
- 微服务架构的分布式事务解决方案 - zhaorui2017的博客 - CSDN博客
微服务架构的分布式事务解决方案 - zhaorui2017的博客 - CSDN博客 http://blog.csdn.net/zhaorui2017/article/details/7643679 ...
- spring cloud 学习(11) - 用fastson替换jackson及用gb2312码输出
前几天遇到一个需求,因为要兼容旧项目的编码格式,需要spring-cloud的rest接口,输出gb2312编码,本以为是一个很容易的事情,比如下面这样: @RequestMapping(method ...
- 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接(转)
前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...
- HDU 4747 Mex (2013杭州网络赛1010题,线段树)
Mex Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...