1. 效果图
2. 实现的代码
firstActivity.java
package iflab.test; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.TextView; import android.widget.Toast; public class firstActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView myTextView = null; // 声明变量 myTextView = (TextView) findViewById(R.id.myTextView); // 获取对象 myTextView.addTextChangedListener(new TextWatcher() { // 添加监听器 @Override public void afterTextChanged(Editable s) { // 文本改变后 // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub //文本改变前 } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub //文本改变时 Toast.makeText( getApplicationContext(), "原字符串共" + String.valueOf(before) + "个字符。" + "从第" + String.valueOf(start) + "个字符开始更改为字符串:" + s + "。共" + String.valueOf(count) + "字符。", Toast.LENGTH_LONG).show(); // 显示提示信息 } }); myTextView.setText("1234567890"); // 重设文本内容 } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
时间: 2024-09-30 22:55:28