1

Android開発は初めてです。私の要件は、EditText ウィジェットの外側をクリックしたときに Android 仮想キーパッドを非表示にすることです。助けてください。

4

2 に答える 2

5

仮想キーボードを非表示にするには、次のコードを実行できます。

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

親レイアウトに関連付けられているonTouchDown()メソッド内にそのコードを配置するだけです。OnTouchListener

于 2011-10-03T17:42:52.210 に答える
3

これを確認してください。

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View view = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);

if (view instanceof EditText) {
    View w = getCurrentFocus();
    int scrcoords[] = new int[2];
    w.getLocationOnScreen(scrcoords);
    float x = event.getRawX() + w.getLeft() - scrcoords[0];
    float y = event.getRawY() + w.getTop() - scrcoords[1];

    if (event.getAction() == MotionEvent.ACTION_UP 
&& (x < w.getLeft() || x >= w.getRight() 
|| y < w.getTop() || y > w.getBottom()) ) { 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
    }
}
return ret;
}

アクティビティに編集テキストがある場合は、このメソッドをオーバーライドします。

于 2012-10-15T06:51:11.327 に答える