当前位置: 编程技术>移动开发
本页文章导读:
▪Handler跟线程 Handler和线程
package com.andli.handlerandthread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class HandlerAndThreadActivity extends Activity {
private String tag = "andli".........
▪ Handler兑现2秒自动切图 Handler实现2秒自动切图
package com.lilin.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
/**
* 通过Handler实现2秒自动换图
*
.........
▪ 等候动画的实现 等待动画的实现
头文件:
#import <UIKit/UIKit.h>
@interface AlertViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webView;
UIAlertView *alert;
}
@property (nonatomic, retain) IBOutlet UIWebView *webVie.........
[1]Handler跟线程
来源: 互联网 发布时间: 2014-02-18
Handler和线程
package com.andli.handlerandthread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class HandlerAndThreadActivity extends Activity {
private String tag = "andli";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i(tag, "---------------Main------------------");
Log.i(tag, "Thread_ID=" + Thread.currentThread().getId());
Log.i(tag, "Thread_Name=" + Thread.currentThread().getName());
Handler handler = new Handler();
Log.i(tag, "--------------Handler_Thread----------");
handler.post(runnable);
Log.i(tag, "---------------Thread------------------");
Thread thread = new Thread(runnable);
thread.start();
}
Runnable runnable = new Runnable() {
public void run() {
Log.i(tag, "Thread_ID=" + Thread.currentThread().getId());
Log.i(tag, "Thread_Name=" + Thread.currentThread().getName());
}
};
}
[2] Handler兑现2秒自动切图
来源: 互联网 发布时间: 2014-02-18
Handler实现2秒自动切图
package com.lilin.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
/**
* 通过Handler实现2秒自动换图
*
* @author lilin
* @date 2011-8-10 上午08:51:27
* @ClassName: Main
* @Description: TODO
*/
public class Main extends Activity {
ImageView myImageView;// ImageView的引用
public void onCreate(Bundle savedInstanceState) {// 重写的onCreate方法
super.onCreate(savedInstanceState);
setTitle("handler通信:每2秒自动换图");
setContentView(R.layout.main);// 设置当前的用户界面
System.out
.println("当前activity的ID--->" + Thread.currentThread().getId());
System.out.println("当前activity的名称--->"
+ Thread.currentThread().getName());
myImageView = (ImageView) findViewById(R.id.myImageView);
new Thread() {
public void run() {
int i = 0;
while (true) {// 循环
myHandler.sendEmptyMessage((i++) % 4);// 发送消息
System.out.println("handler的ID--->"
+ Thread.currentThread().getId());
System.out.println("handler的名称--->"
+ Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
};
}.start();
}
Handler myHandler = new Handler() {// 创建一个Handler对象
public void handleMessage(Message msg) {// 重写接收消息的方法
switch (msg.what) {// 判断what的值
case 0:// what值为0时
myImageView.setImageResource(R.drawable.bbta);
break;
case 1:// what值为1时
myImageView.setImageResource(R.drawable.bbtb);
break;
case 2:// what值为2时
myImageView.setImageResource(R.drawable.bbtc);
break;
case 3:// what值为3时
myImageView.setImageResource(R.drawable.bbtd);
break;
}
super.handleMessage(msg);
}
};
}
[3] 等候动画的实现
来源: 互联网 发布时间: 2014-02-18
等待动画的实现
头文件:
#import <UIKit/UIKit.h>
@interface AlertViewController : UIViewController <UIWebViewDelegate> {
UIWebView *webView;
UIAlertView *alert;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) UIAlertView *alert;
- (void) NavURL;
@end
实现文件:
#import "AlertViewController.h"
@implementation AlertViewController
@synthesize webView, alert;
-(void) NavURL{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://2015.iteye.com"]]];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
if (alert == nil){
alert = [[UIAlertView alloc] initWithTitle: nil
message: @"正在读取网络数据"
delegate: self
cancelButtonTitle: nil
otherButtonTitles: nil];
}
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityView.frame = CGRectMake(120.f, 48.0f, 37.0f, 37.0f);
[alert addSubview:activityView];
[activityView startAnimating];
[activityView release];
[alert show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self NavURL];
}
- (void)dealloc {
[super dealloc];
[webView release];
[alert release];
webView = nil;
alert = nil;
}
@end
示例图:
最新技术文章: