首页 技术 正文
技术 2022年11月7日
0 收藏 920 点赞 294 浏览 2359 个字

MainActivity例如以下:

package cc.testui3;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
/**
* Demo描写叙述:
* 在子线程中更改UI的方式三
*
* 调用Activity.runOnUiThread(Runnable runnable)方法.
*
*该方法的源代码例如以下:
*
* Runs the specified action on the UI thread. If the current thread is the UI
* thread, then the action is executed immediately. If the current thread is
* not the UI thread, the action is posted to the event queue of the UI thread.
*
* @param action the action to run on the UI thread
* public final void runOnUiThread(Runnable action) {
* if (Thread.currentThread() != mUiThread) {
* mHandler.post(action);
* } else {
* action.run();
* }
* }
*
*
* 源代码中的这段凝视太具体和贴心了,赞一个!
* 在UI线程中运行该Runnable.
* 假设当前线程是UI线程,那么该Runnable会马上运行.
* 假设当前的线程不是UI线程则调用UI线程handler的post()方法将其放入UI线程的消息队列中.
* 注意:勿在runOnUiThread(Runnable runnable)中做耗时操作
*
* 參考资料:
* http://blog.csdn.net/guolin_blog/article/details/9991569
* Thank you very much
*/
public class MainActivity extends Activity {
private TextView mTextView;
private Activity mActivity;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mActivity=this;
mTextView=(TextView) findViewById(R.id.textView);
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListenerImpl());
}private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
new Thread(){
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText("My name is zxc , number is 007");
}
});
};
}.start();
}
}}

main.xml例如以下:

<RelativeLayout 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"
> <TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
/> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dip"
/> <TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測试在子线程中更新UI"
android:layout_centerInParent="true"
/></RelativeLayout>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,903
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,429
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,245
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,057
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,689
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,726