[英]My screen doesn't want to refresh
我有一個問題,我敢肯定它解決起來並不復雜,但是無論我做什么都行不通!
這是我的問題:我有這種布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/backgroundshape"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_timer"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#f44242"
android:textSize="95dp"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="25dp"
android:layout_centerVertical="true"/>
<ImageView
android:id="@+id/image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="25dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
關鍵是單擊最后一個ImageView,該ImageView將占據所有屏幕並開始實例化其他View。 這是我的方法:
public class GameScreen extends Activity {
ImageView imgEmpty, img1, img2;
TextView tvTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second);
img1 = (ImageView)findViewById(R.id.image_1);
img2 = (ImageView)findViewById(R.id.image_2);
imgEmpty = (ImageView)findViewById(R.id.image_empty);
imgEmpty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img1.setBackgroundResource(R.drawable.feuille);
img2.setBackgroundResource(R.drawable.feuille);
tvTimer.setTextSize(95);
for(int i=0; i<3; i++) {
imgEmpty.setVisibility(View.GONE);
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); Log.d("Ratatataaa","Y passe pas !!!!"); }
tvTimer.setText(""+i);
view.invalidate();
}
}
});
tvTimer = (TextView)findViewById(R.id.text_timer);
tvTimer.setTextSize(50);
tvTimer.setText("Ready ?");
}
}
無論我做什么(invalidate(),...),屏幕僅在onClick(View view)的末尾刷新
請幫忙
那里幾乎沒有不好的暗示。 例如對於主線程使用sleep()
,並在錯誤的位置初始化TextView
。
試試這個代替:
....
img1 = (ImageView)findViewById(R.id.image_1);
img2 = (ImageView)findViewById(R.id.image_2);
tvTimer = (TextView)findViewById(R.id.text_timer);
tvTimer.setTextSize(50);
tvTimer.setText("Ready ?");
imgEmpty = (ImageView)findViewById(R.id.image_empty);
imgEmpty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
img1.setBackgroundResource(R.drawable.feuille);
img2.setBackgroundResource(R.drawable.feuille);
tvTimer.setTextSize(95);
imgEmpty.setVisibility(View.GONE);
view.invalidate();
new CountDownTimer(3000, 1000) {
@Override
public void onTick (long millisUntilFinished) {
tvTimer.setText("" + ((int)(3000 - millisUntilFinisehd)/1000));
}
@Override
public void onFinish () {}
};
}
});
....
我通過添加一個處理程序和一個Runnable解決了這個問題。
看到這個: 如何在Android中設置計時器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.