Android 监听EditView中的文本改变事件
android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面的搜索列表就显示有包含输入关键字的选项,这个输入监听怎么实现的呢?
我们可以建一个例子,效果图如下:

我们可以监听光标处在哪个位置,选择了几个字符并处理,输入了几个字符
先新建布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/af"> <!-- 上下滚动 --> <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <!-- 编辑框 --> <EditText
android:id="@+id/id_edittext_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/alert_light"
android:textSize="10sp"
android:textColor="#ffff"
/> <TextView
android:id="@+id/id_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffff"
/> <TextView
android:id="@+id/id_textview_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/hah"
android:textColor="#f000"
/> <TextView
android:id="@+id/id_textview_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/hah"
android:textColor="#f000"
/> </LinearLayout>
</ScrollView> </LinearLayout>
然后在代码中对编辑框绑定输入监听事件:
public class EditTextTestActivity extends Activity {
/**编辑框*/
private EditText edit1_;
/**文本*/
private TextView text_;
private TextView text1_;
private TextView text2_;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*设置当前页面的布局*/
setMyLayout();
}
/**
* 设置当前页面的布局
*/
private void setMyLayout(){
/*取得文本*/
text_ = (TextView)findViewById(R.id.id_textview);
text1_ = (TextView)findViewById(R.id.id_textview_1);
text2_ = (TextView)findViewById(R.id.id_textview_2);
/*取得编辑框*/
edit1_ = (EditText)findViewById(R.id.id_edittext_1);
/*监听 编辑框中的文本改变事件*/
edit1_.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
/*++ 文本每次改变就会跑这个方法 ++*/
if(null != text_){
text_.setText("您正在输入......\n当前光标处在第 " + start
+" 个位置\n您选择处理了 " + before + " 个字符\n您这次输入的词语有 "
+ count + " 个字符");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
/*++这里的count树枝上是和onTextChanged()里的before一样的
* after树枝上是和onTextChanged()里的count一样的 ++*/
if(null != text1_){
text1_.setText("您正在输入......\n当前光标处在第 " + start
+" 个位置\n您选择处理了 " + count + " 个字符\n您这次输入的词语有 "
+ after + " 个字符");
}
}
@Override
public void afterTextChanged(Editable s) {
/*++这里显示出输入的字符串++*/
if(null != text2_){
text2_.setText(s);
}
}
});
}
}
源代码:http://download.csdn.net/detail/zoeice/4399601
Android 监听EditView中的文本改变事件的更多相关文章
- 监听EditView中的文本改变事件详解--转
转自: http://blog.csdn.net/zoeice/article/details/7700529 android中的编辑框EditText也比较常用,那比如在搜索框中,没输入一个字,下面 ...
- 【Android】Android 监听apk安装替换卸载广播
[Android]Android 监听apk安装替换卸载广播 首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDE ...
- js 实时监听input中值变化
注意:用到了jquery需要引入jquery.min.js. 需求: 1.每个地方需要分别打分,总分为100; 2.第一个打分总分为40; 3.第二个打分总分为60. 注意:需要判断null.&quo ...
- Android监听系统短信数据库变化-提取短信内容
由于监听系统短信广播受到权限的限制,所以很多手机可能使用这种方式没法监听广播,从而没办法获取到系统短信,所以又重新开辟一条路. Android监听系统短信数据库内容变化使用场景: 1.监听短信数据库的 ...
- 【转】Android开发20——单个监听器监听多个按钮点击事件
原文网址:http://woshixy.blog.51cto.com/5637578/1093936 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律 ...
- Android监听ScrollView滑动到顶端和底部
Android监听ScrollView滑动到顶端和底部 package cn.testscrollview; import android.os.Bundle; import android. ...
- Android 监听双卡信号强度(附完整代码)
Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...
- UITableView延伸:点击cell关闭键盘,加载不同cell,监听里面的textfeild内容改变
其实点击cell关闭键盘只要一句话 - () { cell = [tableView dequeueReusableCellWithIdentifier:){ cell ...
- Fragment-如何监听fragment中的回退事件与怎样保存fragment状态
一.如何监听Fragment中的回退事件 1.问题阐述 在Activity中监听回退事件是件非常容易的事,因为直接重写onBackPressed()函数就好了,但当大家想要监听Fragment中的回退 ...
随机推荐
- linux gcc 和 g++ 编译
gcc编译 gcc -o test.out test.c g++ 编译 g++ -o test.out test.cpp
- Linux C 程序 获取目录信息(16)
4.获取当前目录getcwd 会将当前工作目录绝对路径复制到参数buf所指的内存空间5.设置工作目录chdir6.获取目录信息opendir打开一个目录readdir读取目录中的内容 读取目录项信息 ...
- Traveller项目技术资料
Spring Spring PecClinic:Spring官方的宠物医院项目 it.zhaozhao.info/archives/63818:SPRING JPA入门 Spring Data RES ...
- jQueryr .on方法解析
.On() 其实.bind(), .live(), .delegate()都是通过.on()来实现的,.unbind(), .die(), .undelegate(),也是一样的都是通过.off()来 ...
- php字符串首字母转换大小写的实例分享
php中对字符串首字母进行大小写转换的例子. in: 后端程序首字母变大写:ucwords() <?php $foo = 'hello world!'; $foo = ucwords($foo) ...
- a标签点击后的虚线框问题
以前一直用的方法都是: a {outline: none;star:expression(this.onFocus=this.blur());} 后来发现有瑕疵,不完美.体现在页面调用JS动作比较频繁 ...
- hbase on spark
1.在spark的伪分布式环境下安装HBASE (1)版本:我使用的spark版本是1.3.0,使用的hbase版本是hbase-0.94.16 (2)解压,tar zxvf hbase-0.94. ...
- 概念:RPG与RPGLE的区别
RPG是OPM编程模式,即RPG编程的代码不能编译成*MODULE:编译只能直接生成一个程序,*PGM. RPGLE是ILE编程模式.OS/400环境下,ILE是集成开发环境.在ILE环境下,所 ...
- 【转】perl特殊符号及默认的内部变量
perl特殊符号及默认的内部变量,有需要的朋友不妨参考下 Perl的特殊符号 @ 数组 $x{} x名字前面是美元符号($),后面是花 ...
- 【转】perl如何避免脚本在windows中闪一下就关闭
写好了perl程序,运行后,准备等待结果输出时,结果双击后,看到屏幕闪了一下,然后什么都没有了,根本没有机会然你看到输出的结果 当你刚开始学习perl的时候,写好了程序,准备兴高采烈的等待结果输出时, ...