AndroidでEditTextを入力した後にキーボードを非表示にする方法は?

StackOverflow https://stackoverflow.com/questions/2342620

  •  23-09-2019
  •  | 
  •  

質問

私は持っています EditText ボタンは親の下に揃えられます。

文字を入力してボタンを押してデータを保存しても、仮想キーボードが消えません。

キーボードを非表示にする方法を教えてくれる人はいますか?

役に立ちましたか?

解決

これは動作するはずです。

InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 

だけでは何もフォーカスを持っていない場合this.getCurrentFocusは()それは希望、ヌルを返さないことを確認してください。

他のヒント

またのEditText内imeOptionsを定義したい場合があります。この方法で、あなたは完了に押すとキーボードが消えます。

<EditText
    android:id="@+id/editText1"
    android:inputType="text"
    android:imeOptions="actionDone"/>
   mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // do something, e.g. set your TextView here via .setText()
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });

とXMLでの

  android:imeOptions="actionDone"
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

私は誰もがこの方法を使用して表示されませんでした。

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused) {
        InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (focused)
            keyboard.showSoftInput(editText, 0);
        else
            keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
});

そして、ただのEditTextにフォーカスを要求します:

editText.requestFocus();

ソリューションのEditTextアクションlistennerに含まれます:

public void onCreate(Bundle savedInstanceState) {
    ...
    ...
    edittext = (EditText) findViewById(R.id.EditText01);
    edittext.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    ...
    ...
}

オプションを入力し、以下の2行のコードが動作するダウンだけ書き込みます。

InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
私のEditTextが自動的に入るに却下なっていなかったため、

私はこれを見つけました。

これは私の元のコードでします。

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {

            // Do stuff when user presses enter

            return true;

        }

        return false;
    }
});

私は行を削除することによってそれを解決した。

return true;
ユーザーがEnterキーを押すとものをやった後、

希望これが誰かを助けます。

過去日間これで苦労し、本当によく働く解決策を見つけられて。タッチがどこか外のEditTextが行われたときにソフトキーボードが隠されています。

コードは、ここに掲載:アンドロイドでクリックで非表示デフォルトのキーボードを

この方法を使用して、編集テキストからキーボードを削除します。

 public static void hideKeyboard(Activity activity, IBinder binder) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (binder != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
            inputManager.showSoftInputFromInputMethod(binder, 0);
        }
    }
}

そして、このメソッドはアクティビティからキーボードを削除します (場合によっては機能しません。たとえば、キーボードがバインドされている edittext がフォーカスを失った場合、機能しません。ただし、他の状況ではうまく機能し、キーボードを保持する要素を気にする必要はありません)

 public static void hideKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

あなたは上にマークされた解答を見ることができます。しかし、私はgetDialog().getCurrentFocus()とうまく機能を使用していました。私は私のoncreatedialogで"this"を入力カント原因私はこの答えを投稿します。

これが私の答えです。あなたは答えをマークし、働いていないしようとした場合、あなたは、単にこれを試すことができます:

InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
int klavStat = 1; // for keyboard soft/hide button
int inType;  // to remeber your default keybort Type

エディタは -

のEditTextフィールドであり、
/// metod for onclick button ///
 public void keyboard(View view) {
        if (klavStat == 1) {
            klavStat = 0;

            inType = editor.getInputType();

            InputMethodManager imm = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);


            editor.setInputType(InputType.TYPE_NULL);

            editor.setTextIsSelectable(true);



        } else {
            klavStat = 1;


            InputMethodManager imm = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);

                imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

            editor.setInputType(inType);

        }
    }

uはanatherのEditTextフィールドを持っている場合は、uは、フォーカスの変更を監視する必要があります。

あなたは簡単にこのような呼び出しのためのシングルトンクラスを作成することができます:

public class KeyboardUtils {

    private static KeyboardUtils instance;
    private InputMethodManager inputMethodManager;

    private KeyboardUtils() {
    }

    public static KeyboardUtils getInstance() {
        if (instance == null)
            instance = new KeyboardUtils();
        return instance;
    }

    private InputMethodManager getInputMethodManager() {
        if (inputMethodManager == null)
            inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
        return inputMethodManager;
    }

    @SuppressWarnings("ConstantConditions")
    public void hide(final Activity activity) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                try {
                    getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

ので、後の活動にどのように次のフォームを呼び出すことができます:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        KeyboardUtils.getInstance().hide(this);
    }

}
editText.setInputType(InputType.TYPE_NULL);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top