zoukankan      html  css  js  c++  java
  • Android TextWatcher应用实例

    (1)使用TextWathcer限制输入字符个数
    布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去观察输入框中输入的内容来限制输入字符个数。

    主布局:

    <?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:orientation="vertical">
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:text="Please input the text:"
            android:textColor="@android:color/white" />
    
        <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;
       
        @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);
                }
            }
        };
    }

    (2)使用TextWathcer实现EditeText和TextView同步
    TextWatcher自身是一个接口,首先需要实现这个接口并覆盖其三个方法,分别为Text改变之前,改变之后以及改变的过程中各自发生的动作相应,这里我们只需要实现EditText在文本发生改变时候让TextView的内容跟着发生变化。

    editText.addTextChangedListener(new TextWatcher(){
    @Override  
    public void afterTextChanged(Editable s) {  
        }  
    @Override  
    public void beforeTextChanged(CharSequence s,int start,int count,int after){      
        }  
    @Override  
    public void onTextChanged(CharSequence s, int start, int before, int count) {  
       textView.setText(editText.getText());  
      }  
    }); 

    可以看出TextWatcher是专门用来监听文本变化的,正因为它的这个技能,正是我们实现同步的功能所需要的。

  • 相关阅读:
    com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$Mis
    【MVC框架】——View和Controller之间的传值
    &lt;感悟帖&gt;互联网与电子传统行业之经历
    数据挖掘之七种经常使用的方法
    Linux下Java线程具体监控和其dump的分析使用----分析Java性能瓶颈[张振华-Jack]
    mysql 存储引擎的选择你会吗?
    基于新唐M0的XXTEA加密解密算法源码
    TEA encryption with 128bit key
    数据的加密传输——单片机上实现TEA加密解密算法(转)
    keil c51的内部RAM(idata)动态内存管理程序(转)
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6641411.html
Copyright © 2011-2022 走看看