久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

android on Text Change Listener

android on Text Change Listener(android on Text Change Listener)
本文介紹了android on Text Change Listener的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個情況,有兩個字段.field1field2.我所想要的當 field1 改變時, to do 為空 field2,反之亦然.所以只在最后一個字段上有內容.

I have a situation, where there are two fields. field1 and field2. All I want to do is empty field2 when field1 is changed and vice versa. So at the end only one field has content on it.

field1 = (EditText)findViewById(R.id.field1);
field2 = (EditText)findViewById(R.id.field2);

field1.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      field2.setText("");
   }
  });

field2.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
     field1.setText("");
   }
  });

如果我僅將 addTextChangedListener 附加到 field1 則效果很好,但是當我對應用程序崩潰的兩個領域都這樣做.顯然是因為他們試圖改變彼此無限期.一旦 field1 更改,此時它會清除 field2field2 已更改,因此它將清除 field1 等等...

It works fine if I attach addTextChangedListener to field1 only, but when I do it for both fields the app crashes. Obviously because they try to change each other indefinitely. Once field1 changes it clears field2 at this moment field2 is changed so it will clear field1 and so on...

有人可以提出任何解決方案嗎?

Can someone suggest any solution?

推薦答案

您可以添加一個檢查以僅在字段中的文本不為空時(即長度不為0時)清除.

You can add a check to only clear when the text in the field is not empty (i.e when the length is different than 0).

field1.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0)
        field2.setText("");
   }
  });

field2.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0)
         field1.setText("");
   }
  });

TextWatcher 的文檔此處.

還請遵守命名約定.

這篇關于android on Text Change Listener的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 91aiai| 久久久久亚洲精品 | 亚洲精品无人区 | 色综合色综合 | 欧美日韩久 | 国产一区中文字幕 | 欧美美女爱爱 | 欧美 日韩精品 | 日韩成人高清在线 | 欧美a免费 | 中文字幕视频在线 | www.日本在线 | 91中文字幕 | 国产成人99久久亚洲综合精品 | avav在线看| 久久久久久国产精品免费免费狐狸 | 久久久久久久久久久久久91 | 91在线成人 | 日韩中文字幕区 | 日韩在线视频一区 | 精品国偷自产在线 | 亚洲精彩视频在线观看 | 日韩欧美网 | 国产精品日韩在线观看一区二区 | 黄色在线播放视频 | 亚洲欧美日韩高清 | 狠狠操狠狠干 | 日本涩涩网 | 久久亚洲一区 | 天天色综 | 国产东北一级毛片 | 久草网视频| 我想看国产一级毛片 | 黄色免费av | 亚洲成人一区二区三区 | 精品久久久久久红码专区 | 91精品国产综合久久久久久丝袜 | 中文字幕一区在线观看视频 | 中文字幕免费 | www.日韩 | 最新中文字幕 |