Android edittext和虚拟键盘

时间:2014-02-13 14:19:30

标签: java android

我有一个有编辑文字的活动,但我遇到了问题,因为虚拟键盘会自动出现。

我想知道是否有一种方法不会自动出现,但只有当你点击编辑文本时

5 个答案:

答案 0 :(得分:1)

您可以使用

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

Activity中。键盘只有在你点击它时才会打开

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分: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);
}

答案 4 :(得分: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>
相关问题