如何在onchangedlistnerandroid中保持textview更改时的触发背景色



enter code here在android java中,如何在onchangedlistner android中保持文本视图更改时的触发背景色我得到的号码是49500,如果它增加,然后用绿色改变背景,如果减少,它将在后台用红色改变,如果什么都没有发生,那么背景中的颜色保持不变

我正在从firebase 获取实时数据

如果有任何帮助,谢谢你这是我的代码,如果可能的话请帮我谢谢

@Overridepublic View onCreateView(布局充气器充气器、ViewGroup容器、,Bundle savedInstanceState({查看rateviwe=充气机充气(R.layout.fragment_live_rate,container,false(;

gold9999 = rateviwe.findViewById(R.id.gold9999id);
gold9950 = rateviwe.findViewById(R.id.gold9950id);
silver9999 = rateviwe.findViewById(R.id.silver9999id);
silver9950 = rateviwe.findViewById(R.id.silver9950id);
mcxgoldbuy = rateviwe.findViewById(R.id.goldmcxbuyid);
mcxgoldsell = rateviwe.findViewById(R.id.goldmcxsellid);
mcxgoldhighlow = rateviwe.findViewById(R.id.goldmcxhiglowid);
mcxsilverbuy = rateviwe.findViewById(R.id.silvermcxbuyid);
mcxsilversell = rateviwe.findViewById(R.id.silvermcxsellid);
mcxsilverhighlow = rateviwe.findViewById(R.id.silvermcxhighlowid);
goldcomexbuy = rateviwe.findViewById(R.id.goldcomexbuyid);
goldcomexsell = rateviwe.findViewById(R.id.goldcomexsellid);
goldcomexhighlow = rateviwe.findViewById(R.id.goldcomexhighlowid);
silvercomexbuy = rateviwe.findViewById(R.id.silvercomexbuyid);
silvercomexsell = rateviwe.findViewById(R.id.silvercomexsellid);
silvercomexhighlow = rateviwe.findViewById(R.id.silvercomexhighlowid);
inrcomexbuy = rateviwe.findViewById(R.id.inrcomexbuyid);
inrcomexsell = rateviwe.findViewById(R.id.inrcomexsellid);
inrcomexhighlow = rateviwe.findViewById(R.id.inrcomexhighlowid);
marquetext = rateviwe.findViewById(R.id.marqueeText);
swipeRefreshLayout = rateviwe.findViewById(R.id.pullToRefresh);
mainHandler = new Handler(Looper.getMainLooper());
scrollView = rateviwe.findViewById(R.id.scrollView);
scrollView.setNestedScrollingEnabled(true);
marquetext.setSelected(true);

gold9999.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String one=gold9999.toString();
String two=charSequence.toString();

//int color=i1>我?R.color.green:R.color.red;//gold9999.setBackgroundColor(getResources((.getColor(color((;

/*  if (!(one ==two)) {
goldcomexbuy.setBackgroundColor(Color.parseColor("#242B2E"));
} else {
goldcomexbuy.setBackgroundColor(Color.rgb(255, 0, 0));
}*/
}
@Override
public void afterTextChanged(Editable editable) {

}
});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
try {
onStart();
swipeRefreshLayout.setRefreshing(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});

return rateviwe;
}


@Override
public void onStart () {
super.onStart();

myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {

String gold9999rate = dataSnapshot.child("gold9999").getValue().toString();
String gold9950rate = dataSnapshot.child("gold9950").getValue().toString();
String silver9999rate = dataSnapshot.child("silver9999").getValue().toString();
String silver9950rate = dataSnapshot.child("silver99").getValue().toString();
String goldcomexbuyrate = dataSnapshot.child("goldcomexbuy").getValue().toString();
String goldcomexsellrate = dataSnapshot.child("goldcomexsell").getValue().toString();
String goldcomexhighrate = dataSnapshot.child("goldcomexhigh").getValue().toString();
String goldcomexlowrate = dataSnapshot.child("goldcomexlow").getValue().toString();
String silvercomexbuyrate = dataSnapshot.child("silvercomexbuy").getValue().toString();
String silvercomexsellrate = dataSnapshot.child("silvercomexsell").getValue().toString();
String silvercomexhighrate = dataSnapshot.child("silvercomexhigh").getValue().toString();
String silvercomexlowrate = dataSnapshot.child("silvercomexlow").getValue().toString();
String inrcomexbuyrate = dataSnapshot.child("inrcomexbuy").getValue().toString();
String inrcomexsellrate = dataSnapshot.child("inrcomexsell").getValue().toString();
String inrcomexhighrate = dataSnapshot.child("inrcomexhigh").getValue().toString();
String inrcomexlowrate = dataSnapshot.child("inrcomexlow").getValue().toString();
String goldmcxbuyrate = dataSnapshot.child("goldmcxbuy").getValue().toString();
String goldmcxsellrate = dataSnapshot.child("goldmcxsell").getValue().toString();
String goldmcxhighrate = dataSnapshot.child("goldmcxhigh").getValue().toString();
String goldmcxlowrate = dataSnapshot.child("goldmcxlow").getValue().toString();
String silvermcxbuyrate = dataSnapshot.child("silvermcxbuy").getValue().toString();
String silvermcxsellrate = dataSnapshot.child("silvermcxsell").getValue().toString();
String silvermcxhighrate = dataSnapshot.child("silvermcxhigh").getValue().toString();
String silvermcxlowrate = dataSnapshot.child("silvermcxlow").getValue().toString();
gold9999.setText(gold9999rate);
gold9950.setText(gold9950rate);
silver9999.setText(silver9999rate);
silver9950.setText(silver9950rate);
goldcomexbuy.setText(goldcomexbuyrate);

/* if (!goldcomexbuyrate.isEmpty()) {
goldcomexbuy.setBackgroundColor(Color.rgb(255, 0, 0));
} else {
goldcomexbuy.setBackgroundColor(Color.parseColor("#242B2E"));
} */
goldcomexsell.setText(goldcomexsellrate);
goldcomexhighlow.setText(goldcomexhighrate + "/" + goldcomexlowrate);
silvercomexbuy.setText(silvercomexbuyrate);
silvercomexsell.setText(silvercomexsellrate);
silvercomexhighlow.setText(silvercomexhighrate + "/" + silvercomexlowrate);
inrcomexbuy.setText(inrcomexbuyrate);
inrcomexsell.setText(inrcomexsellrate);
inrcomexhighlow.setText(inrcomexhighrate + "/" + inrcomexlowrate);
mcxgoldbuy.setText(goldmcxbuyrate);
mcxgoldsell.setText(goldmcxsellrate);
mcxgoldhighlow.setText(goldmcxhighrate + "/" + goldmcxlowrate);
mcxsilverbuy.setText(silvermcxbuyrate);
mcxsilversell.setText(silvermcxsellrate);
mcxsilverhighlow.setText(silvermcxhighrate + "/" + silvermcxlowrate);


}catch (Exception e){
e.printStackTrace();

}

}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("TAP","something wrong");
Toast toast=Toast.makeText(getContext(),"No Internet connection. Please try again",Toast.LENGTH_SHORT);
toast.setMargin(50,50);
toast.show();
}
});
}
}

您可以尝试这样的操作,保存以前的值并检查您的数字是减少还是增加。但您应该提供您的代码以获得更好的答案。

int savedLastItem = 49500;
yourObjectLiveData.observe(getViewLifecycleOwner(), object -> 
if(object>savedLastItem){
textView.setBackground(<yourColor>);
} else if (object<savedLastItem) {
textView.setBackground(<yourColor>);
}          
savedLastItem = object; //if you need to save last number
});

最新更新