2012-07-24 65 views
1

我有一個比較字符串的問題:我得到string.xml中的「激活」的字符串值。當我與具有相同值的字符串值進行比較,結果總是假的(在string.xml激活=測試)getString()string.xml比較

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="app_name">MyNameApp</string> 
    <string name="activation">test</string> 
</resources> 

public class CheckGenuine {  
    public static String cod; 
    public static String app; 

    public static Boolean chk(Context ctx) {  
     Boolean ret; 
     cod = ctx.getString(R.string.activation); 
     app = ctx.getString(R.string.app_name); 

     if (cod == "test") { 
      Toast.makeText(ctx, "True cod = " + cod, Toast.LENGTH_LONG).show();  
      ret = true;} 
     else { 
      Toast.makeText(ctx, "False cod = " + cod, Toast.LENGTH_LONG).show(); 
      ret = false;} 

    // *** why ret is always false and Toast shows "False cod = test" ???????????????? 

     return ret;  
    } 

} 

回答

4

您應該使用.equals()來比較兩個字符串值,而不是==運營商。 This article在解釋差異方面做得很好。

+0

謝謝!我瘋了:-) – user1549319 2012-07-24 17:21:15

1

使用if (cod.equals("test")) {

== - >>比較字符串refrences(存儲位置)

.equals()比較字符串值。