9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EditTextの入力を制限する方法

Posted at

InputFilterインターフェイスを実装し、Editableインターフェイスを採用したUI(EditText)に設定することでキー入力を制限できるようになる。

注意

制限されたキーを押下しても入力はされないが、キーボード上部の補完は入力されたような振る舞いをする。
ただしEditTextの内容をTextViewなどに出力しても入力はされていない。

MainActivity.java
		englishNameEditText = (EditText) findViewById(R.id.englishNameEditText);
		InputFilter inputFilter = new InputFilter() {
			@Override
			public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
				if (source.toString().matches("^[a-zA-Z]$")) {
					return source;
				}
				return "";
			}
		};
		InputFilter[] filters = { inputFilter };
		englishNameEditText.setFilters(filters);
9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?