简体   繁体   English

Android:强制键盘出现并专注于EditText

[英]Android: force keyboard to appear and focus on EditText

Thanks in advance for the help. 在此先感谢您的帮助。

I would like a keyboard to appear at the end or during the execution of the following code. 我想在最后或执行以下代码时出现键盘。

    // edit text
    EditText editText = new EditText(activity);
    editText.setBackgroundColor(Color.WHITE);
    editText.setGravity(Gravity.TOP);
    editText.setText("Type here...");

    RelativeLayout relativeLayoutEditText = new RelativeLayout(activity);
    RelativeLayout.LayoutParams paramRelativeLayoutEditText = new RelativeLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, 43 * display.getHeight() / 80 );
    paramRelativeLayoutEditText.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    relativeLayoutEditText.addView(editText,paramRelativeLayoutEditText);

    // add all created views to rootView
    rootView.addView(relativeLayoutEditText, paramEditText);

    // open keyboard
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

but the keyboard only appears after touching the editText field (ie with my finger). 但键盘只在触摸editText字段后出现(即用我的手指)。 Is there a way that I can make the board automatically appear without using a physical touch? 有没有办法让我可以在不使用物理触摸的情况下自动显示电路板?

As an aside I know that how I am specifying the width and height isn't exactly the right way to do things. 顺便说一句,我知道我如何指定宽度和高度并不是正确的做事方式。

Thanks to @Shubham's link I was able to figure it out. 感谢@ Shubham的链接,我能够弄明白。 The solution was not the answer given in the link, however. 然而,解决方案不是链接中给出的答案。 It was the second answer. 这是第二个答案。

editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Edit: 编辑:

once using the above solution the keyboard will remain on the screen until a user presses either the back button or the home button (sometimes it takes a few times). 一旦使用上述解决方案,键盘将保留在屏幕上,直到用户按下后退按钮或主页按钮(有时需要几次)。 To remove the keyboard use this 要删除键盘,请使用此功能

imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0);

in my case rootView is the rootView of the current activity. 在我的情况下,rootView是当前活动的rootView。 I have not tested this to see if this will work on child views. 我没有测试过这个,看看这是否适用于子视图。

Add

    editText.requestFocus();

Try this: 试试这个:

EditText editText = (EditText ) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

ensure that the edittext is focusable in touch mode. 确保edittext在触摸模式下可聚焦。 You can do it two way. 你可以两种方式做到这一点。

In xml: 在xml中:

 android:focusableInTouchMode="true"

in Java: 在Java中:

editText.setFocusableInTouchMode(true);

Try This 试试这个

EditText textView = (EditText ) findViewById(R.id.myTextViewId);
textView.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);

http://developer.android.com/reference/android/view/View.html#requestFocus() http://developer.android.com/reference/android/view/View.html#requestFocus()

Possible answer link . 可能的答案链接 Many of the above are also sourced from this link. 以上许多内容也来自此链接。

Try This : 试试这个 :

EditText textView = (EditText )findViewById(R.id.myTextViewId);
textView.requestFocus();    
InputMethodManager imm = 
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);

this will show the keypad 这将显示键盘

InputMethodManager imm = (InputMethodManager) 
        OrderMainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(txtCustomerId, InputMethodManager.SHOW_IMPLICIT);

enter your activity name instead of this if you calling keypad from the listeners eg. 如果您从监听器调用键盘,请输入您的活动名称而不是此名称。 button click 按钮单击

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

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