Handler是android给我们提供用来更新UI的一套机制,也是一套处理消息的机制,我们可以发送消息,也可以通过它处理消息。
实例1:
在界面中显示textView

activity_main.xml
<LinearLayout 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"
tools:context="${relativePackage}.${activityClass}" >


<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>




MainActivity.java
package com.example.handler01;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Images;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {


private TextView textView;
private Handler handler = new Handler();
private int Images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textview);
new Thread() {
public void run() {
try {
Thread.sleep(1000);
handler.post(new Runnable() {

@Override
public void run() {
textView.setText("update thread");
}
});

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
}
解析:通过新建一个线程在run方法中调用handler的post方法,传进一个Runnable对象,重写run方法,在方法里面更新UI。
实例二、
实现图片的循环播放:

activity_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"
tools:context="${relativePackage}.${activityClass}" >


<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="135dp"
android:src="@drawable/ic_launcher" />


</RelativeLayout>


MainActivity.java
package com.example.handler01;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Images;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {




private Handler handler = new Handler();
private ImageView imageView;
private int Images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
private int index;
private MyRunnable myRunnable = new MyRunnable();
class MyRunnable implements Runnable
{


@Override
public void run() {
index++;
index = index % 3;
imageView.setImageResource(Images[index]);
handler.postDelayed(myRunnable, 1000);
}

}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
handler.postDelayed(myRunnable, 1000);
}

}

真机运行效果图如下(三张图片循环跳转):

[置顶] [Android基础]Android中Handler的用法_第1张图片








更多相关文章

  1. Android实现网络图片查看器和网页源码查看器
  2. Android 九宫格图片展示的实现
  3. 在Android中使用GIF图片
  4. Android Studio的Gradle错误解决方法
  5. Android 主动获取电量的方法
  6. Android SDK 安装过程 与 安装失败的处理方法

随机推荐

  1. Android(安卓)实用工具Hierarchy Viewer
  2. Android(安卓)属性动画(一):Animator属性动
  3. Google Maps 工程的小问题
  4. Android有用代码片段(二)
  5. Android(安卓)LayoutInflater原理分析,带
  6. Android(安卓)Animation学习笔记
  7. Android自定义Button按钮显示样式
  8. Android程序开发0基础教程(一)
  9. android的selector,背景选择器
  10. Shape Drawable的学习