คำอธิบายดูที่ Kotlin
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.java
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 |
package com.phaisarns.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView textViewSample = findViewById(R.id.textViewSample); EditText editTextSample = findViewById(R.id.editTextSample); editTextSample.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { textViewSample.setText("Text in EditText : " + s); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } } |
Link