如何更改 OtpEditText 中的文本方向?

how to change text direction in OtpEditText?

我正在使用此 class 创建自定义编辑文本,但我无法更改输入文本的方向 你知道我该如何改变它吗? 我已经尝试过 textDirection 和 LayoutDirection 但不起作用

提前致谢

(Building a PinEntryEditText in Android)

这是class

package com.example.jorvajoor;

import android.content.Context;
        import android.graphics.Canvas;
        import android.graphics.Paint;
        import android.text.Editable;
        import android.util.AttributeSet;
        import android.view.ActionMode;
        import android.view.View;
        import androidx.appcompat.widget.AppCompatEditText;

public class OTPeditText extends AppCompatEditText {

private float mSpace = 24; //24 dp by default, space between the lines
private float mNumChars = 4;
private float mLineSpacing = 8; //8dp by default, height of the text from our lines
private int mMaxLength = 5;
private float mLineStroke = 2;
private Paint mLinesPaint;
private OnClickListener mClickListener;

public OTPeditText(Context context) {
    super(context);
}

public OTPeditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

public OTPeditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    float multi = context.getResources().getDisplayMetrics().density;
    mLineStroke = multi * mLineStroke;
    mLinesPaint = new Paint(getPaint());
    mLinesPaint.setStrokeWidth(mLineStroke);
    mLinesPaint.setColor(getResources().getColor(R.color.black));
    setBackgroundResource(0);
    mSpace = multi * mSpace; //convert to pixels for our density
    mLineSpacing = multi * mLineSpacing; //convert to pixels for our density
    mNumChars = mMaxLength;

    super.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // When tapped, move cursor to end of text.
            setSelection(getText().length());
            if (mClickListener != null) {
                mClickListener.onClick(v);
            }
        }
    });
}

@Override
public void setOnClickListener(OnClickListener l) {
    mClickListener = l;
}

@Override
public void setCustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback) {
    throw new RuntimeException("setCustomSelectionActionModeCallback() not supported.");
}

@Override
protected void onDraw(Canvas canvas) {
    int availableWidth = getWidth() - getPaddingRight() - getPaddingLeft();
    float mCharSize;
    if (mSpace < 0) {
        mCharSize = (availableWidth / (mNumChars * 2 - 1));
    } else {
        mCharSize = (availableWidth - (mSpace * (mNumChars - 1))) / mNumChars;
    }

    int startX = getPaddingLeft();
    int bottom = getHeight() - getPaddingBottom();

    //Text Width
    Editable text = getText();
    int textLength = text.length();
    float[] textWidths = new float[textLength];
    getPaint().getTextWidths(getText(), 0, textLength, textWidths);


    for (int i = 0; i < mNumChars; i++) {
        canvas.drawLine(startX, bottom, startX + mCharSize, bottom, mLinesPaint);
        if (getText().length() > i) {
            float middle = startX + mCharSize / 2;
            canvas.drawText(text, i, i + 1, middle - textWidths[0] / 2, bottom - mLineSpacing, getPaint());
        }
        if (mSpace < 0) {
            startX += mCharSize * 2;
        } else {
            startX += mCharSize + mSpace;
        }
    }
}
}

这就是我在 XML 文件中使用它的方式

 <com.example.jorvajoor.OTPeditText
                android:id="@+id/word1"
                android:layout_width="@dimen/_200sdp"
                android:layout_height="wrap_content"
                android:cursorVisible="true"
                android:inputType="text"
                android:gravity="center"
                android:letterSpacing="@integer/material_motion_duration_short_1"
                android:textIsSelectable="true"
                android:imeOptions="actionDone"
                android:textSize="20sp"/>

添加清单文件。

<manifest ... >
    ...
    <application ...
        android:supportsRtl="true">
    </application>
</manifest>

我想不通 但我改用 PinEntryEditText 轻松解决了我的问题 只需要添加文字方向