【问题标题】:Using AsyncTask to show a Circular ProgressBar使用 AsyncTask 显示一个循环进度条
【发布时间】:2013-06-27 09:07:27
【问题描述】:

我是初学者 Android 程序员。我正在编写一个小型计算器,其中输入了两个否,并在按下总计时添加了两者。按下 Total 后,必须有一个中间进度条(圆形),持续 3 秒,然后出现总值,我已经完成了计算器和进度条,但不知道如何将它们放在 asynctask 中,计算代码在这里。请指导我如何做到这一点

  <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:visibility="invisible"/>
     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        ></TextView>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/editText2"
        android:text="Clear"
        android:onClick="Clicked" 
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="35dp"
        android:text="Total"
        android:onClick="Clicked"
         />

</RelativeLayout>

此处执行总计

public void   Clicked(View v)
{    int  total; 
     
     
     if(v.getId()==R.id.button1)
     {     
         int v1 = Integer.parseInt(t1.getText().toString());
         int v2 = Integer.parseInt(t2.getText().toString());
         total = v1 + v2;
         loadprogressbar();
         tv.setText(total+"");
         tv.setVisibility(1);
     }
     else if (v.getId()==R.id.button2)
     {
         t1.setText("");
         t2.setText("");
     }
}

我也有 ProgressBar 的代码,但真的不知道如何将这两个代码与 asyncTask 一起使用来先加载 Progressbar,然后再加载 toatl 值

【问题讨论】:

    标签: java android multithreading android-asynctask android-progressbar


    【解决方案1】:

    基本上你需要做以下事情:

    1. 显示进度条。
    2. 请稍等。
    3. 显示结果。

    第二项应该在主线程之外的其他线程中完成,否则 UI 将挂起。但是,其他任务必须在主线程上完成,因为它们操纵 UI。对于这种简单的情况,您实际上不需要使用 AsyncTask,您应该使用 Handler 代替:

    //Display the progress bar here
    
    //Create a handler and tell it to run a task 3 seconds later
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //Hide the progress bar and show the results
        }
    }, 3 * 1000); //3 second delay is specified here
    

    【讨论】:

      【解决方案2】:

      将 ProgressBar 添加到您的布局中,但将其可见性设置为“gone”(android:visibility="gone") 就在您调用 AsyncTask.execute() 之前,将 ProgressBar 设置为可见 (View.setVisibility(View.VISIBLE)。然后在 onPostExecute() 中,再次将其设置为“gone” (View.setVisibility(View.GONE).

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-11
        • 1970-01-01
        • 2014-10-23
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        相关资源
        最近更新 更多