Example of using TextWatcher |
package com.example.androidtextwatcher; import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { EditText input; TextView info, cont; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); input = (EditText)findViewById(R.id.input); info = (TextView)findViewById(R.id.info); cont = (TextView)findViewById(R.id.cont); input.addTextChangedListener(myTextWatcher); } TextWatcher myTextWatcher = new TextWatcher(){ @Override public void afterTextChanged(Editable s) { /* * This method is called to notify you that, * somewhere within s, the text has been changed. */ //info.setText(""); //cont.setText(s.toString()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* * This method is called to notify you that, * within s, the count characters beginning at * start are about to be replaced by new text * with length after. */ info.setText("beforeTextChanged - " + start + " " + count + " " + after); cont.setText(s.toString()); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { /* * This method is called to notify you that, * within s, the count characters beginning * at start have just replaced old text that * had length before. */ info.setText("onTextChanged - " + start + " " + before + " " + count); cont.setText(s.toString()); }}; }
<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:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android-coding.blogspot.com" android:textColor="#A00000" android:textStyle="bold|italic" /> <EditText android:id="@+id/input" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/cont" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
No comments:
Post a Comment