android实践项目八做一个下载读条
16lz
2022-05-25
用progressBar做一个下载读条
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.httpclientexample.MainActivity" > <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="start" android:text="start" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /><TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
代码
public class MainActivity extends Activity { private ProgressBar progress; private TextView text; private Button btn; int Processvalue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text1); progress = (ProgressBar) findViewById(R.id.progressBar1); progress.setVisibility(View.INVISIBLE); } public void start(View view) { uzi(); getView(); } private void getView() { // TODO Auto-generated method stub } private void uzi() { // TODO Auto-generated method stub // 第三个参数是结果类型 new AsyncTask<String, Integer, String>() { // 此方法必须重写,异步执行后台线程需要完成的任务 @Override protected String doInBackground(String... params) { for (int i = 0; i <= 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Processvalue = i; publishProgress(Processvalue); } return null; } // 执行后台耗时操作时前被调用,通常用来完成一些初始化操作 @Override protected void onPreExecute() { super.onPreExecute(); progress.setVisibility(View.VISIBLE); progress.setMax(100); progress.setProgress(0); } // 当doinBackground()完成过后,系统自动调用,并将方法的返 // 回值传给onpostexecute()可运行在主线程操作ui @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); progress.setVisibility(View.INVISIBLE); } // doinBackground()方法中调用publishProgress()方法更行任务的执行进度后,就会触发该方法 @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); int value = values[0]; text.setText("当前进度" + value + "%"); progress.setProgress(value); } @Override protected void onCancelled(String result) { // TODO Auto-generated method stub super.onCancelled(result); } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); } }.execute(); }}
用AsyncTask实现效果。。。。
更多相关文章
- Android之修改部分字体颜色
- Android复习(三)
- Android(安卓)Studio常见错误及解决方法汇总
- android 控件属性
- android 4.0.3 dhcpcd使用方法
- Android(安卓)网络下载图片
- Android实用代码
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用