android默认键盘布局

问题描述 投票:0回答:1

我正在为nexus 7开发一个应用程序,我需要某些EditText来显示带有数字和特殊字符的键盘视图。我知道您可以使用inputType设置EditText的布局,但我的问题是如果我设置inputType =“number”,则会出现拨号盘,并且无法切换到字符视图。我所需要的(是客户的要求)是打开键盘,当你点击左下方的“123”键时显示的布局。我已经尝试了setRawInputType和setInputType的所有组合而没有运气。这个组合显示了拨号盘

txtLineCode.setInputType(InputType.TYPE_CLASS_TEXT);
txtLineCode.setRawInputType(InputType.TYPE_CLASS_NUMBER);

这个组合也显示了拨号盘

txtLineCode.setInputType(InputType.TYPE_CLASS_TEXT);
txtLineCode.setRawInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL|InputType.TYPE_CLASS_NUMBER);

以下是更好地解释我需要的截图

这是默认键盘

这是我点击“?123”时显示的布局,这是我默认需要显示的内容

如果我设置inputType =“number”,则显示布局,不允许切换到lecters布局

目前我的一些EditText是普遍的数字,但应该包含数字,我该怎么办?

非常感谢

android android-softkeyboard android-keypad keyboard-layout
1个回答
1
投票

我想我找到了一个非常优雅的解决方案:我在文本框中使用了Drawable(在我的情况下是drawableRight),我在drawable上分配了一个点击监听器,用于执行数字和文本模式之间的切换。我可以使用Handling click events on a drawable within an EditText中的一个小技巧在drawable上分配一个监听器:

public class MyEdittextextends EditText {

    private Drawable drawableRight;
    private Drawable drawableLeft;
    private Drawable drawableTop;
    private Drawable drawableBottom;


//YOUR STUFF HERE

@Override
    public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
        if (right != null) {
            drawableRight = right;
        }

        if (left != null) {
            drawableLeft = left;
        }
        super.setCompoundDrawables(left, top, right, bottom);
    }
View.OnClickListener _leftDrawableClickListener = null;
View.OnClickListener _rightDrawableClickListener = null;

public void setLeftDrawableClickListener(View.OnClickListener clickListener) {
    _leftDrawableClickListener = clickListener;
}

public void setRightDrawableClickListener(View.OnClickListener clickListener) {
    _rightDrawableClickListener = clickListener;
}

@Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int x, y;
            Rect bounds;
            x = (int) event.getX();
            y = (int) event.getY();
            // this works for left since container shares 0,0 origin with bounds
            if (drawableLeft != null) {
                bounds = drawableLeft.getBounds();
                if (bounds.contains(x - fuzz, y - fuzz)) {
                    try {
                        _leftDrawableClickListener.onClick(this);
                    } catch (Exception e) {
                    }
                    if (consumeEvent) {
                        event.setAction(MotionEvent.ACTION_CANCEL);
                        return false;
                    }
                }
            } else if (drawableRight != null) {
                bounds = drawableRight.getBounds();
                if (x >= (this.getRight() - bounds.width() - fuzz) && x <= (this.getRight() - this.getPaddingRight() + fuzz) && y >= (this.getPaddingTop() - fuzz) && y <= (this.getHeight() - this.getPaddingBottom()) + fuzz) {

                    try {
                        _rightDrawableClickListener.onClick(this);
                    } catch (Exception e) {
                    }
                    if (consumeEvent) {
                        event.setAction(MotionEvent.ACTION_CANCEL);
                        return false;
                    }
                }
            } else if (drawableTop != null) {
                // not implemented yet
            } else if (drawableBottom != null) {
                // not implemented yet
            }
        }

        return super.onTouchEvent(event);
    }


@Override
    protected void finalize() throws Throwable {
        drawableRight = null;
        drawableBottom = null;
        drawableLeft = null;
        drawableTop = null;
        super.finalize();
    }

}

一旦创建了自定义EditText,我就在我的Activity中使用了这段代码

myEdittext = (EditText) findViewById(R.id.myEdittext);
        myEdittext.setRightDrawableClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (myEdittext.getInputType() != InputType.TYPE_CLASS_TEXT) {
                    myEdittext.setInputType(InputType.TYPE_CLASS_TEXT);
                    myEdittext.setRawInputType(InputType.TYPE_CLASS_TEXT);
                    myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_123, 0);

                } else {
                    myEdittext.setRawInputType(InputType.TYPE_CLASS_NUMBER);
                    myEdittext.setInputType(InputType.TYPE_CLASS_NUMBER);
                    myEdittext.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.keyboard_abc, 0);
                }

            }
        });

这就是结果:首次显示EditText时会出现这样的情况

并点击“ABC”图像变为这样

希望这可以帮助别人

© www.soinside.com 2019 - 2024. All rights reserved.