1. mainxml

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextViewandroid:id="@+id/tv"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:textColor="@android:color/white"
  11. android:ellipsize="marquee"
  12. android:focusable="true"
  13. android:marqueeRepeatLimit="marquee_forever"
  14. android:focusableInTouchMode="true"
  15. android:scrollHorizontally="true"
  16. android:text="Pleaseinputthetext:"
  17. />
  18. <EditTextandroid:id="@+id/ET"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:inputType="number"/>
  22. </LinearLayout>

java代码:

[java] view plain copy print ?
  1. packagecom.android.text;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.text.Editable;
  5. importandroid.text.TextWatcher;
  6. importandroid.util.Log;
  7. importandroid.widget.EditText;
  8. importandroid.widget.TextView;
  9. importandroid.widget.Toast;
  10. publicclassTextWatcherDemoextendsActivity{
  11. privateTextViewmTextView;
  12. privateEditTextmEditText;
  13. /**Calledwhentheactivityisfirstcreated.*/
  14. @Override
  15. publicvoidonCreate(BundlesavedInstanceState){
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. mTextView=(TextView)findViewById(R.id.tv);
  19. mEditText=(EditText)findViewById(R.id.ET);
  20. mEditText.addTextChangedListener(mTextWatcher);
  21. }
  22. TextWatchermTextWatcher=newTextWatcher(){
  23. privateCharSequencetemp;
  24. privateinteditStart;
  25. privateinteditEnd;
  26. @Override
  27. publicvoidbeforeTextChanged(CharSequences,intarg1,intarg2,
  28. intarg3){
  29. temp=s;
  30. }
  31. @Override
  32. publicvoidonTextChanged(CharSequences,intarg1,intarg2,
  33. intarg3){
  34. mTextView.setText(s);
  35. }
  36. @Override
  37. publicvoidafterTextChanged(Editables){
  38. editStart=mEditText.getSelectionStart();
  39. editEnd=mEditText.getSelectionEnd();
  40. if(temp.length()>10){
  41. Toast.makeText(TextWatcherDemo.this,
  42. "你输入的字数已经超过了限制!",Toast.LENGTH_SHORT)
  43. .show();
  44. s.delete(editStart-1,editEnd);
  45. inttempSelection=editStart;
  46. mEditText.setText(s);
  47. mEditText.setSelection(tempSelection);
  48. }
  49. }
  50. };
  51. }

更多相关文章

  1. 如何屏蔽自动弹出的输入法软件盘
  2. Android自动判定输入的是电话号码还是网址
  3. Android输入框中提示自动输入内容
  4. InputStream输入流,读取数据实例
  5. Android(安卓)editText 输入字数限制
  6. 【Android】debug 状态下其签名文件 debug.keystore 相关(如何获
  7. cocos2dx在windows下开发,编译到android上
  8. 我的android 第31天 - Activity(四)
  9. Mac下配置Android(安卓)NDK环境并搭建

随机推荐

  1. 展讯android LEDS模块分析----各种关系
  2. Android(安卓)Studio 4.0 新功能之 Layou
  3. Android半透明提示效果的实现
  4. SurfaceTexture,TextureView,GLsurfaceview
  5. android:scrollbarStyle
  6. 2018-08-02 Android中实现阴影和图片圆角
  7. Android(安卓)Activity 的四种启动模式
  8. EditText软键盘弹出相关问题
  9. Windows下Eclipse集成Cygwin配置Android(
  10. 【Android源码】Intent 源码分析