android 代码EditText.setHint后 输入不了 本帖最后由 q5351818 于 2012-04-05 10:14:32 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 final LimitedEditText editSign = new LimitedEditText(this); editSign.setMaxCharLength(203); editSign.setHint("以英文,为分割"); String description = user.getDescription(); if (null != description) { editSign.setText(description); editSign.setSelection(description.length()); } editSign.addTextChangedListener(new TextWatcher() { // 检验输入的是否符合 要求:12个词语,逗号分隔。每个词语最长8汉字或16字符 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (isChanged) { return; } String inPutStr = s.toString(); String clearStr = null; // 如果包含"," 则将他们截成字符串数组 且只能维持在12组词语范围内 每组不能超过 16个字节 clearStr = StringUtil.checkStr(inPutStr, beforStr); isChanged = true; editSign.setText(clearStr); editSign.setSelection(clearStr.length()); // 光标停落在 焦点位置上 isChanged = false; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (isChanged) { return; } } @Override public void afterTextChanged(Editable s) { if (isChanged) { return; } } }); 奇怪呢 我吧 editSign.setHint("以英文','为分割,最多12组,每组最多8个汉字或者16个数组'字符"); 去掉呢 也不能输入呢 昨晚还好好的 LimitedEditText 为自定义控件?也贴出来看看 public class LimitedEditText extends EditText{ /** * 属性名 */ private static final String ATTRIBUTE_MAX_CHAR_LENGTH = "maxCharLength"; /** * [构造简要说明] * * @param context context * @param attrs attrs */ public LimitedEditText(Context context, AttributeSet attrs) { super(context, attrs); int maxCharLen = attrs.getAttributeUnsignedIntValue(null, ATTRIBUTE_MAX_CHAR_LENGTH, -1); if (maxCharLen > 0) { setFilters(new InputFilter[] {new CharLengthFilter(maxCharLen) }); } } /** * [构造简要说明] * * @param context context */ public LimitedEditText(Context context) { super(context); } /** * [构造简要说明] * * @param context context * @param attrs attrs * @param defStyle defStyle */ public LimitedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * * 设置最大字符数<BR> * [功能详细描述] * * @param maxCharLength maxCharLength */ public void setMaxCharLength(int maxCharLength) { setFilters(new InputFilter[] {new CharLengthFilter(maxCharLength) }); } /** * This filter will constrain edits not to make the length of the text * greater than the specified length. */ public static class CharLengthFilter implements InputFilter { private int mMax; /** * * [构造简要说明] * * @param max max */ public CharLengthFilter(int max) { mMax = max; } /** * * [一句话功能简述]<BR> * [功能详细描述] * * @param source source * @param start start * @param end end * @param dest dest * @param dstart dstart * @param dend dend * @return CharSequence * @see android.text.InputFilter#filter(java.lang.CharSequence, int, * int, android.text.Spanned, int, int) */ public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { int destLen = StringUtil.count2BytesChar(dest.toString()); int keep = mMax - (destLen - (dend - dstart)); int srcLen = StringUtil.count2BytesChar(source.toString()); if (keep <= 0) { return ""; } else if (keep >= srcLen) { return null; // keep original } else { StringBuffer buffer = new StringBuffer(); int cnt = 0; for (int i = start; i < end; i++) { char c = source.charAt(i); cnt++; if (c >= 256) { cnt++; } if (cnt <= keep) { buffer.append(c); } else { break; } } return buffer.toString(); } } }} android 调用webservice问题 如何在定义的对话框内,标题跟下面内容之间显示横线 C#做服务端 android做客户端 通讯 android 中widget组件 在监听用户按键时遇到问题(在线求解!) 如何使用root用户 安卓 graphical layout相关问题 android 线程池的问题 android 发送mail实现 我有意制造了一个异常,但是它却不在Console控制台显示啊.100分 android bitmap vm 内存溢出问题 救命啊,这发生了什么?小白求指教
editSign.setMaxCharLength(203);
editSign.setHint("以英文,为分割");
String description = user.getDescription();
if (null != description)
{
editSign.setText(description);
editSign.setSelection(description.length());
}
editSign.addTextChangedListener(new TextWatcher()
{
// 检验输入的是否符合 要求:12个词语,逗号分隔。每个词语最长8汉字或16字符
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
if (isChanged)
{
return;
}
String inPutStr = s.toString();
String clearStr = null;
// 如果包含"," 则将他们截成字符串数组 且只能维持在12组词语范围内 每组不能超过 16个字节
clearStr = StringUtil.checkStr(inPutStr,
beforStr);
isChanged = true;
editSign.setText(clearStr);
editSign.setSelection(clearStr.length()); // 光标停落在 焦点位置上
isChanged = false;
} @Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
if (isChanged)
{
return;
}
} @Override
public void afterTextChanged(Editable s)
{
if (isChanged)
{
return;
}
}
});
{ /**
* 属性名
*/
private static final String ATTRIBUTE_MAX_CHAR_LENGTH = "maxCharLength"; /**
* [构造简要说明]
*
* @param context context
* @param attrs attrs
*/
public LimitedEditText(Context context, AttributeSet attrs)
{
super(context,
attrs);
int maxCharLen = attrs.getAttributeUnsignedIntValue(null,
ATTRIBUTE_MAX_CHAR_LENGTH,
-1);
if (maxCharLen > 0)
{
setFilters(new InputFilter[] {new CharLengthFilter(maxCharLen) });
}
} /**
* [构造简要说明]
*
* @param context context
*/
public LimitedEditText(Context context)
{
super(context);
} /**
* [构造简要说明]
*
* @param context context
* @param attrs attrs
* @param defStyle defStyle
*/
public LimitedEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context,
attrs,
defStyle);
} /**
*
* 设置最大字符数<BR>
* [功能详细描述]
*
* @param maxCharLength maxCharLength
*/
public void setMaxCharLength(int maxCharLength)
{
setFilters(new InputFilter[] {new CharLengthFilter(maxCharLength) });
} /**
* This filter will constrain edits not to make the length of the text
* greater than the specified length.
*/
public static class CharLengthFilter implements InputFilter
{ private int mMax; /**
*
* [构造简要说明]
*
* @param max max
*/
public CharLengthFilter(int max)
{
mMax = max;
} /**
*
* [一句话功能简述]<BR>
* [功能详细描述]
*
* @param source source
* @param start start
* @param end end
* @param dest dest
* @param dstart dstart
* @param dend dend
* @return CharSequence
* @see android.text.InputFilter#filter(java.lang.CharSequence, int,
* int, android.text.Spanned, int, int)
*/
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend)
{
int destLen = StringUtil.count2BytesChar(dest.toString());
int keep = mMax - (destLen - (dend - dstart));
int srcLen = StringUtil.count2BytesChar(source.toString());
if (keep <= 0)
{
return "";
}
else if (keep >= srcLen)
{
return null; // keep original
}
else
{
StringBuffer buffer = new StringBuffer();
int cnt = 0;
for (int i = start; i < end; i++)
{
char c = source.charAt(i);
cnt++;
if (c >= 256)
{
cnt++;
}
if (cnt <= keep)
{
buffer.append(c);
}
else
{
break;
}
}
return buffer.toString();
}
}
}}