2017-01-05 94 views
0

我想比較單個文本的文本前後變化 例如。如果在該TextView中再次顯示9的值(正在接收來自JSON的數據),那麼textview是9,我想要提示說與先前的textview值相等。比較一個textview前後

我有這樣的代碼(注意,我知道這是錯的只是上下文的目的):

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

您可能需要打開更多的上下文。 –

回答

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
  1. 存放在正確的數據類型(在「之前」Stringint,..)
  2. 比較「新」值與存儲值,如果它們匹配(就像你在你的例子中所做的那樣)

    // 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); 
    } 
    
0

我決定使用aftertextchanged方法和beforetextchanged方法這個問題,所有的TextViews有:)感謝所有