简体   繁体   English

我怎么能在画布上显示软键盘并在android上写它

[英]How I can show soft keyboard on canvas and write on it in android

I am trying to write on the canvas by the soft keyboard in android. 我试图通过android中的软键盘在画布上写。

My question is how can I show soft keyboard and handle events it generate on the canvas. 我的问题是如何显示软键盘并处理它在画布上生成的事件。 My code is : 我的代码是:

public class DrawView extends View {
            Paint paint = new Paint();
            private static final String LOGID = "MxView";
            String message = "No key pressed yet.";
            DrawView(Context context) {
                    super(context);
                    setFocusable(true);
            }

            @Override
            protected void onDraw(Canvas canvas) {

                    canvas.drawText(message, 5, 20, paint);

            }
            @Override
            public boolean onKeyDown(int keyCode, KeyEvent ev) {
                    switch(keyCode) {
                    case KeyEvent.KEYCODE_ENTER:
                            message = "Key is Enter!";
                            Log.i(LOGID, message);
                            break;
                    case KeyEvent.KEYCODE_E:
                            message = "Key is E!";
                            Log.i(LOGID, message);
                            break;
                    default:
                            return false;
                    }
                    invalidate();
                    return true;
            }
        }

You can probably do this by providing "Menu" to your screen. 您可以通过在屏幕上提供“菜单”来实现此目的。 When user select that option, do following actions. 当用户选择该选项时,请执行以下操作。

  • Allow user to touch screen, this is the place he want to place his text 允许用户触摸屏幕,这是他想要放置文本的地方
  • Then create a "Custom Dialog" which takes input from user as you want. 然后创建一个“自定义对话框”,根据需要从用户获取输入。
  • Return those text to your screen and write text where user touched. 将这些文本返回到屏幕并在用户触摸时写入文本。

Second way is to view soft keyboard by following code. 第二种方法是通过以下代码查看软键盘。

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);

This will show you soft keyboard, and to take argument from keyboard, use following code. 这将显示软键盘,并从键盘获取参数,使用以下代码。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    Log.d("TEST", "Key Down  :" + keyCode + " String : " + s);
    s += (char) event.getUnicodeChar();

    return super.onKeyDown(keyCode, event);
}

Here, "s" is String object which store pressed values. 这里,“s”是存储按下值的String对象。

To hide keyboard, 要隐藏键盘,

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(rel.getWindowToken(), 0);

Here, rel is RelativeLayout . 这里, relRelativeLayout You can pass any view here. 你可以在这里传递任何视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM