监听EditView中的文本改变事件详解

来源:互联网 发布:手机淘宝5.9.2旧版本 编辑:程序博客网 时间:2024/05/15 08:43

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);            }            }                });    }}
然后就ok了,很多地都可以用到这个办法。

源代码在下面:

http://download.csdn.net/detail/zoeice/4399601



原创粉丝点击