安卓开发中,ProgressBar用来设置进度条
控制显示与隐藏进度条
activity_main.xml中设置了如下的进度条和按钮,想实现点击一下按钮进度条就出现,再点击一下就消失的效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:paddingTop="40dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="显示隐藏进度条"
android:onClick="pbClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java中代码如下
getVisibility获取当前控件的状态值,是否可见
setVisibility设置当前控件的状态值,是否可见
package com.example.luosang;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
pb = findViewById(R.id.pb);
}
public void pbClick(View view) {
//如果此时进度条为不可见,那么点击按钮时出现进度条
if(pb.getVisibility() == View.GONE){
pb.setVisibility(View.VISIBLE);
}else{
//反之,进度条则消失
pb.setVisibility(View.GONE);
}
}
}
未点击按钮之前:
点击按钮之后:
style:设置进度条样式
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
该样式为一条水平线
模拟下载进度条
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="30dp"
android:orientation="vertical">
<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<Button
android:text="模拟下载"
android:onClick="pbClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.example.firstprogramme;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private ProgressBar pb;
private EditText ET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
//获取进度条
pb = findViewById(R.id.pb);
}
public void pbClick(View view) {
//获取进度条进度值
int progress = pb.getProgress();
//每次点击时进度加10
progress += 10;
pb.setProgress(progress);
}
}
每点击一次按钮,进度条加10
max:设置进度条最大值
最大值为100
progress设置进度条当前进度值
最大值为100的进度条,我设置progress值为50
indeterminate循环播放进度条
ProgressBar-indeterminate