TextWatcher监控EditText (转)
16lz
2022-03-31
1. mainxml
[html] view plain copy print ?- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextViewandroid:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textColor="@android:color/white"
- android:ellipsize="marquee"
- android:focusable="true"
- android:marqueeRepeatLimit="marquee_forever"
- android:focusableInTouchMode="true"
- android:scrollHorizontally="true"
- android:text="Pleaseinputthetext:"
- />
- <EditTextandroid:id="@+id/ET"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="number"/>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@android:color/white" android:ellipsize="marquee" android:focusable="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:text="Please input the text:" /><EditText android:id="@+id/ET" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number"/></LinearLayout>
java代码:
[java] view plain copy print ?- packagecom.android.text;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.text.Editable;
- importandroid.text.TextWatcher;
- importandroid.util.Log;
- importandroid.widget.EditText;
- importandroid.widget.TextView;
- importandroid.widget.Toast;
- publicclassTextWatcherDemoextendsActivity{
- privateTextViewmTextView;
- privateEditTextmEditText;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mTextView=(TextView)findViewById(R.id.tv);
- mEditText=(EditText)findViewById(R.id.ET);
- mEditText.addTextChangedListener(mTextWatcher);
- }
- TextWatchermTextWatcher=newTextWatcher(){
- privateCharSequencetemp;
- privateinteditStart;
- privateinteditEnd;
- @Override
- publicvoidbeforeTextChanged(CharSequences,intarg1,intarg2,
- intarg3){
- temp=s;
- }
- @Override
- publicvoidonTextChanged(CharSequences,intarg1,intarg2,
- intarg3){
- mTextView.setText(s);
- }
- @Override
- publicvoidafterTextChanged(Editables){
- editStart=mEditText.getSelectionStart();
- editEnd=mEditText.getSelectionEnd();
- if(temp.length()>10){
- Toast.makeText(TextWatcherDemo.this,
- "你输入的字数已经超过了限制!",Toast.LENGTH_SHORT)
- .show();
- s.delete(editStart-1,editEnd);
- inttempSelection=editStart;
- mEditText.setText(s);
- mEditText.setSelection(tempSelection);
- }
- }
- };
- }
更多相关文章
- 如何屏蔽自动弹出的输入法软件盘
- Android自动判定输入的是电话号码还是网址
- Android输入框中提示自动输入内容
- InputStream输入流,读取数据实例
- Android(安卓)editText 输入字数限制
- 【Android】debug 状态下其签名文件 debug.keystore 相关(如何获
- cocos2dx在windows下开发,编译到android上
- 我的android 第31天 - Activity(四)
- Mac下配置Android(安卓)NDK环境并搭建