Android advanced UI component progressbar progress bar

Two days ago, I learned the basic controls of datepicker calendar, time picker time and chronrmeter timer. Today, I have a little understanding of progressbar, one of the advanced UI components of Android. The progress bar is basically a must for every App. It can not only real-time Show your network speed loading progress, and at the same time increase the user's appreciation, which greatly improves the friendly performance of the software, so let's take a brief look at it today!

I am talking about the code I want to show. The code I posted below is to implement a progress bar. I will randomly generate a natural number from 0-10. The current real-time progress is the accumulated sum. The sleep time is 200 milliseconds, such as If a value of 8 is generated in 200 milliseconds, the progress bar is 8%. If a value of 9 is generated in the next 200 milliseconds, the progress bar at this time reaches (8%+9%), which is 17%. When the total sum reaches 100, the progress bar is completed. The toast output is loaded and the loading is complete, enter the next interface!

A lot of comments have been made in the code segment. If there is anything you don’t understand, you can leave a message in the comment area or private message me. If there are any deficiencies and mistakes in the content, please correct me!

Obviously, the knowledge of handler threads is used to complete such a function. Let me just say it briefly. There will be an issue about the processing mechanism of handler threads in the future.

(Note that the amount I introduce below is defined by myself, do not understand that currentprogress must be used, etc.)

basic introduction:

A simple handler can be understood as the main thread, and a thread can be simply understood as a sub-thread. Android cannot complete time-consuming operations in the main thread, so it needs to complete time-consuming operations in the sub-thread, and then call the handler object of the main thread to send information to The main thread, the main thread updates the UI component information according to the received message! Among them, the currentprogress.what=0 used in the sub-thread and then the msg.what==0 in the main thread is actually a little bit similar to the form of startactivityforresult!

code show as below:

public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private Handler handler;
private int currentprogerss=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar=findViewById(R.id.progressbar);
 handler=new Handler(){
     @Override
     public void handleMessage(@NonNull Message msg) {
       if(msg.what==0){
         progressBar.setProgress(currentprogerss);
       }else {
           Toast.makeText(MainActivity.this,"加载完成",Toast.LENGTH_SHORT).show();
           progressBar.setVisibility(View.GONE);
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
       }
     }
 };
 new Thread(new Runnable() {
     @Override
     public void run() {
        while (true){
            currentprogerss=work();
            Message message=new Message();
            if(currentprogerss<100){
                message.what=0;
                handler.sendMessage(message);
            }else {
                message.what=1;
                handler.sendMessage(message);
                break;
            }
        }
     }
    //Simulate time-consuming operations
     private int work() {          currentprogerss+=Math.random()*10;          try {              Thread.sleep(200);          } catch (InterruptedException e) {              e.printStackTrace();          }          return currentprogerss;      }







 }).start();
    }

}
 

 

Guess you like

Origin blog.csdn.net/Abtxr/article/details/125836819