android 反纠结app开发: 在线程中更新view
大体上想实现一个思路:
对一个view 的内容进行不停地变化, 通过按钮停止这种变化,以达到随机选择的目的.
开发过程中 使用textview 模拟, 建立线程
mythread = new Thread()
{
@Override
public void run()
{
while(isrun)
{
改变textview
}
}
}
结果遭遇报错 Only the original thread that created a view hierarchy can touch its views
通过在网上查阅大量资料, 得知android 的view 和相关控件不是线程安全的,不可以在线程中直接改变.
这种情况下 需要使用handle .
通过Handler更新UI实例:
步骤:
1、创建Handler对象(此处创建于主线程中便于更新UI)。
2、构建Runnable对象,在Runnable中更新界面。
3、在子线程的run方法中向UI线程post,runnable对象来更新UI。
在下面的代码中, 通过变量ab ,不断更新textview中的内容 ,通过按钮可以结束线程.
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock; import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { private Button button;
private TextView textview;
private final int SPLASH_DISPLAY_LENGHT = 1;
private static int flag = 0;
private static int count=0;
private int ab=1;
private boolean isrun = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button1);
textview = (TextView)findViewById(R.id.textView1);
final Handler handler= new Handler(); final Runnable runnable = new Runnable() {
public void run() {
textview.setText(Integer.toString(ab));
}
}; final Thread t = new Thread(){
//public boolean isrun=true;
@Override
public void run() {
while(isrun)
{
handler.post(runnable); //加入到消息队列 这样没有启动新的线程,虽然没有报异常。但仍然阻塞ProgressDialog的显示
ab++;
try {
sleep(1000); //直接调用
} catch (InterruptedException e) {
return;
}
}
}
};
t.start();
button.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View view)
{
isrun=false;
}
}); } }
android 反纠结app开发: 在线程中更新view的更多相关文章
- Android开之在非UI线程中更新UI
当在非UI线程中更新UI(程序界面)时会出现例如以下图所看到的的异常: 那怎样才干在非UI线程中更细UI呢? 方法有非常多种.在这里主要介绍三种: 第一种:调用主线程mHandler的post(Run ...
- Android中不能在子线程中更新View视图的原因
这是一条规律,很多coder知道,但原因是什么呢? 如下: When a process is created for your application, its main thread is ded ...
- Android开发UI之在子线程中更新UI
转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- Android在子线程中更新UI(二)
MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...
- Android在子线程中更新UI(一)
MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...
- Android多线程之(一)View.post()源码分析——在子线程中更新UI
提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...
- 使用Handler在子线程中更新UI
Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...
- C# WINFORM 线程中更新UI
幸好今天是周末,有时间把这个问题记录一下.在多种语言之间切换,发现开发效率降的很低了,开发成本都集中到调式上了,C/C++这些放弃很久了,突然感觉线程这个问题搞的有点烦躁 我这里提到的线程中更新UI, ...
随机推荐
- Android中Action
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. <activity androi ...
- ASP.NET 5服务
ASP.NET5已经把web服务从应用程序当中解耦出来了,它支持IIS和IIS Express, 用Kestrel和WebListener自宿主,另外,开发都或者第三方软件提供商都可以自定义开发ASP ...
- 认识CSS样式
CSS全称为“层叠样式表 (Cascading Style Sheets)”,它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等. 如下列代码: p{ font-size: ...
- Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view...
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tabl ...
- asp.net 图片质量压缩(不改变尺寸)
private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; en ...
- js性能优化--学习笔记
<高性能网站建设进阶指南>: 1.使用局部变量,避免深入作用域查找,局部变量是读写速度最快的:把函数中使用次数超过一次的对象属性和数组存储为局部变量是一个好方法:比如for循环中的.len ...
- phpcms v9自定义表单提交后返回上一页实现方法
PHPcms v9中提交自定义表单后默认都是回到首页的,是不是感觉很不爽! 接下来,就说下phpcms v9自定义表单提交后返回上一页实现方法. 1.找到这个文件 phpcms\modules\for ...
- 安装mysql-python报错
运行: pip install mysql-python报错如下: Downloading/unpacking MYSQL-python Downloading MySQL-python-1.2.5. ...
- [Struts2学习笔记] -- 自定义类型转换
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- PHP之路——微信公众号授权获取用户信息
官方文档链接:http://mp.weixin.qq.com/wiki/4/9ac2e7b1f1d22e9e57260f6553822520.html /** * 获取code */ public f ...