如何在Android中设置EditText的最大字符数?

如何设置Android EditTextinput的最大字符数? 我看到setMaxLines,setMaxEMS,但没有为字符的数量。

在XML中,您可以将此行添加到EditText View ,其中140是最大字符数:

 android:maxLength="140" 

你可以使用一个InputFilter,就是这样的:

 EditText myEditText = (EditText) findViewById(R.id.editText1); InputFilter[] filters = new InputFilter[1]; filters[0] = new InputFilter.LengthFilter(10); //Filter to 10 characters myEditText .setFilters(filters); 

dynamic:

 editText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MAX_NUM) }); 

通过xml:

 <EditText android:maxLength="@integer/max_length" 

它正在为我工​​作。

  etMsgDescription.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maximum_character)}); etMsgDescription.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { tvCharacterLimite.setText(" "+String.valueOf(maximum_character - etMsgDescription.getText().length())); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } });