简体   繁体   English

简单的while循环不起作用

[英]Simple while-loop doesnt work

I got a little problem. 我有一个小问题。 I think the solution is very simple but unfortunately I can't find it. 我认为解决方案非常简单,但不幸的是我找不到它。 I hope someone can help me I got a while-loop who has to count up to ten and write the number into a TextView. 我希望有人可以帮助我,我有一个while循环,他必须数到十并将数字写到TextView中。 It still doesn't work ... Thanks for your help! 它仍然无法正常工作...谢谢您的帮助! Here is the code: 这是代码:

package de.androidnewcomer.animation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import static android.R.attr.button;
import static de.androidnewcomer.animation.R.id.textView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView ball=(ImageView)findViewById(R.id.ball);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            count();
            break;
    }
}
private void count() {
    TextView textView=(TextView)findViewById(R.id.textView);
    int i;
    i=1;
    while(i<10) {
        i++;
        textView.setText(i);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}

Using setText() with an integer value is for setting a string resource reference. 将setText()与整数值一起使用是为了设置字符串资源引用。 To set the text itself, you have to provide a string: Use setText("" + i); 要设置文本本身,您必须提供一个字符串:使用setText("" + i); and it should work. 它应该工作。

The textView.setText(..) needs a string object, but you use a int. textView.setText(..)需要一个字符串对象,但您使用的是int。 You have to convert your int into a string with the following possible options: 您必须使用以下可能的选项将int转换为字符串:

  1. You can use String.valueOf(i) : textView.setText(String.valueOf(i)); 您可以使用String.valueOf(i)textView.setText(String.valueOf(i));

  2. You can use Integer.toString(i) : textView.setText(Integer.toString(i)); 您可以使用Integer.toString(i)textView.setText(Integer.toString(i));

  3. You can use the empty string literal : textView.setText("" + i); 您可以使用空字符串文字textView.setText("" + i);

I prefer the last option. 我更喜欢最后一个选择。 With your code, it should looks like the following code: 使用您的代码,它应类似于以下代码:

private void count() {
   TextView textView=(TextView)findViewById(R.id.textView);
   int i;
   i=1;
   while(i<10) {
       i++;
       textView.setText("" + i);
       try {
           Thread.sleep(200);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
   }
}

You can use a for loop instead of a while loop, like the following code: 您可以使用for循环而不是while循环,例如以下代码:

private void count() {
   TextView textView=(TextView)findViewById(R.id.textView);

   for(int i = 1; i < 11; i++){ // From 1 to 10
       textView.setText("" + i);
       Thread.sleep(200); 
   }
}

Use a CountDown because you are blocking the main thread 使用CountDown,因为您正在阻塞主线程

CountDownTimer countDownTimer = new CountDownTimer(2000 /*amount*/, 200/*step*/) {
        public void onTick(long millisUntilFinished) {
           textView.setText("what ever you want");    
        }

        public void onFinish() {
            textView.setText("Done");
        }
    };
countDownTimer.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM