เมื่อพิมพ์ข้อความใน EditText
ข้อความจะอัพเดทบน TextView
ทันที
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" android:padding="10sp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="50sp" android:gravity="center" android:text="TutorialKart\nEditText On Text Change" android:textSize="25sp" /> <TextView android:id="@+id/textViewSample" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="50sp" android:textSize="20sp" /> <EditText android:id="@+id/editTextSample" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter Text ..." android:textSize="20sp" /> </LinearLayout> </android.support.constraint.ConstraintLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.phaisarn.ktapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.text.Editable import android.text.TextWatcher import kotlinx.android.synthetic.main.activity_main.* 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) { textViewSample.text = "Text in EditText : " + s } }) } } |