📜  Kotlin中的ProgressBar

📅  最后修改于: 2021-05-10 16:34:53             🧑  作者: Mango

Android ProgressBar是一个用户界面控件,用于指示操作进度。例如,下载文件,在互联网上上传文件,我们可以看到进度条以估计剩余的运行时间。

progressBar有两种模式:-

  • 确定ProgressBar
  • 不确定的ProgressBar

确定ProgressBar
通常,我们在progressBar中使用“确定进度”模式,因为它显示进度的数量,例如已下载文件的百分比(%),在Internet上上传或下载的数据量等。

如果必须使用“确定”,则将进度条的样式设置如下:


不确定的ProgressBar
在这里,我们不了解工作进度意味着完成多少工作或需要多长时间。
我们可以通过将indeterminate属性设置为true来使用不确定的progressBar,如下所示。


ProgressBar小部件的不同属性–

XML Attributes Description
android:id Used to uniquely identify the control
android:min Used to set minimum value
android:max Used to set maximum value
android:progress Used to set the default progress integer value between 0 and max.
android:minHeight Used to set the height of progress bar.
android:minWidth Used to set the width of progress bar.
android:background Used to set the background color for progress bar
android:indeterminate Used to enable indeterminate progress mode.
android:padding Used to set the padding for left, right, top or bottom of progress bar.

在activity_main.xml文件中添加ProgressBar窗口小部件



  
    
  
    
  
    

访问MainActivity.kt文件中的ProgressBar窗口小部件

package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import android.os.Handler
  
  
class MainActivity : AppCompatActivity() {
  
    private var progressBar: ProgressBar? = null
    private var i = 0
    private var txtView: TextView? = null
    private val handler = Handler()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        progressBar = findViewById(R.id.progress_Bar) as ProgressBar
        txtView = findViewById(R.id.text_view) as TextView
        val btn = findViewById(R.id.show_button) as Button
        btn.setOnClickListener {
            i = progressBar!!.progress
            Thread(Runnable {
                while (i < 100) {
                    i += 5
                    // Update the progress bar and display the current value
                    handler.post(Runnable {
                        progressBar!!.progress = i
                        txtView!!.text = i.toString() + "/" + progressBar!!.max
                    })
                    try {
                        Thread.sleep(100)
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
  
                }
            }).start()
        }
    }
  
}

AndroidManifest.xml文件



  
    
        
            
                
  
                
            
        
    
  

作为仿真器运行:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!