簡體   English   中英

Android edittext和虛擬鍵盤

[英]Android edittext and virtual keyboard

我有一個活動,其中有編輯文本,但是有一個問題,因為虛擬鍵盤會自動出現。

我想知道是否沒有一種方法不會自動顯示它,而是僅當您單擊“編輯文本”時才顯示

您可以使用

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

在您的Activity 鍵盤只有在您單擊時才會打開

只需為您的活動添加清單: 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM