Android学习(二)--第二个项目遇到的问题
2016-04-10 15:33
387 查看
模拟后台程序运行进度提示
主要方法是用一个线程在后台发送数据,然后使用Handler处理数据。每个线程都有且只有一个Handler,Looper。主线程默认有Looper了。Handler一定要重写handleMessage方法。在该方法中处理数据。
Handler h=new Handler(){ public void handleMessage(Message m){ Bundle b=m.getData(); int progress=b.getInt("progress"); progressBar.setProgress(progress); textView.setText(String.valueOf(progressBar.getProgress())); } };
增加一个线程后台发送数据,注意最后要调用start()来调用run(),sendMessage需要参数Message,Message可以为空,setData参数为Bundle
@Override public void onStart(){ super.onStart(); Thread t=new Thread(new Runnable(){ @Override public void run() { for(int i=0;i<20;i++){ try { Thread.sleep(1000); Message m=new Message(); Bundle b=new Bundle(); b.putInt("progress", i*5); m.setData(b); h.sendMessage(m); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // TODO Auto-generated method stub } }); t.start(); }
完整代码
package com.example.homeworkprogress;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private ProgressBar progressBar;
Handler h=new Handler(){ public void handleMessage(Message m){ Bundle b=m.getData(); int progress=b.getInt("progress"); progressBar.setProgress(progress); textView.setText(String.valueOf(progressBar.getProgress())); } };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(ProgressBar)findViewById(R.id.progressBar);
progressBar.setProgress(0);
textView=(TextView)findViewById(R.id.progressText);
}
@Override public void onStart(){ super.onStart(); Thread t=new Thread(new Runnable(){ @Override public void run() { for(int i=0;i<20;i++){ try { Thread.sleep(1000); Message m=new Message(); Bundle b=new Bundle(); b.putInt("progress", i*5); m.setData(b); h.sendMessage(m); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // TODO Auto-generated method stub } }); t.start(); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
遇到的问题:TextView的setText遇到找不到资源的异常。一开始setText用的是整形参数,后来改成字符串就好了。
相关文章推荐
- 【笔记】Android中Context相关理解
- 搭建Android开发环境及调用Genymotion插件
- android基础学习之广播
- Android Layout布局使用总结Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。 帧布局(FrameLayout):组件从
- Android存储的实现
- Android显示TextView文字的倒影效果
- Android中压缩图片的几种方法
- android实现漫天雪花&下雨效果
- Android使用SQLCipher对加密数据库文件解密
- Android 无线调试
- studyjams notes 1B--width and height
- Android 5.0 新控件的属性介绍
- Android Studio常用快捷键及设置
- Android笔记【0】--manifest
- android shape的使用
- Android学习之5.X过渡动画的实现
- Android入门——电话拨号器和四种点击事件
- Android开发环境的配置
- Android adb命令
- Android Timer,TimerTask简单的使用