如何在Android中使用ProgressBar

huangapple go评论82阅读模式
英文:

How to use ProgressBar in Android

问题

我已经创建了一个应用程序,其中一个服务在后台运行。当有进度时,它会显示一个短暂的消息,但我想要添加一个进度条。有人可以帮我吗?

这是将显示短暂消息的方法:(已下载25%,50%等等)

protected void onProgressUpdate(Integer... progress) {
    Log.d("Downloading files",
            String.valueOf(progress[0]) + "% downloaded");
    Toast.makeText(getBaseContext(),
            String.valueOf(progress[0]) + "% downloaded",
            Toast.LENGTH_LONG).show();
}

在activity_main.xml布局中的进度条布局如下:

<ProgressBar
    android:id="@+id/p_barr"
    android:layout_width="150dp"
    android:layout_height="150dp"
    style="?android:progressBarStyleHorizontal"
    android:progress="5"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />
英文:

I have created an app where a service runs in the background. When there is progress it will display a toast message, but I want to add a progress bar. Can someone helo me with this?

this is the method which will display the toast message: ( 25% downloaded, 50% and so on)

 protected void onProgressUpdate(Integer... progress) {
            Log.d(&quot;Downloading files&quot;,
                    String.valueOf(progress[0]) + &quot;% downloaded&quot;);
            Toast.makeText(getBaseContext(),
                    String.valueOf(progress[0]) + &quot;% downloaded&quot;,
                    Toast.LENGTH_LONG).show();
        }

layout of progress bar in activity_main.xml

&lt;ProgressBar
        android:id=&quot;@+id/p_barr&quot;
        android:layout_width=&quot;150dp&quot;
        android:layout_height=&quot;150dp&quot;
        style=&quot;?android:progressBarStyleHorizontal&quot;
        android:progress=&quot;5&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
        app:layout_constraintRight_toRightOf=&quot;parent&quot;
        /&gt;

答案1

得分: 3

以下是您要的翻译内容:

在您的主活动中非常简单。

    ProgressBar bar; 
    // 在onCreate方法中
    bar = findViewById("您的进度条ID p_barr");
    bar.setProgress(整数值);
英文:

it's very simple in your main activity.do

ProgressBar bar; 
// in oncreate method
bar=findviewbyid(&quot;your progressbar id p_barr &quot;);
bar.setProgress(int);

huangapple
  • 本文由 发表于 2020年5月5日 03:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61600245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定