以编程方式打开软键盘

Vig*_*esh 110 android android-softkeyboard

我有一个没有子窗口小部件的活动,相应的xml文件是,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想在活动开始时以编程方式打开软键盘.我现在尝试的是,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager != null) {
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
Run Code Online (Sandbox Code Playgroud)

给我一些指导.

Vig*_*esh 143

我使用以下行在onclick事件中手动显示软键盘,键盘可见.

InputMethodManager inputMethodManager =
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
    linearLayout.getApplicationWindowToken(),
    InputMethodManager.SHOW_FORCED, 0);
Run Code Online (Sandbox Code Playgroud)

但是当活动打开时我仍然无法打开它,那么有什么解决方案吗?

  • @Vignesh:如果要在启动活动时实现此代码,则需要在打开键盘时给出500毫秒的延迟. (5认同)

小智 115

在清单文件中,尝试将以下内容添加到<activity>活动开始时要显示键盘的位置:

android:windowSoftInputMode="stateVisible"

这应该会导致键盘在活动开始时变得可见.

有关更多选项,请查看文档.

  • 他说"以编程方式". (7认同)
  • 希望我能更频繁地为这个答案投票,只是为了让所有那些肮脏的黑客到达页面底部;) (3认同)
  • 它对我不起作用.相反,这样做:android:windowSoftInputMode ="stateAlwaysVisible" (2认同)

And*_*ger 33

请按照以下代码.我相信你的问题会得到解决.

if (imm != null){
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
   } 
Run Code Online (Sandbox Code Playgroud)


小智 24

这是有效的

<activity
    ...
    android:windowSoftInputMode="stateVisible" >
</activity>
Run Code Online (Sandbox Code Playgroud)

要么

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Run Code Online (Sandbox Code Playgroud)

  • 感谢您设置此程序的方式 - 这可行,而InputMethodManager黑客则不行. (2认同)

小智 16

我所需要的只是在非常精确的时刻曝光键盘.这对我有用!谢谢贝尼特斯.

    private Handler mHandler= new Handler();
Run Code Online (Sandbox Code Playgroud)

在非常精确的时刻:

    mHandler.post(
    new Runnable() {
        public void run() {
            InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
            yourEditText.requestFocus();
        }
    }); 
Run Code Online (Sandbox Code Playgroud)


Ram*_*Ram 12

我使用以下行在onclick事件中手动显示软键盘.

public void showKeyboard(final EmojiconEditText ettext){
          ettext.requestFocus();
          ettext.postDelayed(new Runnable(){
            @Override public void run(){
              InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              keyboard.showSoftInput(ettext,0);
            }
          }
        ,200);
        }
Run Code Online (Sandbox Code Playgroud)


Mar*_*tes 9

把它放在onResume方法中:

findViewById(R.id.root_view_of_your_activity_layout).post(
new Runnable() {
    public void run() {
        InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(yourEditText.getApplicationWindowToken(),     InputMethodManager.SHOW_FORCED, 0);
        yourEditText.requestFocus();
    }
});
Run Code Online (Sandbox Code Playgroud)

runnable是必需的,因为当OS触发onResume方法时,你无法确定绘制的所有视图,所以从你的根布局调用的post方法会让它等到每个视图都准备就绪.


Khe*_*raj 9

科特林

fun hideKeyboard(activity: Activity) {
    val view = activity.currentFocus
    val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    assert(view != null)
    methodManager.hideSoftInputFromWindow(view!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}

private fun showKeyboard(activity: Activity) {
    val view = activity.currentFocus
    val methodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    assert(view != null)
    methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
Run Code Online (Sandbox Code Playgroud)

爪哇

public static void hideKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    assert methodManager != null && view != null;
    methodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

private static void showKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    InputMethodManager methodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    assert methodManager != null && view != null;
    methodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 8

onCreate活动方法或onActivityCreated的片段

....
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            view.removeOnPreDrawListener(this);
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

            // !Pay attention to return `true`
            // Chet Haase told to 
            return true;
        }
    });
Run Code Online (Sandbox Code Playgroud)


Shi*_*kin 7

似乎这是有效的

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patientid);

        editText = (EditText)findViewById(R.id.selectPatient);
        //editText.requestFocus(); //works without that

    }

@Override
    protected void onResume() {

        findViewById(R.id.selectPatient).postDelayed(
        new Runnable() {
            public void run() {
                 editText.requestFocus();
                InputMethodManager inputMethodManager =  (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
            }
        },100);
        super.onResume();
    }
Run Code Online (Sandbox Code Playgroud)

似乎这更好:在清单中:

<application>
    <activity
        android:name="com.doodkin.myapp.ReportActivity"
        android:label="@string/title_activity_report"
        android:screenOrientation="sensor" 
        android:windowSoftInputMode="stateHidden" > // add this or stateVisible
    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

似乎清单在android 4.2.2中工作但在android 4.0.3中不起作用


Nou*_*eep 6

我用这样的方式以编程方式显示软键盘,这对我来说是有效的,可以防止在启动键盘时自动调整屏幕大小.

在清单中:

<activity android:name="XXXActivity" android:windowSoftInputMode="adjustPan">
</activity>
Run Code Online (Sandbox Code Playgroud)

在XXXActvity中:

EditText et =  (EditText))findViewById(R.id.edit_text);  
  Timer timer = new Timer();
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.toggleSoftInputFromWindow(et.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

                }
            };
            timer.schedule(task, 200);
Run Code Online (Sandbox Code Playgroud)

我认为这将节省其他人搜索此问题的时间.


Kai*_*ang 5

InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Run Code Online (Sandbox Code Playgroud)


Shy*_*dda 5

我将它用作单身人士,例如:

public static void showSoftKeyboard(final Context context, final EditText editText) {
        try {
            editText.requestFocus();
            editText.postDelayed(
                    new Runnable() {
                        @Override
                        public void run() {
                            InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                            keyboard.showSoftInput(editText, 0);
                        }
                    }
                    , 200);
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

在您的活动中使用它,例如:

showSoftKeyboard(this, yourEditTextToFocus);
Run Code Online (Sandbox Code Playgroud)


Thi*_*lva 5

这有效:

private static void showKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
Run Code Online (Sandbox Code Playgroud)

你可以这样调用这个方法:

showKeyboard(NameOfActivity.this);
Run Code Online (Sandbox Code Playgroud)