【问题标题】:How to compare current value with old value in TextWatcher?如何在 TextWatcher 中将当前值与旧值进行比较?
【发布时间】:2017-06-05 10:02:16
【问题描述】:

我正在渲染数据库中的值并将这些值放在相应的 EditText 字段中,因此通常允许用户修改他们的保存数据。

我的页面上有一个保存按钮,默认情况下是禁用的。只有在真正修改了 EditText 字段时才应该启用它。

与数据库值比较时,

  1. 如果编辑文本值改变-修改
  2. 如果 EditText 值已编辑但未更改(用户可以复制粘贴相同的值)- 未修改。

这是我的代码,

EditText text1  = (EditText) findViewById(R.id.text1);
EditText text2  = (EditText) findViewById(R.id.text2);
EditText text3  = (EditText) findViewById(R.id.text3);
EditText text4  = (EditText) findViewById(R.id.text4);
EditText text5  = (EditText) findViewById(R.id.text5);

ArrayList<EditText> editTextList = new ArrayList();
editTextList.add(text1);
editTextList.add(text2);
editTextList.add(text3);
editTextList.add(text4);
editTextList.add(text5);

for(EditText e : editTextList)
  e.addTextChangedListener(textWatcher);

private TextWatcher textWatcher = new TextWatcher() {

public void afterTextChanged(Editable s){}
public void beforeTextChanged(CharSequence s, int start, int count, int 
after) {}
 public void onTextChanged(CharSequence s, int start, int before,int 
count) {}
};

有什么方法可以将数据库值与 EditText 中的当前给定值进行比较?

注意:

由于TextWatcher功能对所有EditText都是通用的,所以我想做比较通用的。

【问题讨论】:

    标签: android android-edittext android-textwatcher


    【解决方案1】:

    你必须自己用一个全局变量来控制它。或反应对象或使用数据绑定。

    【讨论】:

    • 感谢您的评论。你介意举个例子吗?
    • 全局变量示例:public Activity extends AppCompactActivity{}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2016-05-27
    • 1970-01-01
    • 2015-03-12
    相关资源
    最近更新 更多