修改 EditText 中的光标位置

时间:2023-01-22 22:40:12

java

EditText et = ... 
String text = "text";
et.setText(text);
et.setSelection(text.length());


xml

        <EditText
android:background="@null"
android:id="@+id/et_nick_con"
style="@style/style_textItem"
android:textColor="@color/gray"
android:maxEms="30"
android:lines="2"
android:gravity="left|center_vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="dongxiang_android_sdk@aliyun.com"
android:focusableInTouchMode="true"
android:focusable="true"
android:selectAllOnFocus="true" />



EditText 的 文本输入监听 TextWatcher使用方法

nickCon.addTextChangedListener(mtWatcher);


private class MyTextWatcher implements TextWatcher{
String beforeStr="";

/** 这个是每个字符改变前的 字符串*/
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
beforeStr=s+"";
Log.d(TAG, "beforeTe = "+beforeStr);

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextCh = "+s);

}

@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "Editable = "+s);
if (s.toString().equals(nickNameRes)) {
titleSave.setEnabled(false);
}else {
titleSave.setEnabled(true);
}
}

}