android 代码EditText.setHint后 输入不了
--------------------编程问答-------------------- 贴相关完整代码 --------------------编程问答-------------------- 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--------------------编程问答-------------------- 骂的 搞的半天是我 逻辑有问题 --------------------编程问答-------------------- 是isChanged?我还正在看 --------------------编程问答-------------------- 恩的 谢谢您咯 帅锅 --------------------编程问答--------------------
{
/**
* 属性名
*/
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