【问题标题】:How to show virtual keypad in an android activity如何在 android 活动中显示虚拟键盘
【发布时间】:2011-02-12 22:52:57
【问题描述】:

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

打包 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> 

【问题讨论】:

标签: android


【解决方案1】:

首先设置内容视图

@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);
    }

【讨论】:

  • 谢谢老板。你节省了我的时间:)
【解决方案2】:
 setContentView(R.layout.main); 

似乎不见了。清单设置应该足以显示键盘

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2017-07-16
    • 2016-12-31
    • 2011-03-17
    • 2014-07-03
    相关资源
    最近更新 更多