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. Android 滑动效果代码分享
  2. Android点击按钮实现全屏的代码
  3. Android开关机代码
  4. android 设置默认launcher 附上代码
  5. Android通过代码自动连接WiFi
  6. Android service: startService的代码实现
  7. 【代码】利用Android的Log 演示一个activity的生命周期

随机推荐

  1. Android 系统自带图标
  2. Android(安卓)Display System --- Surfac
  3. android获取versionName和versionCode
  4. 一句话让Android(安卓)原生launcher2的桌
  5. android TextView 控件居右显示
  6. Android(安卓)DrawableTextView图片文字
  7. ContentProvider数据共享
  8. Android 横屏不重启 Activity
  9. Android(安卓)Architecture Components
  10. Android中绘制圆角矩形图片及任意形状图