自动获取焦点

<!-- 添加:<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: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" />

styles.xml

    <!-- 先继承系统的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" />

整个的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- 添加:<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:textColorHint="#FF0000"设定输入后的文字颜色 -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="默认显示的字符"
android:textColorHint="#FF0000"
android:textColor="#00ff00"
android:ems="10" /> <!-- 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" /> <EditText
android:id="@+id/editText_id"
android:imeOptions="actionSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="实时监听输入的字符"
android:ems="10" /> <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
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="50dp"
android:textColor="#FFFAFA"
android:hint="设定点击效果,点上去边框变黑"
android:background="@drawable/bg_edittext"
android:ems="10" /> <!-- 我们只要确保singleLine为false的话,并且设置宽度一定,就可以自动换行,注意在这里不要设置inputType -->
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="自动换行,有的地方需要用到多行的文本输入框,但EditText在默认的情况下是单选的,且不能进行换行。"
android:textSize="30sp"
android:singleLine="false"
android:ems="10" /> </LinearLayout>

源码下载:http://download.csdn.net/detail/shark0017/7593127

在EditText中限制输入,自定义样式,监听输入的字符,自动换行的更多相关文章

  1. EditText 限制输入,自定义样式,监听输入的字符,自动换行

    自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...

  2. Android EditText截获与监听输入事件

      Android EditText截获与监听输入事件共有2种方法: 1.第一种方法:使用setOnKeyListener(),不过这种方式只能监听硬键盘事件. edittext.setOnKeyLi ...

  3. Unity3D热更新之LuaFramework篇[04]--自定义UI监听方法

    时隔一个多月我又回来啦! 坚持真的是很难的一件事,其它事情稍忙,就很容易说服自己把写博客的计划给推迟了. 好在终于克服了自己的惰性,今天又开始了. 本篇继续我的Luaframework学习之路. 一. ...

  4. JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换

    本篇博客我们就来聊一下Spring框架中的观察者模式的应用,即事件的发送与监听机制.之前我们已经剖析过观察者模式的具体实现,以及使用Swift3.0自定义过通知机制.所以本篇博客对于事件发送与监听的底 ...

  5. EditTextUtil 监听输入字数

    package com.toge.ta.utils; import android.text.Editable;import android.text.Selection;import android ...

  6. Android中Button的五种监听事件

    简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...

  7. [问题贴]mui.openWindow+自定义事件监听操作让alert()执行两次

    仔细看,Alert函数执行了两次 共两个页面:index.html和detail.html, detail.html为按钮设置了自定义事件监听(newsId),触发alert. 在index.html ...

  8. 用jquery监听输入数字的变化

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  9. Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听

    原文:Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听 简单记录下android 盒子开发遥控器的监听 ,希望能帮到新入门的朋友们 不多说,直接贴代码 public cla ...

随机推荐

  1. Vue常用模板语法

    常用模板语法   本篇将在上一篇的基础上记录文本渲染.表达式.过滤器以及常用指令的简单用法. 一.文本渲染 Vue支持动态渲染文本,即在修改属性的同时,实时渲染文本内容.同时为了提高渲染效率,也支持只 ...

  2. python程序后台运行的实现

    后台运行work()方法. work.py # -*- coding:utf-8 -*- def work(): print "running" import time time. ...

  3. 033 Java Spark的编程

    1.Java SparkCore编程 入口是:JavaSparkContext 基本的RDD是:JavaRDD 其他常用RDD: JavaPairRDD JavaRDD和JavaPairRDD转换: ...

  4. windows下mysql配置

    windows下mysql配置   忙活了大半天,总算配置好了,本文献给windows下没试用过Mysql的小白,勿喷 http://blog.csdn.net/z1074907546/article ...

  5. 001.Pip简介安装使用

    一 PIP简介 pip类似RedHat里面的yum,使用PIP安装软件非常便捷快速. 二 PIP下载安装 方式一: [root@localhost ~]# yum install -y epel-re ...

  6. C#并行编程(6):线程同步面面观

    理解线程同步 线程的数据访问 在并行(多线程)环境中,不可避免地会存在多个线程同时访问某个数据的情况.多个线程对共享数据的访问有下面3种情形: 多个线程同时读取数据: 单个线程更新数据,此时其他线程读 ...

  7. 电子邮件-TCP

    参考下图,由于电子邮件是最早应用于网络上的服务,而早期网络的不可靠性,才设计了TCP协议,而邮件必须要保证正确传输,而非高速,所以早期的电子邮件协议 全是基于TCP的,一直持续到现在.

  8. Homebrew安装卸载

    安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/inst ...

  9. 应用通信-方案一:RestTemplate

    @RestController public class ClientController { @Autowired private LoadBalancerClient loadBalancerCl ...

  10. PHP常用设计模式

    1.单例模式指在整个应用中只有一个对象实例的设计模式 class Single { public $rand; static private $instance; // 类直接调用 final pri ...