gpt4 book ai didi

java - getText 与 onTextChanged charSequence

转载 作者:行者123 更新时间:2023-12-01 13:34:56 25 4
gpt4 key购买 nike

我正在学习如何编写 Android 应用程序并有一个问题。使用 TextWatcher 和在 onTextChanged 方法中设置一个等于 CharSequence 参数的字符串值并简单地使用 getText 方法之间有什么区别。

private TextWatcher passwordListener = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
try{
password = charSequence.toString();
}

catch(Exception e){
password=null;
}
}

对比

password = password_EditText.getText().toString();

最佳答案

如果您希望收到文本已更改的通知并且需要执行某些操作来直接响应该事件本身,则可以使用文本观察器。例如,如果您想即时进行一些自定义验证并根据输入的值自动启用/禁用按钮。

使用 getText 只是简单地返回特定时间编辑文本字段中的内容。如果您想通过其他事件(例如单击按钮)获取文本值,则可以使用此方法。

关于java - getText 与 onTextChanged charSequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com