当前位置: 首页 > 知识库问答 >
问题:

简单的总和程序Android java

唐经国
2023-03-14

无错误,无编译,无运行时错误。嘿,我有一个关于Android sum的简单问题。我在做一个简单的求和,代码编译,它在技术上做它应该做的,求和直到它大于120。然而,我希望输出在循环中显示每个和。不幸的是,它只是立即跳到textview上的最后一个总和。我尝试使用一个睡眠函数,wait函数,但结果是它会等待所有循环的时间量,直到总和大于120,然后输出最终的总和。在本例中为4950。在文本视图中显示每个总和有什么建议吗?

我还尝试使用这行代码,它可以单独显示每个和,但是它会立即启动计时器。它不应该这样做。干杯makeText(MainActivity.this,“SUM:”sum1,Toast.LENGTH\u 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();
                            }
                    }

    }

谢谢你的时间。

共有3个答案

常业
2023-03-14
匿名用户

问题是,onClick()在UI线程上运行。这意味着整个方法必须在UI再次更新之前完成,因此突然跳到最后一个数字。这也是为什么使用线程时不会发生任何事情的原因。Sleep()<代码>睡眠()在这种情况下应用于UI线程,因此整个应用程序将冻结。

如果希望TextView计数,则必须使用AsyncTask。添加循环和线程。sleep()到doInBackground(),发布进度并在onProgressUpdate()中设置文本。这个链接实际上包含了一个很好的例子,应该对您有用。

童华池
2023-03-14

这是我建议的解决方案: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();
        }
    }

}
戚阳文
2023-03-14

它“跳转”到最后一个的原因是,单击操作会在阻塞UI线程的同时运行for循环。实际上,您无法看到文本的更改,但它正在更改。您可以通过使用计时器或TimerTask来完成所需的任务,这基本上为您创建了一个单独的线程。它还可以处理您需要的延迟。只需在计时器执行方法中进行数学运算。

//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);

请记住,我设置ti的方式只是一个例子。取消时,您应该确保您有权访问leTask(或任何您称之为leTask的内容)<代码>i和sum1也应该是全局的delay和everySecond不必是变量。我这样做是为了解释他们是怎么做的,这样就更有意义了。我更喜欢这种方法,因为除了更新TextView之外,您不必担心UI线程的问题。

编辑:您还可以使用来自文本视图的post方法从计时器访问UI。它看起来像这样:

sum.post(new Runnable() {
    public void run() {
        sum.setText("Sum:"+sum1);   
    });
}

编辑2:

我必须包含此编辑以供未来遇到此问题的用户彻底使用,因为我刚刚发现TimerTimerTask不再成为调度命令等的首选方法。现在应该是schduledThreadPoolExecitor。虽然这种情况已经存在一段时间了,但我遇到的问题中并没有太多报道。在任何一种情况下,android都在这里的文档中拥有它。它字面上就像第二句话。

 类似资料:
  • C++使用非程序员可能感到奇怪的符号。我们首先介绍一个简单程序:打印一行文本。程序及其屏输出如图1.2。 这段程序演示了C++语言的几个重要特性。我们详细介绍程序的每一行。 // Fig.1.2:fig1_02.cpp // A first program in C++ 以//开头,表示该选项其余部分是注释语句(comment)。程序员手稿注释语句用来说明和提高程序的可读性。注释语句还可以帮助其

  • 我正试图解决这个问题:

  • 编译简单的 C 程序 C 语言经典的入门例子是 Hello World,下面是一示例代码: #include <stdio.h> int main(void) { printf("Hello, world!\n"); return 0; } 我们假定该代码存为文件‘hello.c’。要用 gcc 编译该文件,使用下面的命令: $ gcc -Wall hello.c -o hell

  • 本文向大家介绍SpringMVC程序简单实例,包括了SpringMVC程序简单实例的使用技巧和注意事项,需要的朋友参考一下 StringMVC程序简单实例 第一步:导入jar包 第二步,在WEB-INF文件夹下创建spring-servlet.xml文件。  第三步:在web.xml文件配置springmvc。  第四步:创建一个控制器。    感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • You are the light of the world. A city located on a hill cannot be hidden. People do not light a lamp and put it under a basket but on a lampstand, and it gives light to all in the house. In te same w

  • 本文向大家介绍Freemarker 最简单的例子程序,包括了Freemarker 最简单的例子程序的使用技巧和注意事项,需要的朋友参考一下 Freemarker 最简单的例子程序   freemarker-2.3.18.tar.gz http://cdnetworks-kr-1.dl.sourceforge.net/project/freemarker/freemarker/2.3.18/free