Android - how to hide / show progressbar onclick

前端 未结 3 774
粉色の甜心
粉色の甜心 2021-01-28 22:52

I am trying to show a progressbar when a button is clicked. When i test the app it force closes / stops. My app works fine before the progressbar code is added in.

also

3条回答
  •  梦毁少年i
    2021-01-28 23:16

    try this way, i am not sure it will help you or not

    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
                    ProgressTitle, ProgressMessage, true, false);
    
            new Thread(new Runnable() {
                public void run() {
                loadFeed();
                progress.cancel();
                }
            }).start();
        }
    });
    
    Be careful about what loadFeed(statutory); do. Because now is working inside a THREAD and Threads con not modify UI, If this is the case then you should do something like this:
    
    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
                    ProgressTitle, ProgressMessage, true, false);
    
            new Thread(new Runnable() {
                public void run() {
                loadFeed(); //just load data and prepare the model
                runOnUiThread(new Runnable() {
                                 @Override public void run() {
                                   //here you can modify UI here
                                   //modifiying UI element happen here and at the end you cancel the progress dialog
                                   progress.cancel();
                             }
                        }); // runOnUIthread
    
                }
            }).start();
        }
    });
    

提交回复
热议问题