当前位置: 移动技术网 > 移动技术>移动开发>Android > AndroidStudio实现进度对话框显示(2020)

AndroidStudio实现进度对话框显示(2020)

2020年08月10日  | 移动技术网移动技术  | 我要评论
我们采用progressDialog来实现进度对话框的编写,显示效果如下:废话不多说,直接上代码把。首先布局xml代码:<LinearLayout android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical"

我们采用progressDialog来实现进度对话框的编写,显示效果如下:
原图废话不多说,直接上代码把。
首先布局xml代码:


<LinearLayout android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">
  <TextView
      android:id="@+id/textview"
      android:textSize="40sp"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  <Button
      android:id="@+id/button"
      android:text="刷新"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
</LinearLayout>`

我们定义了一个textview来辅助显示,定义一个button来启动进度条,接着我们来布局java代码:
在MainActivity中建立myTask类继承AsyncTask实现异步任务:

private class myTask extends AsyncTask<Integer,Integer,String> {
        Context context;
        ProgressDialog progressDialog;

        public myTask(Context context) {//获取传入的context,为新建progressDialog引入参数做准备
            this.context=context;
        }
       //后台执行任务
        @Override//传入进度值,并调用publishProgress传入状态更新方法中,实现进度值的前进
        protected String doInBackground(Integer... integers) {
            int i=integers[0];
            while(i<100)
            {
                i++;
                publishProgress(i);//传送进度值
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            return "已经更新完成!";

        }
         //任务更新
        @Override//调用setProgress时刻更新进度值
        protected void onProgressUpdate(Integer... values) {
            progressDialog.setProgress(values[0]);
        }
         //执行任务前
        @Override//设置进度对话框必要参数
        protected void onPreExecute() {
            progressDialog=new ProgressDialog(context);
            progressDialog.setIcon(R.mipmap.ic_launcher_round);
            progressDialog.setTitle("提示");
            progressDialog.setMessage("正在加载中");
            progressDialog.setMax(100);
            progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
            progressDialog.setProgress(60);
            progressDialog.show();
            textView.setText("正在进行中.....");
        }

        @Override//任务完成之后
        //设置textview显示更新完成(s是doInbackground完成后返回的字符串),最后关闭进度对话框显示
        protected void onPostExecute(String s) {
            textView.setText(s);
            progressDialog.dismiss();
        }
    }

关键代码已完成,接着我们设置单击事件来实现:

public class MainActivity extends AppCompatActivity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         textView=findViewById(R.id.textview);
         Button button=findViewById(R.id.button);
        final myTask task=new myTask(MainActivity.this);//实例化
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                task.execute(new Integer(0));//调用execute启动任务
            }
        });
    }

以上代码已经十分详细了,作者也是小白一个,有不懂的可以一起探讨探讨!

本文地址:https://blog.csdn.net/qq_45901004/article/details/107889293

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网