• TextWatcher的简单使用


    xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.helloword.MainActivity">
    <EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"/>
    <TextView
    android:text="TextView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textColor="#000000"
    android:id="@+id/textView"
    android:gravity="center"/>
    </LinearLayout>

    MainActivity源码

    package com.example.administrator.helloword;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

    private EditText ed;
    private TextView showResult_tv;
    private String getResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ed = (EditText)findViewById(R.id.editText);
    showResult_tv = (TextView)findViewById(R.id.textView);
    showResult_tv.setHint("没有内容");
    //设置文本改变监听
    ed.addTextChangedListener(tw);
    }
    //文本改变监听器
    TextWatcher tw = new TextWatcher() {
    //文字改变前的监听
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    //文本改变时的监听
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    if (ed.getText().toString() == null || ed.getText().toString() == "") {
    showResult_tv.setText("");
    } else {
    showResult_tv.setText(ed.getText().toString());
    }
    }
    //文字改变后的监听
    @Override
    public void afterTextChanged(Editable editable) {
    }
    };
    }
  • 相关阅读:
    赤手空拳破解WINDOWS系统密码
    几扇鲜为人知的Windows XP自动运行后门
    Enumerating Logon Sessions
    用浏览器控件做界面,网页界面中定义自己的程序事件
    P2P之UDP穿透NAT的原理与实现(附源代码)
    delphi调用c#写的webservice中文出现乱码的问题
    基于Delphi的DirectShow开发概述
    视频捕获软件开发完全教学
    GINA 模块定义文件(Windows XP)
    GINA后门代码
  • 原文地址:https://www.cnblogs.com/lyh1299259684/p/6528778.html
Copyright © 2020-2023  润新知