- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val editText = findViewById<EditText>(R.id.editText)
- editText.filters = editText.filters + MyInputFilter()
- }
- }
- class MyInputFilter : InputFilter {
- override fun filter(
- source: CharSequence,
- start: Int,
- end: Int,
- dest: Spanned,
- dstart: Int,
- dend: Int
- ): CharSequence? {
- var i: Int = start
- while (i < end) {
- if (source[i] == '\n') {
- break
- }
- i++
- }
- if (i == end) {
- return null
- }
- val filtered = SpannableStringBuilder(source, start, end)
- val start2 = i - start
- val end2 = end - start
- for (j in end2 - 1 downTo start2) {
- if (source[j] == '\n') {
- filtered.delete(j, j + 1)
- }
- }
- return filtered
- }
- }
2021年3月29日月曜日
改行を入力させない EditText 用 InputFilter
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿