Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.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
Android 使自定义编辑文本仅为数字_Android_Android Edittext_Custom Controls_Android Custom View - Fatal编程技术网

Android 使自定义编辑文本仅为数字

Android 使自定义编辑文本仅为数字,android,android-edittext,custom-controls,android-custom-view,Android,Android Edittext,Custom Controls,Android Custom View,我有一个自定义的编辑文本,我从 问题:默认情况下,这会打开普通键盘。我想打开数字键盘。我尝试在XML中添加inputType=“number”,但随后它停止显示占位符行 如何在显示占位符行的同时打开数字键盘? 另外,如何从类内部设置maxLength 代码如下: public class PinEntryEditText extends android.support.v7.widget.AppCompatEditText { public static final String X

我有一个自定义的编辑文本,我从

问题:默认情况下,这会打开普通键盘。我想打开数字键盘。我尝试在XML中添加
inputType=“number”
,但随后它停止显示占位符行

如何在显示占位符行的同时打开数字键盘?

另外,如何从类内部设置
maxLength

代码如下:

public class PinEntryEditText extends android.support.v7.widget.AppCompatEditText {

    public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android";

    private float mSpace = 10; //24 dp by default, space between the lines
    private float mCharSize;
    private float mNumChars = 6;
    private float mLineSpacing = 8; //8dp by default, height of the text from our lines
    private int mMaxLength = 6;
    private int pinLength;
    private OnClickListener mClickListener;
    private float mLineStroke = 1; //1dp by default
    private float mLineStrokeSelected = 2; //2dp by default
    private Paint mLinesPaint;
    int[][] mStates = new int[][]{
        new int[]{android.R.attr.state_selected}, // selected
        new int[]{android.R.attr.state_focused}, // focused
        new int[]{-android.R.attr.state_focused}, // unfocused
    };

    int[] mColors = new int[]{
        Color.GREEN,
        Color.BLACK,
        Color.GRAY
    };

    ColorStateList mColorStates = new ColorStateList(mStates, mColors);

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

    public PinEntryEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PinEntryEditText , 0, 0);
        try {
            pinLength = ta.getInteger(R.styleable.PinEntryEditText_pinLength , mMaxLength);
        } finally {
            ta.recycle();
        }

        mNumChars = pinLength;
        mMaxLength = pinLength;

        init(context, attrs);
    }

    public PinEntryEditText(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;
        mLineStrokeSelected = multi * mLineStrokeSelected;
        mLinesPaint = new Paint(getPaint());
        mLinesPaint.setStrokeWidth(mLineStroke);
        if (!isInEditMode()) {
            TypedValue outValue = new TypedValue();

context.getTheme().resolveAttribute(R.attr.colorControlActivated,
                outValue, true);
        final int colorActivated = outValue.data;
        mColors[0] = colorActivated;

        context.getTheme().resolveAttribute(R.attr.colorPrimaryDark,
                outValue, true);
        final int colorDark = outValue.data;
        mColors[1] = colorDark;

        context.getTheme().resolveAttribute(R.attr.colorControlHighlight,
                outValue, true);
        final int colorHighlight = outValue.data;
        mColors[2] = colorHighlight;
    }
    setBackgroundResource(0);
    mSpace = multi * mSpace; //convert to pixels for our density
    mLineSpacing = multi * mLineSpacing; //convert to pixels for our density
    mMaxLength = attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", mMaxLength);
    mNumChars = mMaxLength;

    //Disable copy paste
    super.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        public void onDestroyActionMode(ActionMode mode) {
        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }
    });
    // When tapped, move cursor to end of text.
    super.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            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) {
    //super.onDraw(canvas);
    int availableWidth = getWidth() - getPaddingRight() - getPaddingLeft();
    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++) {
        updateColorForLines(i == textLength);
        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;
        }
    }
}


private int getColorForState(int... states) {
    return mColorStates.getColorForState(states, Color.GRAY);
}

/**
 * @param next Is the current char the next character to be input?
 */
private void updateColorForLines(boolean next) {
    if (isFocused()) {
        mLinesPaint.setStrokeWidth(mLineStrokeSelected);
        mLinesPaint.setColor(getColorForState(android.R.attr.state_focused));
        if (next) {
            mLinesPaint.setColor(getColorForState(android.R.attr.state_selected));
        }
    } else {
        mLinesPaint.setStrokeWidth(mLineStroke);
        mLinesPaint.setColor(getColorForState(-android.R.attr.state_focused));
    }
}

}
公共类PinEntryEditText扩展了android.support.v7.widget.appcompatiedittext{
公共静态最终字符串XML_NAMESPACE_ANDROID=”http://schemas.android.com/apk/res/android";
private float mSpace=10;//默认情况下为24 dp,行之间的空间
私人浮动麦克哈斯化;
私有浮动mNumChars=6;
private float mlinespace=8;//8dp默认情况下,文本距行的高度
专用内网mMaxLength=6;
私人整数长度;
私有OnClickListener-mcclicklistener;
私有float mLineStroke=1;//默认为1dp
private float mLineStrokeSelected=2;//默认为2dp
私人涂料;
int[]mStates=新int[]{
新建int[]{android.R.attr.state_selected},//selected
新int[]{android.R.attr.state_focused},//focused
新int[]{-android.R.attr.state_-focused},//未聚焦
};
int[]mColors=新int[]{
颜色,绿色,
颜色,黑色,
颜色:灰色
};
ColorStateList mColorStates=新的ColorStateList(mStates,mColors);
公共PinEntryEditText(上下文){
超级(上下文);
}
公共PinEntryEditText(上下文、属性集属性){
超级(上下文,attrs);
TypedArray ta=context.ActainStyledAttributes(attrs,R.styleable.PinEntryEditText,0,0);
试一试{
pinLength=ta.getInteger(R.styleable.PinEntryEditText_pinLength,mMaxLength);
}最后{
ta.recycle();
}
mNumChars=pinLength;
Mmax长度=销长度;
init(上下文,attrs);
}
公共PinEntryEditText(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
init(上下文,attrs);
}
私有void init(上下文上下文、属性集属性){
float multi=context.getResources().getDisplayMetrics().density;
mLineStroke=多*mLineStroke;
mLineStrokeSelected=多*mLineStrokeSelected;
mLinesPaint=新油漆(getPaint());
MLINESTAINT.设定行程宽度(MLINESTRAKE);
如果(!isInEditMode()){
TypedValue outValue=新的TypedValue();
context.getTheme().resolveAttribute(R.attr.colorControlActivated,
超值,正确);
最终int colorActivated=outValue.data;
mColors[0]=颜色激活;
context.getTheme().resolveAttribute(R.attr.colorPrimaryDark,
超值,正确);
最终int colorDark=outValue.data;
mColors[1]=彩色暗;
context.getTheme().resolveAttribute(R.attr.colorControlHighlight,
超值,正确);
最终int colorHighlight=outValue.data;
mColors[2]=彩色高光;
}
挫折资源(0);
mSpace=multi*mSpace;//转换为密度的像素
mlinespace=multi*mlinespace;//转换为密度的像素
mMaxLength=attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID,“maxLength”,mMaxLength);
mNumChars=mm x长度;
//禁用复制粘贴
super.setCustomSelectionActionModeCallback(新的ActionMode.Callback(){
公共布尔onPrepareActionMode(操作模式,菜单){
返回false;
}
公共void onDestroyActionMode(ActionMode模式){
}
公共布尔onCreateActionMode(ActionMode模式,菜单){
返回false;
}
公共布尔值onActionItemClicked(ActionMode模式,菜单项){
返回false;
}
});
//点击后,将光标移动到文本的末尾。
super.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
setSelection(getText().length());
if(mClickListener!=null){
mclicklister.onClick(v);
}
}
});
}
@凌驾
公共void setOnClickListener(OnClickListener l){
MCL=l;
}
@凌驾
公共无效设置CustomSelectionActionModeCallback(ActionMode.Callback actionModeCallback){
抛出新的运行时异常(“不支持setCustomSelectionActionModeCallback()”);
}
@凌驾
受保护的void onDraw(画布){
//super.onDraw(帆布);
int availableWidth=getWidth()-getPaddingRight()-getPaddingLeft();
如果(mSpace<0){
mCharSize=(可用宽度/(mNumChars*2-1));
}否则{
mCharSize=(可用宽度-(mSpace*(mNumChars-1))/mNumChars;
}
int startX=getPaddingLeft();
int bottom=getHeight()-getPaddingBottom();
//文本宽度
可编辑文本=getText();
int textLength=text.length();
float[]textWidths=新的float[textLength];
getPaint().getTextWidths(getText(),0,textLength,textWidths);
对于(int i=0;ii){
中间浮动=startX+mCharSize/2;
drawText(text,i,i+1,中间-textwidhs[0]/2,底部-mlinespace,getPaint());
}
如果(mSpace<0){
startX+=mCharSize*2;
}否则{
startX+=mCharSize+mSpace;
}
}
}
私有整型getColorForState(整型…状态){
返回mColorStates.getColorForState(states,Color.GRAY);
}
/**
*@param next是要输入的下一个字符的当前字符
<com.mridulahuja.kudamm.tools.PinEntryEditText
        android:id="@+id/txtToken"
        android:layout_width="0dp"
        android:layout_height="55dp"
        android:ems="10"
        pin:pinLength="6"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        app:layout_constraintLeft_toLeftOf="parent"/>
private int mMaxLength = 6;
mMaxLength = attrs.getAttributeIntValue(XML_NAMESPACE_ANDROID, "maxLength", mMaxLength);
android:digits="0123456789_"