[英]change drawable of cursor in EditText
我嘗試以不同的方式和不同的順序在 afterTextChanged() 中更改可繪制光標。 我究竟做錯了什么? 在 EditText 中輸入字符后如何更改光標?
public class View extends ConstraintLayout {
private EditText editText;
public View (Context context, AttributeSet attrs) {
super(context, attrs);
editText = findViewById(R.id.editText);
editText.setTextCursorDrawable(R.drawable.first_cursor);
editText.setCursorVisible(true);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
editText.setCursorVisible(false);
editText.setTextCursorDrawable(null);
editText.setTextCursorDrawable(R.drawable.second_cursor);
requestLayout();
editText.invalidate();
invalidate();
editText.setCursorVisible(true);
}
在第二個光標中,我想為它設置填充。
它幫助我:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/start"
android:right="16px">
<shape>
<size
android:width="4px"
android:height="64px" />
<solid android:color="@color/color1" />
<corners android:radius="4px" />
</shape>
</item>
<item
android:id="@+id/end"
android:left="@dimen/16px">
<shape>
<size
android:width="4px"
android:height="64px" />
<solid android:color="@color/color1" />
<corners android:radius="4px" />
</shape>
</item>
</layer-list>
游標文件
public class View extends ConstraintLayout {
private EditText editText;
private boolean isVisibleLayerEnd = false;
public View (Context context, AttributeSet attrs) {
super(context, attrs);
editText = findViewById(R.id.editText);
LayerDrawable drawable = (LayerDrawable) getResources().
getDrawable(R.drawable.cursor);
Drawable layerStart = drawable.findDrawableByLayerId(R.id.start);
Drawable layerEnd = drawable.findDrawableByLayerId(R.id.end);
layerEnd.setAlpha(0);
editText.setTextCursorDrawable(drawable);
editText.setCursorVisible(true);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int
after) {}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int
count) {}
@Override
public void afterTextChanged(Editable s) {
if (editText.getText().length() >= 1 && !isVisibleLayerEnd) {
layerStart.setAlpha(0);
layerEnd.setAlpha(255);
isVisibleLayerEnd = true;
}
if (mPrimaryEditText.getText().length() == 0) {
layerStart.setAlpha(255);
layerEnd.setAlpha(0);
isVisibleLayerEnd = false;
}
editText.setCursorVisible(true);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.