【Android進階學習】監聽EditText的變化
阿新 • • 發佈:2019-02-07
- 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;
- publicclass MainActivity extends Activity {
- private EditText text;
- String str;
- @Override
- publicvoid 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
- publicvoid afterTextChanged(Editable s) {
- // TODO Auto-generated method stub
- Log.d("TAG","afterTextChanged--------------->");
- }
- @Override
- publicvoid beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- // TODO Auto-generated method stub
- Log.d("TAG","beforeTextChanged--------------->");
- }
- @Override
- publicvoid 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();
- }
- }
- };
- privatevoid 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
- publicvoid onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- dialog = builder.create();
- dialog.show();
- }
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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>
- package com.lingdududu.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- publicclass MainActivity extends Activity {
- private Button clearBtn;
- private EditText et;
- private TextView tv;
- finalint MAX_LENGTH = 20;
- int Rest_Length = MAX_LENGTH;
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv =(TextView)findViewById(R.id.tv);
- et = (EditText)findViewById(R.id.et);
- clearBtn = (Button)findViewById(R.id.btn);
- et.addTextChangedListener(new TextWatcher() {
- @Override
- publicvoid beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- tv.setText("還能輸入"+Rest_Length+"個字");
- }
- @Override
- publicvoid afterTextChanged(Editable s) {
- tv.setText("還能輸入"+Rest_Length+"個字");
- }
- @Override
- publicvoid onTextChanged(CharSequence s, int start, int before, int count) {
- if(Rest_Length>0){
- Rest_Length = MAX_LENGTH - et.getText().length();
- }
- }
- });
- clearBtn.setOnClickListener(new Button.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- et.setText("");
- Rest_Length = MAX_LENGTH;
- }
- });
- }
- }