比较之前和之后的一个文本视图

时间:2017-01-05 21:39:26

标签: java android

我想在更改之前和之后比较单个textview的文本 例如。如果在该TextView中再次显示9的值(从JSON接收数据),则textview为9我想要发出一个警告,说明它等于之前的textview值。

我有这个代码(请注意,我知道这是错误的仅用于上下文目的):

if(brandnumber.getText.toString.equals(brandnumber.getText.toString){
//Alert saying its the same value after and before
}

3 个答案:

答案 0 :(得分:2)

  1. 将“”之前的“存储在正确的数据类型中Stringint,..)
  2. “new”值与存储值进行比较(如果它们匹配)(就像您在示例中所做的那样)

    // The initial value, store it in String
    String beforeText = brandnumber.getText().toString();
    
    // After change occurs (e.g.new data from JSON) in TextView store the new value in String
    String currentText = brandnumber.getText().toString();
    // Now you can create a method for comparing the values which will return the boolean True of False depending if they match
    if(compareTextValues(beforeText,currentText) {
        // Do what you want here if they match
    }
    
    // Method to compare two string, it returns True if they match
    private boolean compareTextValues(String beforeText, String currentText)
    {
     return beforeText.equals(currentText);
    }
    

答案 1 :(得分:0)

//get the previous value
String mText = brandnumber.getText().toString();

//set the new value
brandnumber.setText("9");

// compare two values
if(text.equals(brandnumber.getText().toString()){
}

答案 2 :(得分:0)

我使用aftertextchanged方法和所有TextViews的beforetextchanged方法解决了这个问题:)感谢所有