2010-06-10 20 views
69

我想知道編輯文本框的實時字符計數的最佳方式是在Android中。我在看this,但我似乎無法理解它。EditText的實時字符計數

爲了描述這個問題,我有一個EditText,我試圖將字符數限制爲150.我可以使用輸入過濾器來實現,但是我想在文本框下方顯示用戶字符數已進入(幾乎像堆棧溢出正在執行)。

如果有人可以寫一小段示例代碼或指向正確的方向,我會很感激。

回答

128

可以使用TextWatcher時看到的文本已更改

private TextView mTextView; 
private EditText mEditText; 
private final TextWatcher mTextEditorWatcher = new TextWatcher() { 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      //This sets a textview to the current length 
      mTextView.setText(String.valueOf(s.length())); 
     } 

     public void afterTextChanged(Editable s) { 
     } 
}; 

您設置TextWatcher的EditText上與

mEditText.addTextChangedListener(mTextEditorWatcher); 
+5

想這一點,偉大工程!應被選爲正確答案! – 2011-01-03 05:10:14

+2

在文本框的右下角添加計數的最佳方式是什麼。擴展編輯文本並手動繪製字符串? – Bear 2012-01-07 16:40:36

+0

好工作 – 2013-06-27 10:09:03

5

它非常簡單按照以下說明:

====將它們添加到您的進口===

import android.text.Editable; 
import android.text.TextWatcher; 

=====定義這個=====

private TextView sms_count; 

==========裏面上創建=====

sms_count = (TextView) findViewById(R.id.textView2); 


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

    public void onTextChanged(CharSequence s, int start, int before, int count) { 

     sms_count.setText(String.valueOf(s.length())); 
    } 

    public void afterTextChanged(Editable s) { 
    } 
}; 

sms_message.addTextChangedListener(txwatcher); 
0

試着做喜歡這個。

與獲取CharSequence.length相比,此解決方案可能更具性能。每次點擊軟鍵盤時,事件都會觸發;因此,如果你做了一段時間,它會每次計算CharSequence,如果你開始進入大型CharSequnces,這可能會變慢。關於文本更改的事件監聽器會記錄前後的計數。這非常適用於增量和減量值

@Override 
     public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { 
      int tick = start + after; 
      if(tick < mMessageMax) { 
       int remaining = mMessageMax - tick; 
       ((TextView)findViewById(R.id.contact_us_chars)).setText(String.valueOf(remaining)); 
      } 
     } 
+0

這是效果permosrmatce,TextWatcher是最好的方法 – 2014-03-24 09:51:20

4
You can use TextWatcher class to see text has changed and how much number of character remains.Here i have set counter of 140 characters. 

    EditText typeMessageToPost; 
    TextView number_of_character; 
public void onCreate(Bundle savedBundleInstance) { 
     super.onCreate(savedBundleInstance); 
setContentView(R.layout.post_activity); 
typeMessageToPost.addTextChangedListener(mTextEditorWatcher); 
} 
private final TextWatcher mTextEditorWatcher=new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
      number_of_character.setText(String.valueOf(140-s.length())); 
     } 
    }; 
+0

完美................ – 2017-07-14 11:11:30

+0

謝謝@RavishSharma :)我感謝您的評論。 – Rana 2017-07-15 13:27:28

1

我遇到了同樣的問題,我想卡梅隆的方法。它可以工作,但有一個小錯誤:如果用戶使用複製和粘貼,然後它不能計算字符。因此,我建議做後面的文本改變了,象下面這樣:

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

     } 

     public void onTextChanged(CharSequence s, int start, int before, int count) { 

     } 

      public void afterTextChanged(Editable s) { 
      //This sets a textview to the current length 
      mTextView.setText(String.valueOf(s.length())); 
     } 
    }; 
81

你可以做文字使用TextInputLayout包裝器的EditText XML本身計數SupportLibrary v23.1

推出只是一個TextInputLayout並設置CounterEnabled包裝你的EditText爲true並設置counterMaxLength。

<android.support.design.widget.TextInputLayout 
    android:id="@+id/textContainer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:counterEnabled="true" 
    app:counterMaxLength="20" 
    > 
    <EditText 
     android:id="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Text Hint" 
     /> 
</android.support.design.widget.TextInputLayout> 

你會得到像this

有重大影響您可以使用counterOverflowTextAppearancecounterTextAppearance樣式櫃檯。

編輯

從Android文檔。

TextInputEditText提供的類用作此佈局的子項。使用TextInputEditText允許TextInputLayout更好地控制任何文本輸入的視覺方面。一個例子用法如下這樣:

 <android.support.design.widget.TextInputLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

    <android.support.design.widget.TextInputEditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/form_username"/> 

</android.support.design.widget.TextInputLayout> 

TextInputLayoutTextInputEditText

+3

這正是我期待的:)一個乾淨的支持庫impl。謝謝 – 2015-12-06 10:15:47

+2

應該是接受的答案!我完全錯過了這些屬性! – sud007 2016-01-10 14:43:33

+0

太棒了!但可惜的是鍵盤覆蓋的計數器... – SjoerdvGestel 2017-08-17 10:39:39

1

使用Android:最大長度= 「140」

這應該工作。 :)

希望幫助

3

在XML文件中只需設置這兩條線在TextInputLayout

app:counterEnabled="true" 
app:counterMaxLength="200" 
+0

偉大的不知道這一點。看起來總是將Edittext包裝到TextInputLayout中的更好的解決方案。 – 2017-07-01 08:00:27

2

在XML在java中添加此屬性爲EDITTEXT

android:maxLength="80" 

添加此聽衆

ed_caption.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) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      tv_counter.setText(80 - s.toString().length() + "/80"); 

     } 
    }); 
5

您可以用TextInputLayout和compat的圖書館做:

app:counterEnabled="true" 
app:counterMaxLength="420" 

和完整:

<android.support.design.widget.TextInputLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:counterEnabled="true" 
    app:counterMaxLength="420"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:maxLength="420" /> 

</android.support.design.widget.TextInputLayout> 
+0

偉大的這爲我工作,但我如何改變櫃檯的顏色? – 2018-01-29 20:12:49