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