Android通过按钮动态实现操作进度条

Android通过按钮动态实现操作进度条

木来 木来

(1)圆形进度条

在活动的布局中添加一个ProgressBar控件和一个Button控件

<ProgressBar    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/progress_bar"    android:visibility="gone"    />

然后在活动中书写

package com.shihsf.hello;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends BasicActivity implements View.OnClickListener{


    private static final String TAG = "MainActivity";

    private ProgressBar progressBar;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

progressBar=(ProgressBar)findViewById(R.id.progress_bar);



        Button button1=findViewById(R.id.main_button);

        button1.setOnClickListener(this);


    }

    @Override    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_button:
                if(progressBar.getVisibility()== View.GONE){
                    progressBar.setVisibility(View.VISIBLE);
                }else{
                    progressBar.setVisibility(View.GONE);
                }
                break;




        }


    }
}

(2)长方形控制条

修改布局中的ProgressBar控件

<ProgressBar    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/progress_bar"    style="?android:attr/progressBarStyleHorizontal"    android:max="100"    />

修改活动代码

package com.shihsf.hello;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends BasicActivity implements View.OnClickListener{


    private static final String TAG = "MainActivity";

    private ProgressBar progressBar;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

progressBar=(ProgressBar)findViewById(R.id.progress_bar);



        Button button1=findViewById(R.id.main_button);

        button1.setOnClickListener(this);


    }

    @Override    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_button:
               int progress=progressBar.getProgress();
               progress+=10;
               progressBar.setProgress(progress);



                break;




        }


    }
}
0 条评论