Norway


This example implement TextWatcher for , such that we can detect on EditText, no extra R;Enter” button is need.


package com.blogspot._er.androidedittextchanged;

import .os.Bundle;
import .support.v7..AppCompatActivity;
import .text.Editable;
import .text.TextWatcher;
import .widget.EditText;
import .widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    TextView tvMsg, tvInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.edittext);
        tvMsg = (TextView)findViewById(R.id.msg);
        tvInfo = (TextView)findViewById(R.id.info);

        editText.addTextChangedListener(myTextWatcher);
    }

    TextWatcher myTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, 
                                      int i, int i1, int i2) {
            tvInfo.setText("beforeTextChanged(): n"
                    + charSequence + "n"
                    + i + "n"
                    + i1 + "n"
                    + i2);
        }

        @Override
        public void onTextChanged(CharSequence charSequence, 
                                  int i, int i1, int i2) {
            tvInfo.setText("onTextChanged(): n"
                    + charSequence + "n"
                    + i + "n"
                    + i1 + "n"
                    + i2);
            tvMsg.setText(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {
            /*
            String s = editable.toString();
            tvInfo.setText("afterTextChanged(): n"
                    + s);
                    */
        }
    };
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidedittextchanged.MainActivity">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink=""
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold"/>

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter text"
        android:textSize="24dp"/>
    <TextView
        android:id="@+id/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="24dp"/>
    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textSize="20dp"/>

</LinearLayout>


Next:
– Another example of TextWatcher to monitor text changed in EditText



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here