活动开始时软键盘不显示 - 更新?
我希望在我的Activity启动时打开键盘。我已经尝试过所有的方法/答案,下面的问题没有运气。活动开始时软键盘不显示 - 更新?
我相信问题是当硬件键盘可用时,默认行为是软键盘不能显示。这可以被覆盖吗?如果硬件键盘被隐藏会发生什么?
我读过以下问题,没有运气。最接近我遇到的问题是在这里: Question 2712822
其他还包括:
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>
我的主要活动代码如下所示:
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);
*/
}
}
这是一个失败的原因吗?有人可以用硬件键盘关闭的手机进行测试,并告诉我会发生什么?
softinputMode相关的答案应该工作。如果您在清单中设置它没有运气,您可以尝试在onCreate()
中设置它以指示在导航到活动时如何显示键盘。
要随时显示它的活动获得焦点的使用:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
这definitley工作,我一直使用它,不要从onCreate调用它,从onStart或onResume调用它。
public static void showKeyboard(Activity act,EditText t){
InputMethodManager imm = (InputMethodManager)act.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(t, 0);
}
除了我上面的评论,在测试这种方法时,您是否专门设置了启用触摸的模拟器,并禁用了硬件键盘以使其工作?我已经在我的机器人公司上试过了,并且成功了,但没有使用模拟器。 – hydrox467 2011-05-30 03:42:53
试试这个
edtReceiver.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
{
// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null)
{
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
}
}
});
这对我的作品。
我已经在我的代码中尝试了这一点......目前它已被注释掉。这并不成功。你知道仿真器/真实设备当前是否打开硬件键盘,是否会覆盖任何通过编程打开键盘的尝试? – hydrox467 2011-05-30 03:41:22
当硬件键盘暴露时,软件键盘不应该打开 – jqpubliq 2011-06-01 17:59:08