2016-03-05 28 views
0

讓我們假設我有2個EditText對象inp1和inp2。如果我在inp1中輸入內容,我希望它出現在inp2中,反之亦然。兩者中所做的更改都會改變另一個。我實際上想要兩個EditText對象分別輸入10和2的數字。當我輸入一個二進制數時,我希望它的等價十進制數出現在另一個EditText中,反之亦然,而不使用任何按鈕或任何東西。有沒有什麼等同於EditText的按鈕的onClick屬性?每當EditText的文本發生變化時,它可以自動調用一個函數。 我希望我可以明確提出我的問題。 謝謝。在沒有按鈕的情況下在一個編輯文本中輸入時在另一個edittext中輸入輸入按

+0

檢查此鏈接 - https://github.com/grantland/android-autofittextview – Ajinkya

回答

0

您需要在兩個字段上使用TextWatcher。這個問題的答案question基本上沒有你的描述

0

可以使用TextWatcher這樣

tagNameEditText.addTextChangedListener(new TextWatcher() { 

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

       } 


       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // set text or result in other EditText here 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 

           } 
      }); 

在這個監聽器onTextChanged()方法,你可以設置文本在其他的EditText

0

使用TextWatcher。 這是短代碼:

private final TextWatcher edit_one_Watcher = new TextWatcher() { 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     // enter your logic here 
//and print in second edittext 
     } 

     public void afterTextChanged(Editable s) { 

     } 
    }; 

而且

private final TextWatcher edit_second_Watcher = new TextWatcher() { 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // enter your logic here 
    //and print in first edittext 
      } 

      public void afterTextChanged(Editable s) { 

      } 
     }; 

注:使用布爾/標誌忽略autotextchange。

.i.e. boolean ignoreFirstTextChange = true; 
.i.e. boolean ignoreSecondTextChange = true; 
+0

讓我們假設輸入爲INP1和INP2。我如何初始化它們以使用TextWatcher功能?它告訴我edit_one_Watcher和edit_second_Watcher從不使用。 –

+0

inp1.addTextChangedListener(edit_one_Watcher); inp2.addTextChangedListener(edit_second_Watcher); –

相關問題