Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改EditText中光标的可绘制性_Java_Cursor_Drawable - Fatal编程技术网

Java 更改EditText中光标的可绘制性

Java 更改EditText中光标的可绘制性,java,cursor,drawable,Java,Cursor,Drawable,我尝试以不同的方式和顺序更改afterTextChanged()中的可绘制光标。 我做错了什么? 在EditText中输入字符后如何更改光标 public class View extends ConstraintLayout { private EditText editText; public View (Context context, AttributeSet attrs) { super(context, attrs); editText = findViewByI

我尝试以不同的方式和顺序更改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);
     }