2010-04-27 48 views
2

为什么我无法在我的活动中显示虚拟键盘。这里是我的代码:如何在Android活动中显示虚拟键盘

package som.android.keypad;

import android.app.Activity; 
import android.os.Bundle; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.EditText; 

public class ShowKeypad extends Activity { 
    InputMethodManager imm; 

    @Override 
    public void onCreate(Bundle icicle) { 

    super.onCreate(icicle); 
    EditText editText = (EditText)findViewById(R.id.EditText); 

    ((InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE)).showSoftInput(editText, 0); 



    } 

} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="som.android.keypad" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".ShowKeypad" 
        android:windowSoftInputMode="stateAlwaysVisible" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
    <uses-sdk android:minSdkVersion="4" /> 

</manifest> 
+0

结帐我的回答http://stackoverflow.com/questions/7289335/soft-keyboard-shows-up-on-edittext-focus-only-once/7291121#7291121 – 2011-10-07 02:41:29

+0

你的问题解决了没有? – Harshid 2013-06-26 05:13:44

+0

@穆罕默德请接受答案。 – Harshid 2013-08-08 10:00:15

回答

1
setContentView(R.layout.main); 

似乎缺少。舱单,应该足以说明键盘

6

第一的setContentView

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

有时在android 4.2 keyboard不会自动打开

你必须用这种方式来打开和关闭键盘以编程方式。

//For open keyboard 
public void OpenKeyBoard(Context mContext){ 
     InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 
    } 
    //For close keyboard 
    public void CloseKeyBoard(Context mContext){ 
     InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0); 
    } 
+0

谢谢!该解决方案解决了我的问题:) – thiagolsilva 2015-04-13 04:23:37

+0

谢谢老板。你节省了我的时间:) – 2016-06-19 10:15:41