没有错误,编译,没有运行时错误。 嘿,我有一个关于Android总和的简单问题。我正在做一个简单的求和,代码编译,它在技术上做了它想做的事情,将数字加总直到大于120.但是,我希望输出在循环中显示每个总和。不幸的是,它只是立即跳到textview的最后一笔。我尝试使用睡眠功能,等待功能,但最终发生的是它会等待所有循环的时间,直到总和大于120,然后输出最终总和。在这种情况下4950.有关在文本视图中显示每个总和的任何建议吗?
我也试过使用这行代码,它可以单独显示每个总和,但是,它立即启动了计时器。它不应该做什么。 Toast.makeText(MainActivity.this,“SUM:”+ sum1,Toast.LENGTH_SHORT).show();
我的简单应用程序的基本目标 1)显示每个总和 2)当总和大于120启动计时器
问题:如何让它在文本视图中单独显示每个总和而不是跳到最终总和?
public void onClick(View v)
for(i=0;i<100;i++)
{
sum1 = sum1 + i;
sum.setText("Sum:"+sum1);
if(sum1 > 120)
{
timer.start();
}
}
}
感谢您的时间。
答案 0 :(得分:2)
它“跳转”到最后一个的原因是因为点击动作在阻止UI线程时运行for循环。您实际上无法看到文本更改,但它正在发生变化。您可以使用Timer或TimerTask来完成所需的操作,这基本上可以为您创建一个单独的线程。它还可以处理您需要的延迟。只需在Timers执行方法中进行数学运算。
//make sure these are global
int i = 0;
int sum1 = 2;//not really sure where your sum starts so you choose
TimerTask leTask = new TimerTask() {
public void run() {
sum1 = sum1+i;
i++;
/*I don't actually remember if you must go back to the main thread
* when in the tasks run method. Incase you get an error because
* of that then just do that quickly. I have included a link for
* you on how to do that if necessary.*/
sum.setText("Sum:"+sum1);
if(sum1 > 120){
/*Start your actually timer here
* and stop execution of leTask by calling the cancel method*/
}
}
int delay = 0;//you don't really want a delay since you want it immediately after click
int everySecond = 1000;
timer.scheduleAtFixedRate(leTask, 0, everySecond);
请记住我设置的方式只是一个例子。取消后,您应确保可以访问leTask
(或任何您称之为的内容)。 i
和sum1
也应该是全球性的。 delay
和everySecond
不一定是变量。我这样做是为了解释他们做了什么,所以它更有意义。我更喜欢这种方法,因为你不担心UI线程的东西,除了更新TextView。
编辑:您还可以使用TextView
中的post方法从计时器访问用户界面。它看起来像这样:
sum.post(new Runnable() {
public void run() {
sum.setText("Sum:"+sum1);
});
}
<强> EDIT2:强>
我必须将此编辑包含在对未来遇到此问题的用户中,因为我发现Timer
和TimerTask
不再是调度命令等的首选方法。现在应该是ScheduledThreadPoolExecutor
。虽然已经有一段时间了,但我在这里遇到的问题并没有真正报道过。在任何一种情况下,android都在文档here中有它。它实际上就像第二句话。
答案 1 :(得分:0)
问题是onClick()
在UI线程上运行。这意味着整个方法必须在UI再次更新之前完成,因此突然跳转到最后一个数字。这也是使用Thread.Sleep()
时没有任何反应的原因。 sleep()
适用于UI线程,因此整个应用程序将冻结。
如果您希望TextView计数,则必须使用AsyncTask。将循环和Thread.sleep()
添加到doInBackground()
,发布进度并在onProgressUpdate()
中设置文本。该链接实际上包含一个适合您的好例子。
答案 2 :(得分:0)
这是我提出的解决方案:D
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/resultView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start counting" />
</LinearLayout>
<强> MainActivity.java 强>
public class MainActivity extends Activity implements OnClickListener {
private static final long SUM_DELAY = 100;
private TextView mResultView;
private Button mStartButton;
private Handler mHandler = new Handler();
private int mSum = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResultView = (TextView) findViewById(R.id.resultView);
mStartButton = (Button) findViewById(R.id.startButton);
mStartButton.setOnClickListener(this);
}
private Runnable sumTask = new Runnable() {
@Override
public void run() {
mSum += 1;
if (mSum > 120) {
mSum = 0;
mStartButton.setEnabled(true);
return;
}
mResultView.setText(String.valueOf(mSum));
mHandler.postDelayed(this, SUM_DELAY);
}
};
@Override
public void onClick(View view) {
if (view.getId() == mStartButton.getId()) {
mStartButton.setEnabled(false);
sumTask.run();
}
}
}