【问题标题】:My screen doesn't want to refresh我的屏幕不想刷新
【发布时间】:2018-03-10 10:04:14
【问题描述】:

我有一个问题,我确信解决起来并不复杂,但无论我做什么都行不通!

这是我的问题:我有这个布局

<?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,开始实例化其他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)结束时刷新

请帮忙

【问题讨论】:

  • 你的预期行为根本不清楚,说明你想要什么以及发生了什么
  • 另外,你为什么要在循环中三次尝试setVisibility()invalidate() 相同的视图。
  • @Pavneet_Singh 我的预期行为是,当您单击“imgEmpty”时,其他 2 个 ImageView 必须显示我想要的图像,并且 TextView 必须打印 0 然后 1 然后 2。发生的事情是图像并显示文本,但在功能结束时,我有我的 2 个图像和“2”而不是我的 2 个图像和 0 然后 1 然后 2
  • @ZeekHuge 如果您不太了解我想要的内容,请阅读上面的评论。我有 setVisibility 让我的第一个图像消失,我尝试使我的屏幕无效
  • 查看thisthis

标签: android


【解决方案1】:

那里很少有不好的暗示。例如将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 () {}
        };
    }
});
....

【讨论】:

    【解决方案2】:

    我通过添加 Handler 和 Runnable 解决了这个问题。

    看到这个:How to set a timer in android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多