Android开发教程:监听EditText的变化
2013-02-20 10:11
465 查看
之前的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下EditText。
监听EditText的变化
使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,TextWatcher是一个接口类,所以必须实现TextWatcher里的抽象方法:
当EditText里面的内容有变化的时候,触发TextChangedListener事件,就会调用TextWatcher里面的抽象方法。
MainActivity.java
package com.lingdududu.watcher;
import Android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText text;
String str;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText)findViewById(R.id.text);
text.addTextChangedListener(textWatcher);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Log.d("TAG","afterTextChanged--------------->");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
Log.d("TAG","beforeTextChanged--------------->");
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("TAG","onTextChanged--------------->");
str = text.getText().toString();
try {
//if ((heighText.getText().toString())!=null)
Integer.parseInt(str);
} catch (Exception e) {
// TODO: handle exception
showDialog();
}
}
};
private void showDialog(){
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error);
builder.setMessage("你输出的整型数字有误,请改正");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog = builder.create();
dialog.show();
} }
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入整型数字" /> <EditText android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> 效果图:
当我们在输入框输入不是整型数字的时候,会立刻弹出输入框,提示你改正
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2012-01/52648.htm
监听EditText的变化
使用EditText的addTextChangedListener(TextWatcher watcher)方法对EditText实现监听,TextWatcher是一个接口类,所以必须实现TextWatcher里的抽象方法:
当EditText里面的内容有变化的时候,触发TextChangedListener事件,就会调用TextWatcher里面的抽象方法。
MainActivity.java
package com.lingdududu.watcher;
import Android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText text;
String str;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText)findViewById(R.id.text);
text.addTextChangedListener(textWatcher);
}
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Log.d("TAG","afterTextChanged--------------->");
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
Log.d("TAG","beforeTextChanged--------------->");
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("TAG","onTextChanged--------------->");
str = text.getText().toString();
try {
//if ((heighText.getText().toString())!=null)
Integer.parseInt(str);
} catch (Exception e) {
// TODO: handle exception
showDialog();
}
}
};
private void showDialog(){
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("消息").setIcon(android.R.drawable.stat_notify_error);
builder.setMessage("你输出的整型数字有误,请改正");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog = builder.create();
dialog.show();
} }
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入整型数字" /> <EditText android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout> 效果图:
当我们在输入框输入不是整型数字的时候,会立刻弹出输入框,提示你改正
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2012-01/52648.htm
相关文章推荐
- Android开发教程:监听EditText的变化
- Android应用开发EditText文本内容变化监听方法
- Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》
- Android应用开发EditText文本内容变化监听方法
- Android简易实战教程--第十九话《手把手教您监听EditText文本变化,实现抖动和震动的效果》
- Android开发:监听EditText字数的变化
- Android应用开发EditText文本内容变化监听方法
- Android应用开发EditText文本内容变化监听方法
- Android应用开发EditText文本内容变化监听方法
- 【Android进阶学习】监听EditText的变化
- Android---监听EditText变化
- 【Android进阶学习】监听EditText的变化
- Android开发之EditText 详解(addTextChangedListener监听用户输入状态)
- Android监听EditText内容变化
- Android: EditText监听内容变化
- 【Android进阶学习】监听EditText的变化
- 【Android进阶学习】监听EditText的变化
- Android中动态监听EditText控件内容变化
- Android开发之EditText 详解(addTextChangedListener监听用户输入状态)