Android EditText input listening, dynamic acquisition of input characters

Source: Internet
Author: User

Sometimes we may use the constant listening for the EditText input characters, the number of listening characters, and some regular expression processing. The following methods can be implemented:

What I do is synchronize the data input by EditeText to TextView from time to time.

Layout file:

[Html]
<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 code:
[Html]
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;
}
 

}

Author: com360

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.