Code example for EditText

Methods: getTextsetSelectiongetSelectionStartsetInputTypesetText

0
 
    private void updateTextInputType() { 
        int inputType = InputType.TYPE_CLASS_NUMBER;
        if (isSignedNumberAllowed()) 
            inputType |= InputType.TYPE_NUMBER_FLAG_SIGNED;
        mText.setInputType(inputType);
    } 
 
    public void setCurrent(int current) {
        setCurrentInternal(current);
        updateView(); 
    } 
 
    public void setCurrentAndNotify(int current) {
        setCurrentInternal(current);
        notifyChange(); 
        updateView(); 
    } 
 
    private void setCurrentInternal(int current) {
        if (mStart > current) throw new IllegalArgumentException("Current value cannot be less than the range start.");
        if (mEnd < current) throw new IllegalArgumentException("Current value cannot be greater than the range end.");
        mCurrent = current;
    } 
 
    /** 
     * The speed (in milliseconds) at which the numbers will scroll 
     * when the the +/- buttons are longpressed. Default is 300ms. 
     */ 
    public void setSpeed(long speed) {
        mSpeed = speed;
    } 
 
    @Override 
    public void onClick(View v) {
        validateInput(mText);
        if (!mText.hasFocus()) mText.requestFocus();
 
        // now perform the increment/decrement 
        if (R.id.increment == v.getId()) {
            changeCurrent(mCurrent + 1);
        } else if (R.id.decrement == v.getId()) {
            changeCurrent(mCurrent - 1);
        } 
    } 
 
    protected String formatNumber(int value) {
        return (mFormatter != null)
                ? mFormatter.toString(value)
                : String.valueOf(value);
    } 
 
    protected void changeCurrent(int current) {
        // Wrap around the values if we go past the start or end 
        if (current > mEnd) {
            current = mWrap ? mStart : mEnd;
        } else if (current < mStart) {
            current = mWrap ? mEnd : mStart;
        } 
        mPrevious = mCurrent;
        mCurrent = current;
 
        notifyChange(); 
        updateView(); 
    } 
 
    protected void notifyChange() { 
        if (mListener != null) {
            mListener.onChanged(this, mPrevious, mCurrent);
        } 
    } 
 
    protected void updateView() { 
        int selectPos;
 
        /* If we don't have displayed values then use the 
         * current number else find the correct value in the 
         * displayed values for the current number. 
         */ 
        if (mDisplayedValues == null) {
            int curSelectedPos = mText.getSelectionStart();
            int curTextLength = mText.getText().length();
             
            mText.setText(formatNumber(mCurrent));
             
            /* Preserve previous cursor position (right justified) */ 
            selectPos = mText.getText().length();
            selectPos -= (curTextLength - Math.max(curSelectedPos, 0));
            selectPos = Math.max(selectPos, 0);
        } else { 
            mText.setText(mDisplayedValues[mCurrent - mStart]);
            selectPos = mText.getText().length();
        } 
        mText.setSelection(selectPos);
    } 
 
    private static int constrain(int x, int min, int max) {
        return Math.min(Math.max(x, min), max);
    }