Java Android编辑文本和虚拟键盘

Java Android编辑文本和虚拟键盘,java,android,Java,Android,我有一个活动,其中有编辑文本,但我有一个问题,因为虚拟键盘会自动出现 我想知道是否有一种方法,它不会自动显示,而只是当您单击您可以使用的编辑文本时 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 在您的活动中。仅当您单击键盘时,键盘才会打开。只需为您的活动添加清单:android:windowSoftInputMode=“stateHidden”。例如: <a

我有一个活动,其中有编辑文本,但我有一个问题,因为虚拟键盘会自动出现

我想知道是否有一种方法,它不会自动显示,而只是当您单击您可以使用的编辑文本时

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

在您的
活动中
。仅当您单击键盘时,键盘才会打开。

只需为您的活动添加清单:
android:windowSoftInputMode=“stateHidden”
。例如:

<activity
    android:name="com.proj.activity.MainActivity"
    android:windowSoftInputMode="stateHidden" />

onResume()方法中写入以下代码,然后键盘将不会自动弹出

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
试试这个

private void showKeyboard(View view) {
    InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.showSoftInput(view, 0);
}

private void hideKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager) this
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
            .getWindowToken(), 0);
}

您需要设置EditText以响应焦点更改事件,并手动隐藏键盘

public class Activity1 extends Activity implements OnFocusChangeListener
{
    protected void onCreate( Bundle b )
    {
         .....
        txtX = (EditText) findViewById(R.id.text_x);
        txtX.setOnFocusChangeListener(this);
    }

    public void hideKeyboard(View view)
    {
        InputMethodManager inputMethodManager =(InputMethodManager)context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    @Override
    public void onFocusChange(View view, boolean arg1)
    {
        if(! view.hasFocus())
            hideKeyboard(view);
    }
}
在xml中,将布局设置为可聚焦

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusableInTouchMode="true" >

        <EditText
            android:id="@+id/text_x"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
</LinearLayout>

您是否意识到
的可能重复