0

我是初学anderoid程序员,我编写了一个小型计算器,其中输入了两个no,并按下总数两者都被添加。在按下Total时,必须有一个中间进度条(循环),持续3秒,然后出现总值,我已经完成了计算器和进度条,但不知道如何将它们放在asynctask中,calc代码在这里。请指导我该怎么办呢使用AsyncTask显示循环ProgressBar

<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> 

总在这里perfomed

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(""); 
    } 
} 

我有代码,进度很好,但真是越来越不知道如何这两个代码一起使用具有的AsyncTask先加载进度然后toatl值

回答

2

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

  1. 显示在p进度条。
  2. 等待一段时间。
  3. 显示结果。

第二项应该在除了main之外的其他线程中完成,否则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 
1

的进度添加到您的布局,而将其可见性“水涨船高”(机器人:知名度=” “) 就在您调用AsyncTask.execute()之前,将ProgressBar设置为可见(View.setVisibility(View.VISIBLE)。然后在onPostExecute()中,将其设置为”消失“ (View.setVisibility(View。 GONE)。