2012-05-08 76 views
-1

我正在使用XML parser.in,我必须根据来自API的值更改textview的颜色。textview中的颜色变化

从api中将返回1或-1(对于我来说,如果它是1意味着我必须将背景更改为绿色,否则为红色)。

我该怎么做。

回答

3

简单...

TextView yourTextView = (TextView)findViewById(R.id.yourTextView); 

int response = responseFromParse(); // your parser logic 

if(response == 1){ 
    yourTextView.setBackgroundColor(Color.GREEN);  
}else{  
    yourTextView.setBackgroundColor(Color.RED);  
} 
+0

谢谢Rashmi.I多了一个doubt.In我的情况下,我存储值从API得到了在一个哈希映射,在这我怎么能检查VALU从哈希映射等于1或not.I试过这种if(map.get(KEY_DIRECTION).contains(“1”))但它不起作用。 – subburaj

+0

是你的HashMap是还是? – NullPointerException

+0

subburaj

0

这很容易:

if(API()==1) 
    textView.setBackgroundColor(R.color.black); 
else 
    textView.setBackgroundColor(R.color.black); 
0

如果returm值是字符串尝试

TextView txt =(TextView) findViewById(R.id.textView01);

String k;

if(k.contentEquals("1")){

`txt.setBackgroundColor(Color.GREEN);` 

}

else{

txt.setBackgroundColor(Color.RED); 

}

0

试试这个,

TextView txt= (TextView) findViewById(R.id.textview1); 
int val=Integer.parseInt(txt.getText().toString()); 
if(val==1) 
    txt.setBackgroundColor(Color.GREEN); 
else if(val==-1) 
    txt.setBackgroundColor(Color.RED); 
0

TextView text_view =(TextView)findViewById(R.id.textView1);

int returnval= your_returnval(); 

if(returnval== 1){ 

    text_view.setBackgroundColor(Color.GREEN); 

} 
else if(returnval== -1){ 

    text_view.setBackgroundColor(Color.RED);  
}