使用setText到TextView时创建的新TextView

时间:2019-02-09 14:37:08

标签: java android textview

我的Android应用中有一个TextView。当我使用setText()TextView的旧文本时,仍然显示新文本。如果我关闭手机的屏幕并在一段时间后打开手机,TextView的旧文本就消失了。

enter image description here

我该如何解决?

灰色是TextView的旧文本,绿色是TextView的新文本

这是xml代码:

<TextView
    android:id="@+id/challengeStateInResultActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="5dp"
    android:background="@color/gray"
    android:padding="7dp"
    android:text="جارى التحميل ..."
    android:textColor="@color/white"
    android:textSize="20sp" />

这是演示者代码:

     fireStoreChallenges.document(challengeId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                final DocumentSnapshot documentSnapshot = task.getResult();
                final long opponentScore = documentSnapshot.getLong("player1score");

                String player1uid = documentSnapshot.getString("player1Uid");
                fireStoreUsers.document(player1uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            String opponentName = document.getString("userName");
                            String opponentImage = document.getString("userImage");

                            view.setOpponentData(opponentScore, opponentName, opponentImage);
                        }
                    }
                });

                int opponentScoreInt = (int) opponentScore;

                if (score == opponentScoreInt) {
                    view.setChallengeTvText(drawChallengeText);
                } else {
                    if (score > opponentScoreInt) {
                        view.setChallengeTvText(wonChallengeText);
                        view.setChallengeTvBGColor(context.getResources().getColor(R.color.green));
                    } else {
                        view.setChallengeTvText(loseChallengeText);
                        view.setChallengeTvBGColor(context.getResources().getColor(R.color.red));
                    }
                }
            }
        }
    });

这是视图中的方法:

@Override
public void setChallengeTvText(String text) {
    challengeStateTv.setText(text);
    challengeStateTv.invalidate();
}

1 个答案:

答案 0 :(得分:0)

要更新先前的文本,可以使用

更新TextView。
  

TextView.setText("NEW_TEXT");

代码示例:

TextView tv;

// inside OnCreate method
tv = (TextView) findViewById(R.id.TextView) // link to your xml TextView Definition

// to clear the TextView you can use as below
// tv.setText("");

// to set Text as "Hello" 
tv.setText("Hello");