以编程方式打开软键盘

我有一个没有子窗口小部件的活动,相应的 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>

我想在活动开始的时候以编程的方式打开软键盘,

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);
}

给我一些指导。

159517 次浏览

我使用以下代码行在 onclick 事件中手动显示软键盘,键盘是可见的。

InputMethodManager inputMethodManager =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
linearLayout.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);

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

请遵循以下代码。我相信你的问题将得到解决。

if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}

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

android:windowSoftInputMode="stateVisible"

这应该会使键盘在活动开始时变得可见。

有关更多选项,请检查 文件

把它放在 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();
}
});

需要使用 runable 是因为当操作系统触发 onResume 方法时,您无法确保所有的视图都在其中绘制,所以从根布局调用的 post 方法会让它等到每个视图都准备好。

所有我需要的是暴露键盘,在一个非常精确的时刻。这为我工作! 感谢贝尼特斯。

    private Handler mHandler= new Handler();

在这个非常精确的时刻:

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

我已经用这样的程序显示软键盘,这是为我工作,以防止自动调整屏幕大小,而启动键盘。

清单:

<activity android:name="XXXActivity" android:windowSoftInputMode="adjustPan">
</activity>

活动:

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);

我认为这将节省其他人寻找这个问题的时间。

这是工作

<activity
...
android:windowSoftInputMode="stateVisible" >
</activity>

或者

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

看起来起作用了

 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();
}

看起来这个方法更有效: 清单:

<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>

似乎清单工作在 android 4.2.2,但不工作在 android 4.0

我使用以下代码行在 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);
}

在活性的 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;
}
});

类似于@ShimonDoodkin 的答案,这是我在一个片段中所做的。

Https://stackoverflow.com/a/29229865/2413303

    passwordInput.postDelayed(new ShowKeyboard(), 300); //250 sometimes doesn't run if returning from LockScreen

ShowKeyboard在哪里

private class ShowKeyboard implements Runnable {
@Override
public void run() {
passwordInput.setFocusableInTouchMode(true);
passwordInput.requestFocus();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(passwordInput, 0);
}
}

输入成功后,我还要确保隐藏键盘

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getView().getWindowToken(), 0);

这是必需的源代码:

public static void openKeypad(final Context context, final View v)
{
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
InputMethodManager inputManager =   (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
Log.e("openKeypad", "Inside Handler");
}
},300);}

有关详情,请通过这个链接。这对我有帮助。 Https://github.com/nikhillosalka/keyboard/blob/master/readme.md

InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

已经有太多答案了,但除了这个,没有一个对我有用

inputMethodManager.showSoftInput(emailET,InputMethodManager.SHOW_FORCED);

我用了 showSoftInputSHOW_FORCED

我的活动

 android:windowSoftInputMode="stateVisible|adjustResize"

希望这能帮到别人

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

在 onResume ()中使用上面的代码打开软键盘

我把它用作单例模式,比如:

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();
}
}

在你的活动中使用它,比如:

showSoftKeyboard(this, yourEditTextToFocus);

将此方法发布到您的基本活动中,并使用其他活动,如魅力

public void openKeyboard() {
InputMethodManager imm =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}

科特林

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)
}

爪哇咖啡

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);
}

这种方法是有效的:

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);
}

你可以这样称呼这个方法:

showKeyboard(NameOfActivity.this);

SHOW _ FORCED 不是一个好的选择。如果你使用这个设置,你应该管理隐藏键盘状态。我的建议是这样的;

    public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}

此外,您还可以将注意力集中在视图(通常是 EditText)上,以获取它的参数,这使它成为一个更有用的函数

有关 InputMethodManager.SHOW _ IMPLICIT 和 SHOW _ FORCED; InputMethodManager 输入法管理器的详细信息,请参阅

public final class AAUtilKeyboard {


public static void openKeyboard(final Activity activity, final EditText editText) {
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}


public static void hideKeyboard(final Activity activity) {
final View view = activity.getCurrentFocus();
if (view != null) {
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}

编辑文本框显示和隐藏软键盘的完美代码... ..。

// code to hide soft keyboard
public void hideSoftKeyBoard(EditText editBox) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editBox.getWindowToken(), 0);
}








// code to show soft keyboard
private void showSoftKeyBoard(EditText editBox){
InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
editBox.requestFocus();
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

我喜欢把它作为 Context 的扩展,这样你就可以在任何地方调用它

fun Context.showKeyboard(editText: EditText) {
val inputMethodManager: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(
editText.applicationWindowToken,
InputMethodManager.SHOW_IMPLICIT, 0
)
editText.requestFocus()
editText.setSelection(editText.text.length)
}


fun Context.hideKeyboard(editText: EditText) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editText.windowToken, 0)
}

基于上面这样的答案,只要你有上下文,它就能在 KOTLIN中工作。

fun Context.showKeyboard(editText: EditText) {


editText.requestFocus()
editText.setSelection(editText.text.length)


GlobalScope.launch {
delay(200L)


val inputMethodManager: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInputFromWindow(
editText.applicationWindowToken,
InputMethodManager.SHOW_IMPLICIT, 0
)
}
}

然后您可以在 碎片中调用它,例如,如下所示

ShowKeyboard (binding.myEditText)

import androidx.core.view.WindowInsetsCompat.Type
import androidx.core.view.WindowInsetsControllerCompat


fun Activity.openKeyboard() {
WindowInsetsControllerCompat(window, window.decorView).show(Type.ime())
}


fun Activity.hideKeyboard() {
WindowInsetsControllerCompat(window, window.decorView).hide(Type.ime())
}

如果希望在启动活动/片段时启动键盘,可以使用下面的代码。

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager =
(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
etPhoneNumber.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
}, 500);

科特林中,您可以使用下面的扩展函数来显示和隐藏软键盘。

/**
* Extension method to provide show keyboard for [Activity].
*/
fun Activity.showSoftKeyboard() {
if (currentFocus != null) {
val inputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(this.currentFocus,InputMethodManager.SHOW_IMPLICIT)
}
}




/**
* Extension method to provide hide keyboard for [Activity].
*/
fun Activity.hideSoftKeyboard() {
if (currentFocus != null) {
val inputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
}

仅此而已! 干杯!

打开 Keyboard的最佳方法是调用以下 kotlin 代码:

val inputMethodManager: InputMethodManager =
getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager


inputMethodManager.showSoftInput(editText, 2)

试试这个:

    fun closeKeyboard(view: View) {
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.hideSoftInputFromWindow(view.windowToken, HIDE_IMPLICIT_ONLY)
}


fun showKeyBoard(view: View) {
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(
view,
SHOW_IMPLICIT
)
}

如果不起作用,可以尝试在 run 块中使用 wrapfunc:

  view.postDelayed({showKeyBoard(view)},100)