android 中如何限制 EditText 最大输入字符数?
在代码中使用InputFilter 进行过滤。
//editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)}); 即限定最大输入字符数为20。
[java] 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. EditText editText = (EditText)findViewById(R.id.entry);
08. editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
09. }}public class TextEditActivity extends Activity {/** Called when the activity is first created. */@Overridepublic 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)});}}