Sabtu, 25 Januari 2014

Implement TextWatcher to EditText

This example implement TextWatcher, attach to EditText with addTextChangedListener(). Its methods will be called when the text is changed.

Implement TextWatcher to EditText
Implement TextWatcher to EditText

package com.example.androidedittext;

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 {

TextView promptAfter, promptBefore, promptOn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

promptAfter = (TextView)findViewById(R.id.promptAfter);
promptBefore = (TextView)findViewById(R.id.promptBefore);
promptOn = (TextView)findViewById(R.id.promptOn);

EditText input = (EditText)findViewById(R.id.input);
input.addTextChangedListener(textWatcher);
}

TextWatcher textWatcher = new TextWatcher(){

@Override
public void afterTextChanged(Editable s) {
promptAfter.setText(s.toString());
}

@Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {
promptBefore.setText(s + "\n" +
start + " / " + count + " / " + after);
}

@Override
public void onTextChanged(CharSequence s,
int start, int before, int count) {
promptOn.setText(s + "\n"
+ start + " / " + before + " / " + count);
}

};

}

<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" />

<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:text="afterTextChanged(Editable s)" />
<TextView
android:id="@+id/promptAfter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:text="beforeTextChanged(CharSequence s, int start, int count, int after)" />
<TextView
android:id="@+id/promptBefore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:text="onTextChanged(CharSequence s, int start, int before, int count)" />
<TextView
android:id="@+id/promptOn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />

</LinearLayout>

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved