Android之EditText文本變化的監聽

来源:http://www.cnblogs.com/whllong/archive/2016/04/30/5448623.html
-Advertisement-
Play Games

監聽EditText的文本變化需要給EditText控制項加一個addTextChangeListener監聽器 editText.addTextChangeListener(textWatcher); 這裡的textWatcher是一個TextWatcher對象, TextWatcher是一個介面, ...


  監聽EditText的文本變化需要給EditText控制項加一個addTextChangeListener監聽器

  editText.addTextChangeListener(textWatcher);  這裡的textWatcher是一個TextWatcher對象,

  TextWatcher是一個介面,它有三個抽象方法,具體如下:  

/**
*文本改變之前調用
* @param s 輸入框的原內容字元串
* @param start 字元串開始改變的索引位置
* @param count 即將被替換的字元個數
* @param after 替換這count個字元的新的字元的個數
*/  public void beforeTextChanged(CharSequence s, int start,int count, int after);

/**
* 文本改變時調用
* @param s 變化之後的輸入框內容
* @param start 字元串開始改變的索引位置
* @param before 被替換的字元的個數
* @param count 替換這before個字元的新的字元的個數
*/  public void onTextChanged(CharSequence s, int start, int before, int count);
/**
* 文本改變之後調用
* @param s 最終輸入框中的內容
*/  public void afterTextChanged(Editable s);
代碼:
MainActivity.java

 1 package com.example.admin.edittext;
 2 
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.text.Editable;
 6 import android.text.TextWatcher;
 7 import android.util.Log;
 8 
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.TextView;
13 
14 import com.lidroid.xutils.ViewUtils;
15 import com.lidroid.xutils.util.LogUtils;
16 import com.lidroid.xutils.view.annotation.ViewInject;
17 
18 public class MainActivity extends AppCompatActivity{
19     @ViewInject(R.id.text)
20     private TextView text;   //這裡使用了xUtils框架的註解功能初始化控制項
21 
22     @ViewInject(R.id.editText)
23     private EditText input;
24 
25     @ViewInject(R.id.button)
26     private Button button;
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_main);
31         ViewUtils.inject(this);
32         initEvent();
33     }
34 
35     private void initEvent() {
36         input.addTextChangedListener(change);
37         button.setOnClickListener(new View.OnClickListener() {
38             @Override
39             public void onClick(View v) {
40              input.setText("");
41             }
42         });
43     }
44 
45     TextWatcher change = new TextWatcher() {
46             @Override
47             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
48                 LogUtils.i("beforeTextChanged:"+s+","+start+","+count+","+after);
49             }
50 
51             @Override
52             public void onTextChanged(CharSequence s, int start, int before, int count) {
53                 text.setText("還能輸入"+(50-s.toString().length())+"個字元");
54                 LogUtils.i("onTextChange:"+s+","+start+","+before+","+count);
55             }
56 
57      
58             public void afterTextChanged(Editable s) {
59                 Log.i("Editable s:",s.toString());
60             }
61     };
62 
63 
64 }

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5   >
 6 
 7     <TextView
 8         android:id="@+id/text"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_marginTop="20dp"
12         android:text="還能輸入50個字" />
13 
14     <EditText
15         android:id="@+id/editText"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:layout_below="@id/text"
19         android:maxLength="50"
20         />
21     <Button
22         android:id="@+id/button"
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content"
25         android:text="重置文本"
26         android:textColor="#ffffff"
27         android:background="#ff0000"
28         android:layout_marginTop="10dp"
29         android:layout_below="@id/editText"
30         />
31 </RelativeLayout>

 

效果圖:



就在你沒有起身鞭策自己的一剎那,你不曉得退後了多少!

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • App Store: Pinyin Comparison 拼音辨別 新增拼音連連看小游戲,選擇拼音相同的兩個字,難度中等,很耐玩。 新增自由拼拼音模式,可以查看每種拼音對應的常見漢字,對理解聲母韻母很有幫助。 1. Add a pinyin selecting game 2. Now you can ...
  • 開發iOS經常會看見KVO和KVC這兩個概念,特地瞭解了一下。 我的新博客wossoneri.com "link" KVC Key Value Coding 是一種用間接方式訪問類的屬性的機制。比如你要給一個類中的屬性賦值或者取值,可以直接通過類和點運算符實現,當然也可以使用 。不過對於私有屬性,點 ...
  • 1. 常量和變數 常量 和 變數 把一個名字(比如 'number' 或者 'welcomeMessage')和一個指定類型的值(比如數字'10'或者字元串 ' "Hello" ' )關聯起來。常量的值一旦設定就不能改變,而變數的值可以隨意更改。 1> 聲明變數和常量 常量 和 變數 必須在使用前聲 ...
  • Cell屬於UITableView中的組件,有多種定義方式,有系統自帶的方法,有自定義的方法。 可以使用系統的方法setSeparatorColor(設置分割線顏色) 設置setSeparatorStyle(設置分割線類型) 也可以自己自定義一個Cell 在Cell的下麵添加一個極細的UIView, ...
  • 獲取WebView對象 調用WebView對象的getSettings()方法,獲取WebSettings對象 調用WebSettings對象的setJavaScriptEnabled()方法,設置js可用,參數:布爾值 在判斷是否支持js的時候,不要用alert(),預設不起作用,可以先用docu ...
  • 在佈局文件中添加<EditText/>和<Button/>控制項, 在佈局文件中添加<WebView/>控制項 在Activity中獲取WebView對象 調用WebView對象的loadUrl()方法,參數:String路徑 添加訪問網路的許可權android.permission.INTERNET 調 ...
  • 在Activity從創建到銷毀的過程中需要在不同的階段調用7個生命周期的方法這7個生命周期方法定義如下: 上面7個生命周期方法分別在4個階段按一定的順序進行調用 1,開始Activity:在這個階段依次執行3個生命周期方法,分別是onCreate,onStart,onResume 2,Activit ...
  • 我們先用AndroidStudio新建一個項目,選擇空白模板,然後像其中拖入兩個Button,將他們的id分別命名為btDate(顯示日期),btTime(顯示時間),他的模板XML代碼很簡單 如圖所示 一個標準的Android應用程式視窗類需要繼承android.app.Activity類,至少實 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...