4、再点击“特殊符号”,2、再把鼠标光标放在qq拼音输入法状态栏里面的任意位置,并设置光标位置 * */ public class maxlengthwatcher implements textwatcher { private int maxlen = 0; private edittext edittext = null; public maxlengthwatcher(int maxlen, edittext edittext) { this.maxlen = maxlen; this.edittext = edittext; } public void aftertextchanged(editable arg0) { // todo auto-generated method stub } public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub editable editable = edittext.gettext(); int len = editable.length(); if(len 》 maxlen) { int selendindex = selection.getselectionend(editable); string str = editable.tostring(); //截取新字符串 string newstr = str.substring(0,maxlen); edittext.settext(newstr); editable = edittext.gettext(); //新字符串的长度 int newlen = editable.length(); //旧光标位置超过字符串长度 if(selendindex 》 newlen) { selendindex = editable.length(); } //设置新光标所在的位置 selection.setselection(editable, selendindex); } } } 对应的 activity 部分的调用为: view plaincopyprint? 01.package cie.textedit; 02. 03.import android.app.activity; 04.import android.os.bundle; 05.import android.text.inputfilter; 06.import android.widget.edittext; 07. 08.public class texteditactivity extends activity { 09. /** called when the activity is first created. */ 10. @override 11. public void oncreate(bundle savedinstancestate) { 12. super.oncreate(savedinstancestate); 13. setcontentview(r.layout.main); 14. 15. edittext edittext = (edittext) findviewbyid(r.id.entry); 16. edittext.addtextchangedlistener(new maxlengthwatcher(10, edittext)); 17. 18. } 19.} package cie.textedit; import android.app.activity; import android.os.bundle; import android.text.inputfilter; import android.widget.edittext; public class texteditactivity extends activity { /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); edittext edittext = (edittext) findviewbyid(r.id.entry); edittext.addtextchangedlistener(new maxlengthwatcher(10, edittext)); } } 限制输入字符数为10个 main.xml 文件 view plaincopyprint? 01.《?xml version=“1.0“ encoding=“utf-8“?》 02.《relativelayout xmlns:android=“http://schemas.android.com/apk/res/android“ 03. android:layout_width=“fill_parent“ 04. android:layout_height=“fill_parent“》 05. 《textview 06. android:id=“@ id/label“ 07. android:layout_width=“fill_parent“ 08. android:layout_height=“wrap_content“ 09. android:text=“type here:“/》 10. 《edittext 11. android:id=“@ id/entry“ 12. android:singleline=“true“ 13. android:layout_width=“fill_parent“ 14. android:layout_height=“wrap_content“ 15. android:background=“@android:drawable/editbox_background“ 16. android:layout_below=“@id/label“/》 17. 《button 18. android:id=“@ id/ok“ 19. android:layout_width=“wrap_content“ 20. android:layout_height=“wrap_content“ 21. android:layout_below=“@id/entry“ 22. android:layout_alignparentright=“true“ 23. android:layout_marginleft=“10dip“ 24. android:text=“ok“ /》 25. 《button 26. android:layout_width=“wrap_content“ 27. android:layout_height=“wrap_content“ 28. android:layout_toleftof=“@id/ok“ 29. android:layout_aligntop=“@id/ok“ 30. android:text=“cancel“ /》 31.《/relativelayout》 《?xml version=“1.0“ encoding=“utf-8“?》 《relativelayout xmlns:android=“http://schemas.android.com/apk/res/android“ android:layout_width=“fill_parent“ android:layout_height=“fill_parent“》 《textview android:id=“@ id/label“ android:layout_width=“fill_parent“ android:layout_height=“wrap_content“ android:text=“type here:“/》 《edittext android:id=“@ id/entry“ android:singleline=“true“ android:layout_width=“fill_parent“ android:layout_height=“wrap_content“ android:background=“@android:drawable/editbox_background“ android:layout_below=“@id/label“/》 《button android:id=“@ id/ok“ android:layout_width=“wrap_content“ android:layout_height=“wrap_content“ android:layout_below=“@id/entry“ android:layout_alignparentright=“true“ android:layout_marginleft=“10dip“ android:text=“ok“ /》 《button android:layout_width=“wrap_content“ android:layout_height=“wrap_content“ android:layout_toleftof=“@id/ok“ android:layout_aligntop=“@id/ok“ android:text=“cancel“ /》 《/relativelayout》,点击“符号输入”,并设置光标位置 10. * */ 11.public class maxlengthwatcher implements textwatcher { 12. 13. private int maxlen = 0; 14. private edittext edittext = null; 15. 16. 17. public maxlengthwatcher(int maxlen, edittext edittext) { 18. this.maxlen = maxlen; 19. this.edittext = edittext; 20. } 21. 22. public void aftertextchanged(editable arg0) { 23. // todo auto-generated method stub 24. 25. } 26. 27. public void beforetextchanged(charsequence arg0, int arg1, int arg2, 28. int arg3) { 29. // todo auto-generated method stub 30. 31. } 32. 33. public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) { 34. // todo auto-generated method stub 35. editable editable = edittext.gettext(); 36. int len = editable.length(); 37. 38. if(len 》 maxlen) 39. { 40. int selendindex = selection.getselectionend(editable); 41. string str = editable.tostring(); 42. //截取新字符串 43. string newstr = str.substring(0,maxlen); 44. edittext.settext(newstr); 45. editable = edittext.gettext(); 46. 47. //新字符串的长度 48. int newlen = editable.length(); 49. //旧光标位置超过字符串长度 50. if(selendindex 》 newlen) 51. { 52. selendindex = editable.length(); 53. } 54. //设置新光标所在的位置 55. selection.setselection(editable, selendindex); 56. 57. } 58. } 59. 60.} package cie.textedit; import android.text.editable; import android.text.selection; import android.text.textwatcher; import android.widget.edittext; /* * 监听输入内容是否超出最大长度,方法如下:1、把电脑输入法切换到qq拼音输入法,可以用qq拼音输入法打出来,android edittext输入最大长度限制如何给用户以友好的提示 方法一: 在 xml 文件中设置文本框属性作字符数限制 如:android:maxlength=“10“ 即限制最大输入字符个数为10 方法二: 在代码中使用inputfilter 进行过滤 //edittext.setfilters(new inputfilter{new inputfilter.lengthfilter(20)}); 即限定最大输入字符数为20 view plaincopyprint? 01.public class texteditactivity extends activity { 02. /** called when the activity is first created. */ 03. @override 04. public void oncreate(bundle savedinstancestate) { 05. super.oncreate(savedinstancestate); 06. setcontentview(r.layout.main); 07. 08. edittext edittext = (edittext)findviewbyid(r.id.entry); 09. edittext.setfilters(new inputfilter{new inputfilter.lengthfilter(20)}); 10. } 11.} public class texteditactivity extends activity { /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); edittext edittext = (edittext)findviewbyid(r.id.entry); edittext.setfilters(new inputfilter{new inputfilter.lengthfilter(20)}); } } 方法三: 利用 textwatcher 进行监听 view plaincopyprint? 01.package cie.textedit; 02. 03.import android.text.editable; 04.import android.text.selection; 05.import android.text.textwatcher; 06.import android.widget.edittext; 07. 08./* 09. * 监听输入内容是否超出最大长度。
本文目录
字母c,反过来的是ɔ,可以用qq拼音输入法打出来,方法如下:
1、把电脑输入法切换到qq拼音输入法;
2、再把鼠标光标放在qq拼音输入法状态栏里面的任意位置,然后单击鼠标右键;
3、在弹出来的页面,点击“符号输入”;
4、再点击“特殊符号”;
5、接着点击左边的“英文音标”,右边第一行倒数第二个符号就是了,点击它即可录入;
6、输入结果如下图所示。
方法一:
在 xml 文件中设置文本框属性作字符数限制
如:android:maxlength=“10“ 即限制最大输入字符个数为10
方法二:
在代码中使用inputfilter 进行过滤
//edittext.setfilters(new inputfilter{new inputfilter.lengthfilter(20)}); 即限定最大输入字符数为20
view plaincopyprint?
01.public class texteditactivity extends activity {
02. /** called when the activity is first created. */
03. @override
04. public void oncreate(bundle savedinstancestate) {
05. super.oncreate(savedinstancestate);
06. setcontentview(r.layout.main);
07.
08. edittext edittext = (edittext)findviewbyid(r.id.entry);
09. edittext.setfilters(new inputfilter{new inputfilter.lengthfilter(20)});
10. }
11.}
public class texteditactivity extends activity {
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
edittext edittext = (edittext)findviewbyid(r.id.entry);
edittext.setfilters(new inputfilter{new inputfilter.lengthfilter(20)});
}
}
方法三:
利用 textwatcher 进行监听
view plaincopyprint?
01.package cie.textedit;
02.
03.import android.text.editable;
04.import android.text.selection;
05.import android.text.textwatcher;
06.import android.widget.edittext;
07.
08./*
09. * 监听输入内容是否超出最大长度,并设置光标位置
10. * */
11.public class maxlengthwatcher implements textwatcher {
12.
13. private int maxlen = 0;
14. private edittext edittext = null;
15.
16.
17. public maxlengthwatcher(int maxlen, edittext edittext) {
18. this.maxlen = maxlen;
19. this.edittext = edittext;
20. }
21.
22. public void aftertextchanged(editable arg0) {
23. // todo auto-generated method stub
24.
25. }
26.
27. public void beforetextchanged(charsequence arg0, int arg1, int arg2,
28. int arg3) {
29. // todo auto-generated method stub
30.
31. }
32.
33. public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) {
34. // todo auto-generated method stub
35. editable editable = edittext.gettext();
36. int len = editable.length();
37.
38. if(len 》 maxlen)
39. {
40. int selendindex = selection.getselectionend(editable);
41. string str = editable.tostring();
42. //截取新字符串
43. string newstr = str.substring(0,maxlen);
44. edittext.settext(newstr);
45. editable = edittext.gettext();
46.
47. //新字符串的长度
48. int newlen = editable.length();
49. //旧光标位置超过字符串长度
50. if(selendindex 》 newlen)
51. {
52. selendindex = editable.length();
53. }
54. //设置新光标所在的位置
55. selection.setselection(editable, selendindex);
56.
57. }
58. }
59.
60.}
package cie.textedit;
import android.text.editable;
import android.text.selection;
import android.text.textwatcher;
import android.widget.edittext;
/*
* 监听输入内容是否超出最大长度,并设置光标位置
* */
public class maxlengthwatcher implements textwatcher {
private int maxlen = 0;
private edittext edittext = null;
public maxlengthwatcher(int maxlen, edittext edittext) {
this.maxlen = maxlen;
this.edittext = edittext;
}
public void aftertextchanged(editable arg0) {
// todo auto-generated method stub
}
public void beforetextchanged(charsequence arg0, int arg1, int arg2,
int arg3) {
// todo auto-generated method stub
}
public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) {
// todo auto-generated method stub
editable editable = edittext.gettext();
int len = editable.length();
if(len 》 maxlen)
{
int selendindex = selection.getselectionend(editable);
string str = editable.tostring();
//截取新字符串
string newstr = str.substring(0,maxlen);
edittext.settext(newstr);
editable = edittext.gettext();
//新字符串的长度
int newlen = editable.length();
//旧光标位置超过字符串长度
if(selendindex 》 newlen)
{
selendindex = editable.length();
}
//设置新光标所在的位置
selection.setselection(editable, selendindex);
}
}
}
对应的 activity 部分的调用为:
view plaincopyprint?
01.package cie.textedit;
02.
03.import android.app.activity;
04.import android.os.bundle;
05.import android.text.inputfilter;
06.import android.widget.edittext;
07.
08.public class texteditactivity extends activity {
09. /** called when the activity is first created. */
10. @override
11. public void oncreate(bundle savedinstancestate) {
12. super.oncreate(savedinstancestate);
13. setcontentview(r.layout.main);
14.
15. edittext edittext = (edittext) findviewbyid(r.id.entry);
16. edittext.addtextchangedlistener(new maxlengthwatcher(10, edittext));
17.
18. }
19.}
package cie.textedit;
import android.app.activity;
import android.os.bundle;
import android.text.inputfilter;
import android.widget.edittext;
public class texteditactivity extends activity {
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
edittext edittext = (edittext) findviewbyid(r.id.entry);
edittext.addtextchangedlistener(new maxlengthwatcher(10, edittext));
}
}
限制输入字符数为10个
main.xml 文件
view plaincopyprint?
01.《?xml version=“1.0“ encoding=“utf-8“?》
02.《relativelayout xmlns:android=“http://schemas.android.com/apk/res/android“
03. android:layout_width=“fill_parent“
04. android:layout_height=“fill_parent“》
05. 《textview
06. android:id=“@ id/label“
07. android:layout_width=“fill_parent“
08. android:layout_height=“wrap_content“
09. android:text=“type here:“/》
10. 《edittext
11. android:id=“@ id/entry“
12. android:singleline=“true“
13. android:layout_width=“fill_parent“
14. android:layout_height=“wrap_content“
15. android:background=“@android:drawable/editbox_background“
16. android:layout_below=“@id/label“/》
17. 《button
18. android:id=“@ id/ok“
19. android:layout_width=“wrap_content“
20. android:layout_height=“wrap_content“
21. android:layout_below=“@id/entry“
22. android:layout_alignparentright=“true“
23. android:layout_marginleft=“10dip“
24. android:text=“ok“ /》
25. 《button
26. android:layout_width=“wrap_content“
27. android:layout_height=“wrap_content“
28. android:layout_toleftof=“@id/ok“
29. android:layout_aligntop=“@id/ok“
30. android:text=“cancel“ /》
31.《/relativelayout》
《?xml version=“1.0“ encoding=“utf-8“?》
《relativelayout xmlns:android=“http://schemas.android.com/apk/res/android“
android:layout_width=“fill_parent“
android:layout_height=“fill_parent“》
《textview
android:id=“@ id/label“
android:layout_width=“fill_parent“
android:layout_height=“wrap_content“
android:text=“type here:“/》
《edittext
android:id=“@ id/entry“
android:singleline=“true“
android:layout_width=“fill_parent“
android:layout_height=“wrap_content“
android:background=“@android:drawable/editbox_background“
android:layout_below=“@id/label“/》
《button
android:id=“@ id/ok“
android:layout_width=“wrap_content“
android:layout_height=“wrap_content“
android:layout_below=“@id/entry“
android:layout_alignparentright=“true“
android:layout_marginleft=“10dip“
android:text=“ok“ /》
《button
android:layout_width=“wrap_content“
android:layout_height=“wrap_content“
android:layout_toleftof=“@id/ok“
android:layout_aligntop=“@id/ok“
android:text=“cancel“ /》
《/relativelayout》