zoukankan      html  css  js  c++  java
  • Android EditText的输入监听,输入字符的动态获取

    http://itindex.net/detail/38974-android-edittext-%E7%9B%91%E5%90%AC

    有时候我们可能会用到时时的监听EditText输入字符的时时监听,监听字符的个数,做一些正则表达式的处理等。如下方法可以实现:

    我做的是时时的把EditeText输入的数据同步到TextView上

    布局文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:padding="@dimen/padding_medium"
            tools:context=".Test02Activity" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textview"
            android:layout_below="@+id/textview"
            android:layout_marginTop="31dp"
            >
    
            <requestFocus />
        </EditText>
    
    </RelativeLayout>
    


    java代码:

    package com.example.testdemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.support.v4.app.NavUtils;
    import android.text.Editable;
    import android.text.TextWatcher;
    
    public class Test02Activity extends Activity {
    	private static final String TAG= "Test";
    	private EditText mEditText;
    	private TextView mTextView;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test02);
            mEditText = (EditText) findViewById(R.id.editText1);
            mTextView = (TextView) findViewById(R.id.textview);
            
            mEditText.addTextChangedListener(new TextWatcher(){
    
    			@Override
    			public void afterTextChanged(Editable s) {
    				Log.d(TAG, "afterTextChanged");
    			}
    
    			@Override
    			public void beforeTextChanged(CharSequence s, int start, int count,
    					int after) {
    				Log.d(TAG, "beforeTextChanged:" + s + "-" + start + "-" + count + "-" + after);
    				
    			}
    
    			@Override
    			public void onTextChanged(CharSequence s, int start, int before,
    					int count) {
    				Log.d(TAG, "onTextChanged:" + s + "-" + "-" + start + "-" + before + "-" + count);
    				mTextView.setText(s);
    			}
            	
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_test02, menu);
            return true;
        }
    
        
    }
    
  • 相关阅读:
    卡牌分组
    css字体样式+文本样式
    jQuery---on注册事件的2种方式
    css3神奇的背景控制属性+使用颜色过渡实现漂亮的渐变效果
    js Dom为页面中的元素绑定键盘或鼠标事件
    ES6中Set和WeakSet
    Vue之计算属性Computed和属性监听Watch,Computed和Watch的区别
    JS数据类型和堆栈+变量比较和值的复制+参数传递和类型检测
    复习node中加载静态资源--用express+esj
    种花问题
  • 原文地址:https://www.cnblogs.com/misybing/p/5032450.html
Copyright © 2011-2022 走看看