FileDocCategorySizeDatePackage
CalculatorDisplay.javaAPI DocAndroid 1.5 API5167Wed May 06 22:42:42 BST 2009com.android.calculator2

CalculatorDisplay

public class CalculatorDisplay extends android.widget.ViewSwitcher
Provides vertical scrolling for the input/result EditText.

Fields Summary
private static final char[]
ACCEPTED_CHARS
private static final int
ANIM_DURATION
android.view.animation.TranslateAnimation
inAnimUp
android.view.animation.TranslateAnimation
outAnimUp
android.view.animation.TranslateAnimation
inAnimDown
android.view.animation.TranslateAnimation
outAnimDown
Constructors Summary
public CalculatorDisplay(android.content.Context context, android.util.AttributeSet attrs)

    
         
        super(context, attrs);
    
Methods Summary
android.widget.EditTextgetEditText()

        return (EditText) getCurrentView();
    
intgetSelectionStart()

        EditText text = (EditText) getCurrentView();
        return text.getSelectionStart();
    
android.text.EditablegetText()

        EditText text = (EditText) getCurrentView();
        return text.getText();
    
voidinsert(java.lang.String delta)

        EditText editor = (EditText) getCurrentView();
        int cursor = editor.getSelectionStart();
        editor.getText().insert(cursor, delta);
    
protected voidonFocusChanged(boolean gain, int direction, android.graphics.Rect prev)

        //Calculator.log("focus " + gain + "; " + direction + "; " + prev);
        if (!gain) {
            requestFocus();
        }
    
protected voidonSizeChanged(int w, int h, int oldW, int oldH)

        inAnimUp = new TranslateAnimation(0, 0, h, 0);
        inAnimUp.setDuration(ANIM_DURATION);
        outAnimUp = new TranslateAnimation(0, 0, 0, -h);
        outAnimUp.setDuration(ANIM_DURATION);

        inAnimDown = new TranslateAnimation(0, 0, -h, 0);
        inAnimDown.setDuration(ANIM_DURATION);
        outAnimDown = new TranslateAnimation(0, 0, 0, h);
        outAnimDown.setDuration(ANIM_DURATION);
    
protected voidsetLogic(Logic logic)

        NumberKeyListener calculatorKeyListener =
            new NumberKeyListener() {
                public int getInputType() {
                    // Don't display soft keyboard.
                    return InputType.TYPE_NULL;
                }
            
                protected char[] getAcceptedChars() {
                    return ACCEPTED_CHARS;
                }

                public CharSequence filter(CharSequence source, int start, int end,
                                           Spanned dest, int dstart, int dend) {
                    /* the EditText should still accept letters (eg. 'sin')
                       coming from the on-screen touch buttons, so don't filter anything.
                    */
                    return null;
                }
            };

        Editable.Factory factory = new CalculatorEditable.Factory(logic);
        for (int i = 0; i < 2; ++i) {
            EditText text = (EditText) getChildAt(i);
            text.setBackgroundDrawable(null);
            text.setEditableFactory(factory);
            text.setKeyListener(calculatorKeyListener);
        }
    
public voidsetOnKeyListener(OnKeyListener l)

        getChildAt(0).setOnKeyListener(l);
        getChildAt(1).setOnKeyListener(l);
    
voidsetSelection(int i)

        EditText text = (EditText) getCurrentView();
        text.setSelection(i);
    
voidsetText(java.lang.CharSequence text, com.android.calculator2.CalculatorDisplay$Scroll dir)

        if (getText().length() == 0) {
            dir = Scroll.NONE;
        }
        
        if (dir == Scroll.UP) {
            setInAnimation(inAnimUp);
            setOutAnimation(outAnimUp);            
        } else if (dir == Scroll.DOWN) {
            setInAnimation(inAnimDown);
            setOutAnimation(outAnimDown);            
        } else { // Scroll.NONE
            setInAnimation(null);
            setOutAnimation(null);
        }
        
        EditText editText = (EditText) getNextView();
        editText.setText(text);
        //Calculator.log("selection to " + text.length() + "; " + text);
        editText.setSelection(text.length());
        showNext();