• 我的Android进阶之旅------>Android关于TextWatcher的初步了解


    首先来看一下TextWatcher的源码

    package android.text;
    
    /**
     * When an object of a type is attached to an Editable, its methods will
     * be called when the text is changed.
     */
    public interface TextWatcher extends NoCopySpan {
        /**
         * This method is called to notify you that, within <code>s</code>,
         * the <code>count</code> characters beginning at <code>start</code>
         * are about to be replaced by new text with length <code>after</code>.
         * It is an error to attempt to make changes to <code>s</code> from
         * this callback.
         */
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after);
        /**
         * This method is called to notify you that, within <code>s</code>,
         * the <code>count</code> characters beginning at <code>start</code>
         * have just replaced old text that had length <code>before</code>.
         * It is an error to attempt to make changes to <code>s</code> from
         * this callback.
         */
        public void onTextChanged(CharSequence s, int start, int before, int count);
    
        /**
         * This method is called to notify you that, somewhere within
         * <code>s</code>, the text has been changed.
         * It is legitimate to make further changes to <code>s</code> from
         * this callback, but be careful not to get yourself into an infinite
         * loop, because any changes you make will cause this method to be
         * called again recursively.
         * (You are not told where the change took place because other
         * afterTextChanged() methods may already have made other changes
         * and invalidated the offsets.  But if you need to know here,
         * you can use {@link Spannable#setSpan} in {@link #onTextChanged}
         * to mark your place and then look up from here where the span
         * ended up.
         */
        public void afterTextChanged(Editable s);
    }
    

    以下通过通过一个小实例来学习TextWatcher的相关使用方法

    实现该接口

    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) {
    			mDetailView.setText(s);
    		}
    
    		@Override
    		public void afterTextChanged(Editable s) {
    			editStart = mEditText.getSelectionStart();
    			editEnd = mEditText.getSelectionEnd();
    			if (temp.length() > 11) {
    				Toast.makeText(MainActivity.this, "你输入的字数已经超过了限制!",
    						Toast.LENGTH_SHORT).show();
    				s.delete(editStart - 1, editEnd);
    				int tempSelection = editStart;
    				mEditText.setText(s);
    				mEditText.setSelection(tempSelection);
    			}
    		}
    	};
    再注冊这个监听

            TextView mDetailView;
    	EditText mEditText;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		mDetailView = (TextView) findViewById(R.id.mDetailView);
    		mEditText = (EditText) findViewById(R.id.mEditText);
    		mEditText.addTextChangedListener(mTextWatcher);
    	}

    看执行效果





     


                                ====================================================================================

      作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

      转载请保留原文地址http://blog.csdn.net/ouyang_peng

    ====================================================================================

     


  • 相关阅读:
    什么是系统打开文件表?
    为什么Unix只允许对非目录文件实行勾链?
    在Unix系统中,主存索引节点和辅存索引节点从内容上比较有什么不同,为什么要设置主存索引节点?
    Unix系统的文件目录项的内容是什么,这样处理的好处是什么?
    Unix系统使用的地址索引结构有什么特点?
    Unix文件系统的主要特点是什么?
    什么是打开文件操作,什么关闭文件操作,引入这两个操作的目的是什么?
    在非洲运营互联网系统-基础建设
    这一年在非洲(4年一轮回总结完结)
    4年一轮回(后半部)
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6744581.html
Copyright © 2020-2023  润新知