带有showSoftInput的Android show软键盘不工作?
我创建了一个简单的应用程序来测试以下功能。启动“我的活动”时,需要打开软键盘 我的代码不起作用 我尝试了清单中的各种“状态”设置,以及InputMethodManager(imm)代码中的不同标志 我已经在AndroidManifest.xml中包含了该设置,并在onCreate的only活动中显式调用了该设置带有showSoftInput的Android show软键盘不工作?,android,show,android-softkeyboard,android-input-method,Android,Show,Android Softkeyboard,Android Input Method,我创建了一个简单的应用程序来测试以下功能。启动“我的活动”时,需要打开软键盘 我的代码不起作用 我尝试了清单中的各种“状态”设置,以及InputMethodManager(imm)代码中的不同标志 我已经在AndroidManifest.xml中包含了该设置,并在onCreate的only活动中显式调用了该设置 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.andro
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.android.studyIme"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".StudyImeActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysVisible">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是如此微妙,以至于它是犯罪。这适用于没有硬滑出式键盘的手机。带有硬键盘的手机不会在本次通话中自动打开。我的LG和旧NexusOne没有键盘——因此,活动启动时软键盘会打开(这就是我想要的),但是,装有滑出式键盘的MyTouch和HTC G2手机在我关闭硬键盘的情况下触摸编辑字段之前,不会打开软键盘。嘿,我希望你仍然在寻找答案,因为我在测试代码时找到了答案。代码如下:
InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);
以下是我回答的问题:
我在一部带有硬键盘的手机上就做到了这一点:
editText1.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
当活动启动时,键盘似乎在最初显示,但被其他东西隐藏,因为以下操作有效(但实际上是一项肮脏的工作): 第一种方法
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
@Override
public void run()
{
editText.requestFocus();
imm.showSoftInput(editText, 0);
}
}, 100);
第二种方法
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.postDelayed(new Runnable()
{
@Override
public void run()
{
editText.requestFocus();
imm.showSoftInput(editText, 0);
}
}, 100);
在onCreate中,在“创建”活动中启动它
new Handler().postDelayed(new Runnable() {
@Override
public void run()
{
// InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// inputMethodManager.toggleSoftInputFromWindow(EnterYourViewHere.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
if (inputMethodManager != null)
{
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
}
}, 200);
第三种方法
将给定代码添加到清单中的活动标记。它将在启动时显示键盘,并将第一个焦点设置为您想要的视图
android:windowSoftInputMode="stateVisible"
我的代码中有切换,但没有延迟。我尝试了showSoftInput的postDelayed,但没有成功,此后我尝试了您建议的解决方案。我正要放弃它,因为它是另一个失败的潜在解决方案,直到我决定增加延迟时间。它对我来说一直有效到200毫秒,但在这一点上它不起作用,至少在物理电话上不起作用。所以,在可怜的安卓开发者放弃这个答案之前,试着增加延迟以获得成功的解决方案。为旧的、速度较慢的手机增加一点可能是值得的。感谢heaps,我花了几个小时来研究这个问题。这个答案可能晚了,但对我来说非常有效。也许这对某人有帮助:) 根据需要,您可以使用其他标志
InputMethodManager.SHOW_FORCED
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
显示软键盘是一个大问题。为了得出最后的结论,我搜索了很多。由于这个答案提供了许多线索: 问题: 通常,我们在初始化视图后立即调用showSoftInput。在活动中,这主要是在onCreate中,在片段onCreateView中。为了显示键盘,IMM需要将FocusedView设置为活动状态。这可以使用IMM的isActive(查看)方法进行检查。如果在创建视图时调用showSoftInput,则该视图很有可能不会在IMM中处于活动状态。这就是为什么有时50-100毫秒延迟showSoftInput很有用的原因。但是,这仍然不能保证100毫秒后视图将变为活动状态。所以在我看来,这又是一次黑客攻击 解决方案: 我使用下面的类。这将保持每100毫秒运行一次,直到成功显示键盘。它在每次迭代中执行各种检查。有些检查可以停止运行,有些在100毫秒后发布
public class KeyboardRunnable extends Runnable
{
// ----------------------- Constants ----------------------- //
private static final String TAG = "KEYBOARD_RUNNABLE";
// Runnable Interval
private static final int INTERVAL_MS = 100;
// ----------------------- Classes ---------------------------//
// ----------------------- Interfaces ----------------------- //
// ----------------------- Globals ----------------------- //
private Activity parentActivity = null;
private View targetView = null;
// ----------------------- Constructor ----------------------- //
public KeyboardRunnable(Activity parentActivity, View targetView)
{
this.parentActivity = parentActivity;
this.targetView = targetView;
}
// ----------------------- Overrides ----------------------- //
@Override
public void run()
{
// Validate Params
if ((parentActivity == null) || (targetView == null))
{
Dbg.error(TAG, "Invalid Params");
return;
}
// Get Input Method Manager
InputMethodManager imm = (InputMethodManager) parentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
// Check view is focusable
if (!(targetView.isFocusable() && targetView.isFocusableInTouchMode()))
{
Dbg.error(TAG, "Non focusable view");
return;
}
// Try focusing
else if (!targetView.requestFocus())
{
Dbg.error(TAG, "Cannot focus on view");
Post();
}
// Check if Imm is active with this view
else if (!imm.isActive(targetView))
{
Dbg.error(TAG, "IMM is not active");
Post();
}
// Show Keyboard
else if (!imm.showSoftInput(targetView, InputMethodManager.SHOW_IMPLICIT))
{
Dbg.error(TAG, "Unable to show keyboard");
Post();
}
}
// ----------------------- Public APIs ----------------------- //
public static void Hide(Activity parentActivity)
{
if (parentActivity != null)
{
InputMethodManager imm = (InputMethodManager) parentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(parentActivity.findViewById(android.R.id.content).getWindowToken(), 0);
}
else
{
Dbg.error(TAG, "Invalid params to hide keyboard");
}
}
// ----------------------- Private APIs ----------------------- //
protected void Post()
{
// Post this aftr 100 ms
handler.postDelayed(this, INTERVAL_MS);
}
}
要使用它,只需创建该类的一个实例。向其传递父活动和targetView,后者将具有键盘输入和焦点。然后使用处理程序发布实例。这对我很有用:
public void requestFocusAndShowSoftInput(View view){
view.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
Xamarin开发人员的解决方案(_digit1==EditText):
以下几点对我很有用:
mEditTextStudy.requestFocus();
mEditTextStudy.post(
new Runnable() {
@Override
public void run() {
InputMethodManager imm =
(InputMethodManager)
getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(mEditTextStudy, SHOW_FORCED);
}
}
});
如果您试图在片段中显示软键盘,则需要等待活动创建完成后再调用
showsoftwinput()
。示例代码:
public class SampleFragment extends Fragment {
private InputMethodManager mImm;
private TextView mTextView;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
showSoftKeyboard(mTextView);
}
/**
* Request the InputMethodManager show the soft keyboard. Call this in {@link #onActivityCreated(Bundle)}.
* @param view the View which would like to receive text input from the soft keyboard
*/
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
mImm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
}
这是对西德哈特·加格答案的修改版本。它100%的时间都能工作
import android.content.Context;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
public class SoftInputService implements Runnable {
private static final String TAG = SoftInputService.class.getSimpleName();
private static final int INTERVAL_MS = 100;
private Context context;
private View targetView;
private Handler handler;
public SoftInputService(Context context, View targetView) {
this.context = context;
this.targetView = targetView;
handler = new Handler(Looper.getMainLooper());
}
@Override
public void run() {
if (context == null || targetView == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (!targetView.isFocusable() || !targetView.isFocusableInTouchMode()) {
Log.d(TAG,"focusable = " + targetView.isFocusable() + ", focusableInTouchMode = " + targetView.isFocusableInTouchMode());
return;
} else if (!targetView.requestFocus()) {
Log.d(TAG,"Cannot focus on view");
post();
} else if (!imm.showSoftInput(targetView, InputMethodManager.SHOW_IMPLICIT)) {
Log.d(TAG,"Unable to show keyboard");
post();
}
}
public void show() {
handler.post(this);
}
public static void hide(Context context, IBinder windowToekn) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(windowToekn, 0);
}
protected void post() {
handler.postDelayed(this, INTERVAL_MS);
}
}
用法:
// To show the soft input
new SoftInputService(context, theEditText).show();
// To hide the soft input
SoftInputService.hide(context, theEditText.getWindowToken());
类似的问题,但不同的解决方案,所以张贴的情况下,它是有用的其他人 问题不在于我的代码和使用:
inputMethodManager.showSoftInput(kbdInput, InputMethodManager.SHOW_IMPLICIT);
这个问题与最近的sdk编译有关。我再也无法将上述内容应用于隐藏的字段。似乎您必须使字段可见且大于0,才能显示键盘。我这么做是因为我的应用程序更像是一个游戏,使用键盘作为图像输入。所以我所要做的就是改变:
<EditText
android:id="@+id/kb_input"
android:layout_width="0dp"
android:layout_height="0dp"
/>
到
我的背景是黑色的,所以当
EditText
现在可见时,它在黑色背景上看起来是不可见的。Hmm。。。我只是在我的Sprint LG Optimus手机上用默认的“HelloWorld”活动(也就是说,不包括SHOW_强制代码)尝试了一下,结果一切正常。该功能是否可能依赖于设备(已安装操作系统)??回家后,我会在我的其他设备(HTC、G2和MyTouch)上再次测试。请看我的回答,我已经尝试了下面提到的所有技术,但都成功了:多么糟糕的垃圾API。无论我使用它多少次,它都不会有意义,也不会像预期的那样工作。应该有一个showKeyboard()和hideKeyboard()方法,按照您期望的方式工作。不好意思,谷歌。注意:我已经用EditText和InputMethodManager做了很多实验,试图在设备有硬键盘时强制打开软键盘,但没有成功。对于在Visual Studio中进行Xamarin开发的任何人来说,在AVD管理器中,你可以编辑你的AVD,并且有一个标记为“硬件键盘存在”。取消选中该选项将允许显示软输入。@仲裁员——你可以放松。这就像100年前的谷歌时代一样。(你知道吗,有人甚至制造了滑出式硬键盘——哈哈)。那时,我是唯一一个对问题和解决方案感兴趣的人,所以我试着
// To show the soft input
new SoftInputService(context, theEditText).show();
// To hide the soft input
SoftInputService.hide(context, theEditText.getWindowToken());
inputMethodManager.showSoftInput(kbdInput, InputMethodManager.SHOW_IMPLICIT);
<EditText
android:id="@+id/kb_input"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<EditText
android:id="@+id/kb_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textColorHighlight="@color/black"
android:backgroundTint="@color/black"
android:cursorVisible="false"
/>