AsyncTash基础创建规则与例子
来源:互联网 发布:穿越时光的地铁知乎 编辑:程序博客网 时间:2024/06/04 21:52
《AsyncTask子类.class文件》import android.os.AsyncTask;import android.util.Log;import android.widget.Button;import android.widget.EditText;/** * AsyncTask使用方法 * 1--自定义AsyncTask * 继承 AsyncTask<Params, Progress, Result> * Params:调用类中执行execute()方法时传入的参数。 * 如果不需要传递参数,则为Void。 * Progress:子线程执行的进度。 * Result: 和doInBackground()方法的返回值类型保持一致。 * 2--调用自定义的AsyncTask * 在调用类中生成一个自定义AsyncTask对象(通常实例化自定义AsyncTask的子类), * 然后执行:对象.excute(Params... params); */public class PrintDynamic extends AsyncTask<Void,Integer,Void> { EditText ed; Button bt; //构造函数,实例化后可以调用该类的函数方法并执行 public PrintDynamic(EditText editText,Button button){ this.ed=editText; this.bt=button; } private static final String TAG="Async"; @Override protected Void doInBackground(Void... voids) { //该方法中是后台执行的内容,比如睡眠,下载,播放歌曲等 for(int i=0;i<50;i++){ try { Thread.sleep(1000);//线程睡眠需要抓取错误 } catch (InterruptedException e) { e.printStackTrace(); } publishProgress(i);//i就是能够传递给onProgressUpdate的参数values } Log.d(TAG, "doInBackground: "); return null; } @Override protected void onProgressUpdate(Integer... values) {//values打印线程中的参数数组,线程有变化就执行 if(ed!=null){ ed.setText(values[0].toString()); } Log.d(TAG, "onProgressUpdate: "); } @Override protected void onPostExecute(Void aVoid) {//当onProgressUpdate后台执行的方法完成后执行一次 if(bt!=null){ bt.setText("完成"); } }}《主页面Activity》import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;import android.widget.EditText;public class MYActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); } @Override protected void onResume() { super.onResume(); Button button= (Button) findViewById(R.id.button); EditText editText= (EditText) findViewById(R.id.edname); new PrintDynamic(editText,button).execute();//实例化并执行 }}
《layout布局.xml文件》<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/name" android:layout_width="100dp" android:layout_height="50dp" android:textColor="@color/colorAccent" android:gravity="center" android:textSize="20dp" android:text="用户名" /> <EditText android:id="@+id/edname" android:layout_width="180dp" android:layout_height="50dp" android:hint="请输入用户名" android:text="huyue" android:layout_toRightOf="@id/name" android:textColor="@color/colorPrimary" /> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/password" android:layout_width="100dp" android:layout_height="50dp" android:gravity="center" android:textSize="19dp" android:textColor="@color/colorAccent" android:text="密码" /> <EditText android:layout_width="180dp" android:layout_height="50dp" android:hint="输入密码" android:layout_toRightOf="@id/password" /> </RelativeLayout> <Button android:id="@+id/button" android:layout_width="96dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="105dp" android:text="登录" /></LinearLayout>
阅读全文
0 0
- AsyncTash基础创建规则与例子
- 字节对齐的规则与例子分析
- RISC基础与80/20规则
- lucene索引创建与查询入门例子
- 读取文件与创建文件例子解说
- 文件的读写与创建简单例子
- Oracle 创建与删除job小例子
- 汇编程序的基础与一些例子
- 主键与索引的创建的规则以及区别
- 【JAVA陷阱与缺陷-->规则】(一)基础篇
- C++基础:继承与派生(声明 继承方式 访问规则)
- 【Linux基础】运算符重载的格式与规则
- C++基础:同名隐藏与赋值兼容规则
- 基础例子
- delphi 2009 创建OCX 与JS传参例子
- VS_2005_创建DLL工程,并进行调用与调试例子
- VC6.0 DLL调用与创建的例子(HOOK)
- Struts2校验规则创建
- 设计模式-创建型软件设计模式(三)
- Stereo match 基本原理介绍
- KMP字符串匹配
- Python 将模块作为脚本来执行
- 记一次失败的oracle安装过程
- AsyncTash基础创建规则与例子
- gcc和g++的区别
- BOZJ 2301: [HAOI2011]Problem b 莫比乌斯反演
- 51nod 1267 4个数和为0
- finally究竟什么时候执行
- Python数据结构--字典
- 上传代码到github
- rapidjson读写测试
- 调用微信接口方法