저는 Android 개발이 처음입니다. 꽤 간단하게 뭔가를 시도하는 - 타이머가 틱 때 일부 표시 텍스트를 변경하십시오. 여기에 잠재적으로 관련 코드입니다 :TextView의 텍스트 변경 - 오래된 텍스트가 사라지지 않습니다 (Android 4.1.2)
CountDownTimer currentTimer;
Resources res;
TextView timerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
res = getResources();
timerText = (TextView) findViewById(R.id.timer_text);
}
@Override
protected void onStart() {
super.onStart();
//"Get ready" countdown
currentTimer = new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timerText.setText("" + (int)Math.ceil(millisUntilFinished/1000.0));
}
@Override
public void onFinish() {
...
}
};
currentTimer.start();
}
이 (물리적 및 에뮬레이션 모두) 에뮬레이트 4.2.2 장치에서 잘 작동하지만, 4.1.2 장치가 변경된 텍스트 뷰는 카운트 다운이 진행하는 동안 다음과 같이 나타납니다
당신이 말할 수없는 경우 , 즉 5,4,3가 오버레이 숫자입니다. 따라서 TextView에 새 문자열을 설정하면 새 문자열이 표시되지만 이전 문자열은 대체하지 않습니다. 내 응용 프로그램에서 사용되는 다른 TextViews도 같은 방식으로 동작합니다.
어떤 아이디어가 문제이며 해결 방법은 무엇입니까?
편집 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ExerciseActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical" >
...
<TextView
android:id="@+id/timer_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textIsSelectable="false"
android:hint="@string/timer_default" />
...
</LinearLayout>
관련이있을 수있는 전부 야 다음 XML 레이아웃 파일에서.
UI 스레드의 텍스트를 변경했는지 확인하십시오. 어떤 스레드가'onTick()'을 실행 중입니까? – SimonSays
레이아웃 XML 파일을 게시하십시오. – Squonk
텍스트를 설정 한 후'timerText.invalidate();를 실행하십시오. – 323go