android中text怎么使用方法,如何在Android中使用TextWatcher类?

这个TextWatcher接口有3个回调方法,当文本发生更改时,这些方法都按以下顺序调用:

beforeTextChanged(CharSequence s, int start, int count, int after)

叫以前这些更改已应用于文本。

这个s参数是前文任何更改都适用。

这个start参数是位置修改后的部分在文本中的开头。

这个count参数是长度中更改的部分的s自start就位。

而after参数是新序列的长度它将取代s序列start到start+count.

你,你们绝不能改变中的文本TextView从这个方法(通过使用myTextView.setText(String newText)).

onTextChanged(CharSequence s, int start, int before, int count)

类似于beforeTextChanged方法,但称为后文字变了。

这个s参数是后面的文字已经进行了更改。

这个start参数与beforeTextChanged方法。

这个count参数是after参数在前TextChanged方法中。

而before参数是count参数在前TextChanged方法中。

你,你们绝不能改变中的文本TextView从这个方法(通过使用myTextView.setText(String newText)).

afterTextChanged(Editable s)

你,你们可以改变中的文本TextView从这个方法。

/!警告:当您更改TextView,TextWatcher将再次触发,启动无限循环。然后,您应该添加类似于boolean _ignore属性,该属性阻止无限循环。

例:new TextWatcher() {

boolean _ignore = false; // indicates if the change was made by the TextWatcher itself.

@Override

public void afterTextChanged(Editable s) {

if (_ignore)

return;

_ignore = true; // prevent infinite loop

// Change your text here.

// myTextView.setText(myNewText);

_ignore = false; // release, so the TextWatcher start to listen again.

}

// Other methods...

}

准备使用的类:TextViewListener

就我个人而言,我做了我的自定义文本监听器,它给了我4部分单独的字符串,这对我来说,使用起来更加直观。/**

* Text view listener which splits the update text event in four parts:

*     

The text placed  before the updated part.

*     

The  old text in the updated part.

*     

The  new text in the updated part.

*     

The text placed  after the updated part.

* Created by Jeremy B.

*/

public abstract class TextViewListener implements TextWatcher {

/**

* Unchanged sequence which is placed before the updated sequence.

*/

private String _before;

/**

* Updated sequence before the update.

*/

private String _old;

/**

* Updated sequence after the update.

*/

private String _new;

/**

* Unchanged sequence which is placed after the updated sequence.

*/

private String _after;

/**

* Indicates when changes are made from within the listener, should be omitted.

*/

private boolean _ignore = false;

@Override

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

_before = sequence.subSequence(0,start).toString();

_old = sequence.subSequence(start, start+count).toString();

_after = sequence.subSequence(start+count, sequence.length()).toString();

}

@Override

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

_new = sequence.subSequence(start, start+count).toString();

}

@Override

public void afterTextChanged(Editable sequence) {

if (_ignore)

return;

onTextChanged(_before, _old, _new, _after);

}

/**

* Triggered method when the text in the text view has changed.

* You can apply changes to the text view from this method

* with the condition to call {@link #startUpdates()} before any update,

* and to call {@link #endUpdates()} after them.

*

* @param before Unchanged part of the text placed before the updated part.

* @param old Old updated part of the text.

* @param aNew New updated part of the text?

* @param after Unchanged part of the text placed after the updated part.

*/

protected abstract void onTextChanged(String before, String old, String aNew, String after);

/**

* Call this method when you start to update the text view, so it stops listening to it and then prevent an infinite loop.

* @see #endUpdates()

*/

protected void startUpdates(){

_ignore = true;

}

/**

* Call this method when you finished to update the text view in order to restart to listen to it.

* @see #startUpdates()

*/

protected void endUpdates(){

_ignore = false;

}

}

例子:myEditText.addTextChangedListener(new TextViewListener() {

@Override

protected void onTextChanged(String before, String old, String aNew, String after) {

// intuitive usation of parametters

String completeOldText = before + old + after;

String completeNewText = before + aNew + after;

// update TextView

startUpdates(); // to prevent infinite loop.

myEditText.setText(myNewText);

endUpdates();

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值