
这个控件是进度条
?用一个按钮模拟显示和隐藏

getVisibility ()的值来判断控件的显示或者隐藏?
(1)0? ? --------? ?VISIBLE? ???可见 (1)4? ? --------? ?INVISIBLE? ? 不可见但是占用布局空间 (1)8? ? --------? ?GONE? ? 不?可见也不占用布局空间

?效果如下

?Java中我们可调用下述方法
- getMax():返回这个进度条的范围的上限
- getProgress():返回进度
- getSecondaryProgress():返回次要进度
- incrementProgressBy(int diff):指定增加的进度
- isIndeterminate():指示进度条是否在不确定模式下
- setIndeterminate(boolean indeterminate):设置不确定模式下

每点击一下就+进度条值
?设置了
android:indeterminate="true"
就跟我们上面那个转圈圈一样了

主要代码
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击按钮显示隐藏进度条"
android:onClick="leoClick"
/>
<ProgressBar
android:id="@+id/pd2"
android:indeterminate="true"
android:layout_width="300dp"
android:max="100"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"/>
<Button
android:text="模拟下载"
android:onClick="load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private ProgressBar progressBar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.pb);
progressBar2=findViewById(R.id.pd2);
}
public void leoClick(View view){
if(progressBar.getVisibility()==View.GONE){
// 当前的进度条的隐藏的
progressBar.setVisibility(View.VISIBLE);
}else {
progressBar.setVisibility(View.GONE);
}
}
public void load(View view){
int progress = progressBar2.getProgress();
progress+=10;
progressBar2.setProgress(progress);
}
}
|