如何在android中创建和实现数字键盘

时间:2014-04-22 08:20:08

标签: android custom-controls android-keypad

我创建了一个生成按钮运行时的类,我希望将该类实现为特定编辑文本字段中的键盘,我该怎么做。我班的代码如下:

public class KeyboardView extends View
{

    LinearLayout verticalLinearLayout, horizontalLinearLayout;
    Button button, btnZero, btnPoint, btnDone;




    public KeyboardView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public KeyboardView(Context context,AttributeSet attributeSet) {
        super(context, attributeSet);
        // TODO Auto-generated constructor stub
    }

    public KeyboardView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        // TODO Auto-generated constructor stub
    }

    public LinearLayout addView(){
        verticalLinearLayout = new LinearLayout(getContext());
        verticalLinearLayout.setOrientation(LinearLayout.VERTICAL);
        int count = 1;
        for (int i = 1; i <= 3; i++) {
            horizontalLinearLayout = new LinearLayout(getContext());
            for (int j = 1; j <= 3; j++) {

                button = new Button(getContext());
                button.setText(String.valueOf(count++));
                button.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        ((Button)v).getText();
                    }
                });
                horizontalLinearLayout.addView(button);

            }
            verticalLinearLayout.addView(horizontalLinearLayout);
        }

        horizontalLinearLayout = new LinearLayout(getContext());

        btnZero = new Button(getContext());
        btnZero.setText("0");
        horizontalLinearLayout.addView(btnZero);

        btnPoint = new Button(getContext());
        btnPoint.setText(".");
        horizontalLinearLayout.addView(btnPoint);

        btnDone = new Button(getContext());
        btnDone.setText("Done");
        horizontalLinearLayout.addView(btnDone);

        verticalLinearLayout.addView(horizontalLinearLayout);

        return verticalLinearLayout;

    }


    public Button getButton() {
        return button;
    }

    public Button getBtnZero() {
        return btnZero;
    }

    public Button getBtnPoint() {
        return btnPoint;
    }

    public Button getBtnDone() {
        return btnDone;
    }
}

我想在某些特定的编辑文本视图中将此类实现为键盘,请任何人帮助我吗?

1 个答案:

答案 0 :(得分:0)

您可以关注2个链接。比如

1号#:Creating a Numeric Keypad with GridLayout

和2号#Introduction to Android development : TouchCalculator

我希望你会从这些链接中得到帮助。祝你好运!