2012-07-29 39 views
0

我想让Android键盘在我的活动启动时弹出。一个简单的谷歌搜索显示,你只需要requestFocus,我在我的.xml,但它仍然不会弹出。我是否犯了任何导致此不起作用的小错误?试图让Android键盘在活动启动时弹出

测试上:

物理4.1 模拟器2.2

layout.xml:

<EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:ems="10" 
     android:hint="To:" 
     android:inputType="textPersonName" > 

     <requestFocus /> 
    </EditText> 
+0

http://stackoverflow.com/questions/4804493/how-to-automatically-pop-up-keyboard – 2012-07-29 17:06:04

+0

我尝试了所有的答案,但没有一次成功。 – EGHDK 2012-07-29 17:28:45

回答

2

这工作:

myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     } 
    } 
}); 
+0

太棒了。谢谢! – EGHDK 2012-07-29 18:00:01

+0

乐意帮忙.. – 2012-07-29 18:05:46

0

试试这个代码:

EditText input = (EditText) findViewById(R.id.editText1); 
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);   
inputManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT); 
+0

我在'getActivity()'上得到一个错误,所以我用'this.getSystemService ...'替换它,但它不起作用。 – EGHDK 2012-07-29 17:24:27