2008年大学生就业形势
2008-04-23 15:47
295 查看
说到ProgressDialog和ProgressBar,相信大家不陌生吧,就是比如导入联系人的时候会出现一个进度条的控件,或者下载的时候的进度条。
首先说一下ProgressBar:
既然要更新UI,在android中,一般是不在主线程也就是Activity所在的那个线程来做些费时的操作的,因为这样会阻塞主线程,导致出现ANR错误。因此,一般是新开一个线程来做费时的操作,当然,做操作的时候又要通知前端的主线程来更新界面,这样能让用户知道在做操作啊,因此,就需要使用android中的线程通信Handler了。好了。下面是示例:
1,写好一个带progressbar的xml布局文件:
2,Activity代码:
当然,这里涉及两种progressbar,一种是进度条的,一种是环形的,另外这里的线程停止方式,还可以有另外的方式,就是设置一个boolean变量来控制线程的停止。
再说一下ProgressDialog的用法;示例代码:
1,也是先看布局:
2,再看Activity代码:
很简单吧,主要使用的android中线程之间的通信机制Handler。
首先说一下ProgressBar:
既然要更新UI,在android中,一般是不在主线程也就是Activity所在的那个线程来做些费时的操作的,因为这样会阻塞主线程,导致出现ANR错误。因此,一般是新开一个线程来做费时的操作,当然,做操作的时候又要通知前端的主线程来更新界面,这样能让用户知道在做操作啊,因此,就需要使用android中的线程通信Handler了。好了。下面是示例:
1,写好一个带progressbar的xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/rectangleProgressBar" style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <ProgressBar android:id="@+id/circleProgressBar" style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <Button android:id="@+id/button" android:text="Show ProgressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
2,Activity代码:
public class ProgressBarDemo extends Activity { private ProgressBar rectangleProgressBar,circleProgressBar; private Button mButton; protected static final int STOP = 0x10000; protected static final int NEXT = 0x10001; private int iCount = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //findView By id rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar); circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar); mButton = (Button)findViewById(R.id.button); rectangleProgressBar.setIndeterminate(false); circleProgressBar.setIndeterminate(false); mButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { rectangleProgressBar.setVisibility(View.VISIBLE); circleProgressBar.setVisibility(View.VISIBLE); rectangleProgressBar.setMax(100); rectangleProgressBar.setProgress(0); circleProgressBar.setProgress(0); //创建一个线程,每秒步长为5增加,到100%时停止 Thread mThread = new Thread(new Runnable() { public void run() { for(int i=0 ; i < 20; i++){ try{ iCount = (i + 1) * 5; Thread.sleep(1000); if(i == 19){ Message msg = new Message(); msg.what = STOP; mHandler.sendMessage(msg); break; }else{ Message msg = new Message(); msg.what = NEXT; mHandler.sendMessage(msg); } }catch (Exception e) { e.printStackTrace(); } } } }); mThread.start(); } }); } //定义一个Handler private Handler mHandler = new Handler(){ public void handleMessage(Message msg){ switch (msg.what) { case STOP: rectangleProgressBar.setVisibility(View.GONE); circleProgressBar.setVisibility(View.GONE); Thread.currentThread().interrupt(); break; case NEXT: if(!Thread.currentThread().isInterrupted()){ rectangleProgressBar.setProgress(iCount); circleProgressBar.setProgress(iCount); } break; } } }; }
当然,这里涉及两种progressbar,一种是进度条的,一种是环形的,另外这里的线程停止方式,还可以有另外的方式,就是设置一个boolean变量来控制线程的停止。
再说一下ProgressDialog的用法;示例代码:
1,也是先看布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/beginBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="begin" /> </LinearLayout>
2,再看Activity代码:
public class ProgressBarDemo extends Activity { private TextView statusTextView; private Button beginBtn; private ProgressDialog progressDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); statusTextView = (TextView)findViewById(R.id.status); beginBtn = (Button)findViewById(R.id.beginBtn); setListener(); } /** * 用Handler来更新UI */ private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //关闭ProgressDialog progressDialog.dismiss(); //更新UI statusTextView.setText("Completed!"); }}; /** * 点击按钮事件listener */ private void setListener(){ beginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //显示ProgressDialog progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false); //新建线程 new Thread(){ @Override public void run() { //需要花时间计算的方法 Calculation.calculate(4); //向handler发消息 handler.sendEmptyMessage(0); }}.start(); } }); } class Calculation { public static void calculate(int sleepSeconds){ try { Thread.sleep(sleepSeconds * 1000); } catch (Exception e) { // TODO: handle exception } } } }
很简单吧,主要使用的android中线程之间的通信机制Handler。
相关文章推荐
- 大学生就业形势
- 计算机专业大学生供需现状和就业形势
- 大学生就业形势
- 我眼中2013大学生就业形势
- 大学生就业形势最严峻一年背后的巧合
- 把脉2010年大学生的就业形势之产业转型
- 友情发布:大学生就业心理健康调查
- 面对严峻的就业形势,如何提升自身就业能力,实现顺利就业
- 大学生就业自荐信
- NoSQL就业形势分析:Cassandra和MongoDB最受欢迎
- 大学生对于就业的幻想
- 对计算机大学生就业的一些思考
- 大学生,就业or择业?
- 计算机专业大学生就业启示录(注意时间因素)
- 独家评论:就业还是创业 大学生首先应学会敬业
- 人生就业-大学生在干些什么
- 07大学生就业指南:技术类人才吃香
- 影响大学生就业的几个关键问题
- 影响大学生就业的几个关键问题
- 大学生面对就业,你准备好了吗?