gpt4 book ai didi

java - 使用 AsyncTask 显示圆形进度条

转载 作者:行者123 更新时间:2023-12-01 23:44:22 25 4
gpt4 key购买 nike

我是初学者 Android 程序员。我正在编写一个小型计算器,其中输入两个数字,然后按总计将两个数字相加。按“总计”时,必须有一个中间进度条(圆形)仅持续 3 秒,然后出现总计值,我已经完成了计算器和进度条,但不知道如何将它们放在异步任务中,计算代码在这里。请指导我该怎么做

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

最佳答案

基本上您需要执行以下操作:

  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

关于java - 使用 AsyncTask 显示圆形进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com