活动启动时软键盘不显示 - 更新?
我希望在我的活动开始时打开键盘。我已经尝试了以下问题的所有方法/答案,但没有成功。
我认为问题是当硬件键盘可用时,默认行为是不显示软键盘。这可以被覆盖吗?如果隐藏硬件键盘会发生什么?
我读过以下问题但没有运气。最接近我遇到的问题是在这里: 问题 2712822
其他包括:
问题 3379403
问题 2479504
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/testText" android:focusable="true" android:focusableInTouchMode="true" android:hint="Input here"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="and here"></EditText>
</LinearLayout>
</LinearLayout>
</ScrollView>
我的主要活动代码如下所示:
package com.example.example3;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class example3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText)this.findViewById(R.id.testText);
edit.requestFocus();
/* // Below Doesn't work
InputMethodManager imm = (InputMethodManager)
example3.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){/ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
//Below Doesn't work
// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
*/
}
}
这是丢失的吗原因?有人可以在带有关闭的硬件键盘的手机上进行测试并告诉我会发生什么吗?
I would like the keyboard to open when my Activity starts. I've tried all the methods/answers with the questions below with no luck.
I believe the issue is when the hardware keyboard is available, the default behavior is for the soft keyboard to not be displayed. Can this be overridden? What happens if the hardware keyboard is hidden?
I've read the following questions with no luck. The closest to the problem I'm experiencing is here:
Question 2712822
Others Include:
Question 3379403
Question 2479504
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/testText" android:focusable="true" android:focusableInTouchMode="true" android:hint="Input here"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="match_parent" android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_width="match_parent">
<TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:hint="and here"></EditText>
</LinearLayout>
</LinearLayout>
</ScrollView>
My Main Activity code looks like this:
package com.example.example3;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class example3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText)this.findViewById(R.id.testText);
edit.requestFocus();
/* // Below Doesn't work
InputMethodManager imm = (InputMethodManager)
example3.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){/ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
//Below Doesn't work
// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
*/
}
}
Is this a lost cause? Can someone test this on a phone with hardware keyboard that is closed and tell me what happens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
softinputMode 相关的答案应该有效。如果您无法在清单中设置它,您可以尝试在
onCreate()
中设置它,以指示导航到 Activity 时键盘的显示方式。要在活动获得焦点时显示它,请使用:
The softinputMode related answers should be working. If you're having no luck with setting it in the manifest you might try setting it in
onCreate()
to dictate how the keyboard is shown when navigating to the activity.To show it anytime the activity gets focus use:
这绝对有效,我一直使用它,不要从 onCreate 调用它,从 onStart 或 onResume 调用它。
This definitley works, i use it all the time, dont call it from onCreate, call it from onStart or onResume.
试试这个
这对我有用。
Try this
This works for me.