zoukankan      html  css  js  c++  java
  • Android监听EditText内容变化

    1、给EditText追加ChangedListener

    EditText editText = (EditText) findViewById(R.id.edittext);
    
    editText.addTextChangedListener(watcher); 

    2、描述监听

    private TextWatcher watcher = new TextWatcher() {
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    // 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 afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
    
    }
    };

    监听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; 
    
    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(); 
    } 
    } 

    main.xml

    <?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>

    效果图:

    当我们在输入框输入不是整型数字的时候,会立刻弹出输入框,提示你改正 

  • 相关阅读:
    即将到来的“分布式云”(DPaaS):分布式计算+ DB +存储即服务
    【玩转MLS系列】基础教程
    【云速建站】微信公众平台中维护IP白名单
    基于华为云对话机器人技能平台的规则模板概述
    云享专家倪升武:微服务架构盛行的时代,你需要了解点 Spring Boot
    Python一秒搭建ftp服务器,帮助你在局域网共享文件
    华为担纲建设基础软硬件国家新一代AI开放创新平台
    基于华为云区块链服务快速部署和搭建链上应用
    protobuf代码生成
    python的str()和repr()的区别
  • 原文地址:https://www.cnblogs.com/xuewater/p/2591471.html
Copyright © 2011-2022 走看看