0
1

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 3 years have passed since last update.

【Kotlin 初学者】EditTextで文字入力した後の状態を取得する

Posted at

EditText内の文字入力後の状態を把握する

使い所はX文字以上の場合は入力できません、などを表示したい場合によく使います
当たり前ですがEditTextは初期入力文字数が0なので、下記のような書き方で文字入力後の文字数を取得してあげます

MainActivity.kt

class MainActivity : AppCompatActivity() {
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        editTextSample.addTextChangedListener(object : TextWatcher {
 
            override fun afterTextChanged(s: Editable) {}
 
            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
            }
 
            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {
             val editText = findViewById<EditText>(R.id.edit_text_input)
                if (editText.count() == 8){
                   .maxLengthText.visibility = View.VISIBLE
                }
            }
        })
    }
}
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?