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;
    }

    
}
posted @ 2015-12-09 12:38  misybing  阅读(3421)  评论(0编辑  收藏  举报