您的位置:首页 > 移动开发 > Android开发

android EditView自动判断输入长度,并限制字数,然后将光标停在最后

2014-11-11 15:28 489 查看
<EditText
android:id="@+id/myinfoedit_remark_et"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:background="@color/white"
android:gravity="top"
android:hint="我的签名"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:textColor="@color/text_222222"
android:textColorHint="@color/text_c4c4c4"
android:textSize="14dp" />


<TextView
android:id="@+id/shufuxianzhi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#016cd9"
android:textSize="12dp" />

mRemark_Et.addTextChangedListener(new TextWatcher()
{//EditView自动判断输入长度

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s)
{
String content = mRemark_Et.getText().toString();
int l = content.length();
mRemarkXianZhi.setText(l + "");//需要将数字转成字符串
if (l > 30)
{
mRemark_Et.setText(content.substring(0, 30));
mRemark_Et.setSelection(30);//EditView设置光标到最后
}

}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: