惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
GbyAI
GbyAI
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
B
Blog
博客园 - 叶小钗
V
Visual Studio Blog
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
博客园 - Franky
V
V2EX
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
IT之家
IT之家
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Check Point Blog
N
Netflix TechBlog - Medium
博客园 - 【当耐特】

博客园 - Class Xman

手动建库时一个小错误:ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance Windows下USB磁盘开发系列二:枚举系统中所有USB设备 Clouda聊天室实践 ASP.NET Repeater嵌套Repeater实现菜单加载 浅说正则——会了就不会忘 android开发中难免遇到listview刷新数据出现异常 hdu2068RPG的错排 用实例讲解RSA加密算法(精) ARM——操作系统—最小操作系统-开发板测试 HTML入门教程 HDU 4770 Lights Against DudelyLights (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素) Android 程序崩溃后的处理 IE7 float:left失效的解决方法 android实现通过浏览器点击链接打开本地应用(APP)并拿到浏览器传递的数据 如何避免来自企业内部的网络安全威胁(二) Android安装失败 Installation error code: -110 不让自己的应用程序在桌面的图标列表里启动显示的方法 Fragment与FragmentAcitvity间的传值
Android TextWatcher监控EditText中的输入内容并限制其输入字...
Class Xman · 2013-11-14 · via 博客园 - Class Xman

布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去观察输入框中输入的内容,作个笔记。

主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  android:id="@+id/tv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@android:color/white" 
    android:ellipsize="marquee" 
    android:focusable="true" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:focusableInTouchMode="true" 
    android:scrollHorizontally="true"    
    android:text="Please input the text:"
    />
<EditText android:id="@+id/ET" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:inputType="number"/>
</LinearLayout>

java代码:

package com.android.text;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TextWatcherDemo extends Activity {
    private TextView mTextView;
    private EditText mEditText;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextView = (TextView)findViewById(R.id.tv);
        mEditText = (EditText)findViewById(R.id.ET);
        mEditText.addTextChangedListener(mTextWatcher);
    }
    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart ;
        private int editEnd ;
        @Override
        public void beforeTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
            temp = s;
        }
       
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
            mTextView.setText(s);
        }
       
        @Override
        public void afterTextChanged(Editable s) {
            editStart = mEditText.getSelectionStart();
            editEnd = mEditText.getSelectionEnd();
            if (temp.length() > 10) {
                Toast.makeText(TextWatcherDemo.this,
                        "你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
                        .show();
                s.delete(editStart-1, editEnd);
                int tempSelection = editStart;
                mEditText.setText(s);
                mEditText.setSelection(tempSelection);
            }
        }
    };
}