英文:
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("Downloading files",
String.valueOf(progress[0]) + "% downloaded");
Toast.makeText(getBaseContext(),
String.valueOf(progress[0]) + "% downloaded",
Toast.LENGTH_LONG).show();
}
layout of progress bar in 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"
/>
答案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("your progressbar id p_barr ");
bar.setProgress(int);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论