Android 如何计算文本中的字符数
在我的应用程序中,我想计算edittext.中的特许权数,在字符数达到某个限制后,计数器应再次以140开头,我想在字符数达到0后以140/1的格式显示,它再次以140统计,但以1乘以1的增量递增,并显示为140/2。我怎么做?有什么建议吗Android 如何计算文本中的字符数,android,character,textwatcher,character-limit,Android,Character,Textwatcher,Character Limit,在我的应用程序中,我想计算edittext.中的特许权数,在字符数达到某个限制后,计数器应再次以140开头,我想在字符数达到0后以140/1的格式显示,它再次以140统计,但以1乘以1的增量递增,并显示为140/2。我怎么做?有什么建议吗 edMessage.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before,
edMessage.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// Display Remaining Character with respective color
count = MAX_COUNT - s.length();
Word_counter.setText(Integer.toString(count) + "/" + Integer.toString(word) );
Word_counter.setTextColor(Color.BLACK);
if(count < 10)
{
Word_counter.setTextColor(Color.YELLOW);
}
}
});
试试这个方法,希望这能帮助你解决你的问题
activity_main.xml
@Haresh我希望它显示为140/1,当s.length==140时,用户可以再次键入,但显示为140/2.,这里140是他可以在一条消息中键入的字符数。@user3683036,你能在我的代码中解释得更具体些吗?我显示140个字符后没有字符数用户类型他不能键入更多字符。是的,但我想在140个字符后用户可以键入,但s.length再次以140个字符开始..然后出现一个祝酒词,告诉他第二条消息已开始@Haresh没有显示我键入的字符数在edittext中,从140开始,剩下的我留下了,。u将其修复为140。。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
public class MainActivity extends Activity {
private EditText editText;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
editText.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) {
if(s.length()/140 == 0){
textView.setText("140");
}else{
textView.setText("140/"+String.valueOf((s.length()/140)+1));
}
}
});
}
}