Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
来源:互联网 发布:oracle for mac 下载 编辑:程序博客网 时间:2024/04/28 08:08
布局中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);
}
}
};
}
本文转自: http://www.cnblogs.com/cat-fang/archive/2011/04/12/2013428.html
0 0
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- 入门——TextWatcher监控EditText中的输入内容并限制其输入字符
- 监控EditText中的输入内容并限制其输入字符个数
- 自定义TextWatcher:实现EditText输入字符限制
- XSS编码剖析
- 给连续S个数组定义空间
- Demo of use SQL (Insert logs in system resource usage table)使用sql 的一个示例(插入系统使用日志数据)
- MSSQL 查找用户数据库下面的表名、表说明、列名、列说明、列类型、列长度、列是否可为空相关信息
- 硬盘的检测
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
- Eclipse远程调试jar包
- 转:iOS消息推送实现过程记录
- Mac OS X命令行中10组快捷键(导航、编辑)
- 【产品】产品经理必读的九步法
- Android 下枚举型使用、及与 int 转换的困惑
- (一)指定目标主机和网络
- 基于Matlab的FFT算法
- 游戏开发中的问题-----摘自《大型多人在线游戏开发》