我做了一个查询单词的简单app, 当在EditText中输入单词的时候,点击lookup,则在TextView区域显示出该单词的意思,当EditText中没有任何字符时,显示"word definition result", 注意,单词的最大长度限制是20个字符,且禁止回车键(换行键);

这个里面需要用到读文件操作、禁止键盘回车键、Textwatcher监听、限制输入长度、提示语等操作!

1.   首先需要一个Lookup的button, 一个可以输入字符的EditText 和一个显示单词结果的TextView;

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.chenye.dictionary.MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/wordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="input a word"
android:maxLength="20"
android:singleLine="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lookup"
android:onClick="lookup"/> </LinearLayout> <TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="word definition result"
android:textSize="20dp"/> </LinearLayout>

其中android:singleLine="true"为禁止虚拟键盘的操作; android:maxLength="20"为限制最大长度;android:hint="input a word"为提示语;这里EditText和button部分是水平布局,而它俩与TextView显示结果区域是垂直布局;

2.  java代码;

package com.chenye.dictionary;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.util.HashMap;
import java.util.Scanner; public class MainActivity extends AppCompatActivity {
private HashMap<String, String> dictionary;
EditText wordEditText;
TextView resultTextView; public void readAllDefinition() {
Scanner scanner = new Scanner(getResources().openRawResource(R.raw.gre_words));
if(this.dictionary == null){
this.dictionary = new HashMap<>();
}
while (scanner.hasNext()){
String line = scanner.nextLine();
String[] pieces = line.split("\t");
this.dictionary.put(pieces[0], pieces[1]); }
scanner.close();
}
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readAllDefinition();
wordEditText = findViewById(R.id.wordEditText);
resultTextView = findViewById(R.id.result);
wordEditText.addTextChangedListener(new TextWatcher() {
private CharSequence word; @Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
word = charSequence; } @Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
//resultTextView.setText("");
} @Override
public void afterTextChanged(Editable editable) {
if(word.length()==0) {
resultTextView.setText("word definition result");
}
else if (word.length() == 20){
Toast.makeText(MainActivity.this, "已到达输入的最大长度!", Toast.LENGTH_SHORT).show();
}
}
}); }
// button的方法
public void lookup(View view) {
EditText wordText = findViewById(R.id.wordEditText);
String word = wordText.getText().toString();
if(this.dictionary == null){
readAllDefinition();
}
String def = this.dictionary.get(word);
TextView defTextView = findViewById(R.id.result);
if(def == null){
defTextView.setText("word not fond!");
}else{
defTextView.setText(def);
}
} }
readAllDefinition()是在app启动的时候就会被调用的方法,此时已经将所有的单词,以及其对应的含义放在了map中;
其中,getResources().openRawResource(R.raw.gre_words)方法获取到InputStream流, gre_words是存放单词及对应含义的文本文件;
onCreate()方法中,分别采用wordEditText = findViewById(R.id.wordEditText);resultTextView = findViewById(R.id.result)方法获取id
最重要的是addTextChangedListener这个方法,是监听EditText的方法,里面有三个方法:
beforeTextChange()是在EditText改变之前里面可以做一些操作;
onTextChanged()是改变中;

afterTextChanged()是改变后;
参数:charSequence是这串字符串;start表示开始变化的位置,count表示变化的字符串长度,after表示变化之后该位置的字符数量, before表示变化之前该位置的字符数量;
3. 结果如下:
初始情况: 查询单词如下: 删除单词中间结果如下:

长度超出情况如下:

ps: 本宝也是刚开始学习android, 记录一下自己的学习过程,好记性不如烂笔头!

												

Android TextWatcher的使用方法(监听ExitText的方法)的更多相关文章

  1. Oracle Net Manager 的使用方法(监听的配置方法)

    一,在服务端配置oracle端口 win+R  输入netca 弹出如下窗口后 选择监听程序配置,点击下一步 二.配置端口后使用Telnet工具调试端口是否联通 在命令行输入telnet 服务器ip ...

  2. android -- 小问题 关于ListView设置了OnScrollListener之后onScrollStateChanged()和onScroll方法监听不到的问题

    关于ListView设置了OnScrollListener之后onScrollStateChanged()和onScroll方法监听不到的问题: 原因: 首先OnScrollListener是焦点滚动 ...

  3. 关于scrollview监听的一些方法

    一 package cn.testscrollview; import android.os.Bundle; import android.view.MotionEvent; import andro ...

  4. android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

    android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...

  5. javascript 原生方法监听DOM结构改变事件

    js原生方法监听DOM结构改变事件 document.addEventListener('DOMNodeInserted',function(){alert(1)},false);document.a ...

  6. android的Home键的监听封装工具类(一)

    android的Home键的监听封装: package com.gzcivil.utils; import android.content.BroadcastReceiver; import andr ...

  7. v-on可以监听多个方法吗?

    原文地址 v-on可以监听多个方法 <template> <div class="about"> <button @click="mycli ...

  8. 【Android】添加菜单和监听菜单方法详解

    添加菜单 可以在onCreateOptionsMenu或者onPrepareOptionsMenu方法中来添加菜单 代码添加: menu.add((int groupId, int itemId, i ...

  9. Android 另类方法监听软键盘的弹出收起事件

    http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...

随机推荐

  1. Pixel XL 刷机及Root详细方法

    需要准备的文件: 获取 Google USB 驱动程序并安装 https://developer.android.com/studio/run/win-usb 下载Google官方镜像文件 [8.1. ...

  2. python3爬虫03(find_all用法等)

    #read1.html文件# <html><head><title>The Dormouse's story</title></head># ...

  3. hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)

    题意:给一个字符串,求最长回文子串的长度. 思路: (1)暴力穷举.O(n^3) -----绝对不行. 穷举所有可能的出现子串O(n^2),再判断是否回文O(n).就是O(n*n*n)了. (2)记录 ...

  4. 【洛谷1993】小K的农场(差分约束系统模板题)

    点此看题面 大致题意: 给你若干组不等式,请你判断它们是否有解. 差分约束系统 看到若干组不等式,应该很容易想到差分约束系统吧. \(A-B≥C\):转换可得\(A-B≥C\) \(A-B≤C\):转 ...

  5. CentOS 7 防火墙 出现Failed to start iptables.service: Unit iptables.service failed to load

    错误信息如下: [root]# service iptables start Redirecting to /bin/systemctl start iptables.service Failed t ...

  6. 基于纹理内存的CUDA热传导模拟

    原文链接 项目中有三个,第一个是全局内存,其余两个分别是基于1d和2d纹理内存.项目打包下载. 纹理内存是只读内存,与常量内存相同的是,纹理内存也缓存在芯片中,因此某些情况下,它能减少对内存的请求并提 ...

  7. angular2新建组件

    1,使用ng g c hello 创建一个新的组件 它创建了4个文件,并更新了app.module.ts 如果想访问这个组件,只需要添加它的路由 成功访问这个组件 Import语句定义了我们需要用到的 ...

  8. git常用命令以及速查命令

    工作中使用的是git,所以写这个只是为了加深自己的记忆,提高熟练度 共勉~ git 主要命令 要关联一个远程库,使用命令git remote add origin git@server-name:pa ...

  9. windows 安装nodejs及配置服务

    一.什么是nodejs Node.js是一个Javascript运行环境(runtime).实际上它是对Google V8引擎进行了封装.V8引 擎执行Javascript的速度非常快,性能非常好.N ...

  10. 记python版本管理--pyenv

    随记: 众所周知,python2.x版本与3.x版本有比较大的区别,如果你是2.x版本的使用者,突然接了3.x版本的项目,或者反过来,遇到这种情况该怎么办呢?重新安装自己电脑上的python,来匹配对 ...