android:layout_gravity="Center" 设置控件居中
android:gravity="Center" 设置文本居中
简单的在Activity间传数据,我们一般有两种方式:
1.直接用Intent的putExtra(), getStringExtra();
2.先new一个Bundle对象,用Bundle的putExtra().
那么这两种方式有什么不一样呢?
我们先看一下Intent对象相应函数的源代码:
public Intent putExtra(String name, String value) { if (mExtras == null) { mExtras = new Bundle(); } mExtras.putString(name, value); return this; }
public String getStringExtra(String name) { return mExtras == null ? null : mExtras.getString(name); }
可以知道,Intent的相关操作的实现是基于Bundle的,Bundle操作数据相对于Intent有哪些优势呢?
举个例子,如果我要从A界面跳转到B界面和C界面,那么我要写写两个Intent,如果传的数据相同,我两个Intent都要添加,但是如果我用Bundle,直接一次性包装就可以了。
再有,如果A界面要传值给B界面,再从B界面传值到C界面,你可以想象用Intent是多么的麻烦了,但是用Bundle就很简洁,而且还可以在B界面中添加新的信息。
其实在Android代码中这样的情况蛮多的,比如Intent的setClass和ComponentName这两种方式又有什么区别呢?请看setClass源代码:
public Intent(Context packageContext, Class<?> cls) { mComponent = new ComponentName(packageContext, cls); }
public Intent setClass(Context packageContext, Class<?> cls) { mComponent = new ComponentName(packageContext, cls); return this; }
当然如果传的数据非常之多而且很复杂,用这两种方式显然是不适合的,这时候我们可以使用可序列化的结构类,实例代码如下:
Parcelable类。最主要的类,也就是我们要传送的对象的类,需要实现Parcelable接口。
package com.zeph.android.Parcelable; import android.os.Parcel; import android.os.Parcelable; public class BenParcelable implements Parcelable { public String name; public int age; public String profession; public BenParcelable(String name, int age, String profession) { this.name = name; this.age = age; this.profession = profession; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getprofession() { return profession; } public void setprofession(String profession) { this.profession = profession; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel parcel, int flag) { parcel.writeString(name); parcel.writeInt(age); parcel.writeString(profession); } public static final Creator<BenParcelable> CREATOR = new Creator<BenParcelable>() { public BenParcelable createFromParcel(Parcel in) { return new BenParcelable(in); } public BenParcelable[] newArray(int size) { return new BenParcelable[size]; } }; private BenParcelable(Parcel in) { name = in.readString(); age = in.readInt(); profession = in.readString(); } }
ParcelableActivity类,传递对象的Activity类。
package com.zeph.android.Parcelable; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ParcelableActivity extends Activity { private Button myButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton = (Button) findViewById(R.id.myButton); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { BenParcelable benParcelable = new BenParcelable("BenZeph", 23, "Java/Android Engineer"); Intent intent = new Intent(); intent.setClass(getApplicationContext(), GetParcelableActivity.class); Bundle bundle = new Bundle(); bundle.putParcelable("Ben", benParcelable); intent.putExtras(bundle); startActivity(intent); } }); } }
GetParcelableActivity类,接收序列化对象的Activity类。
package com.zeph.android.Parcelable; import android.app.Activity; import android.os.Bundle; public class GetParcelableActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); BenParcelable parcelable = getIntent().getParcelableExtra("Ben"); System.out.println(parcelable.getName()); System.out.println(parcelable.getAge()); System.out.println(parcelable.getprofession()); } }
1.使用ZBar项目。下载地址是: http://zbar.sourceforge.net/iphone/index.html
2.新建一个项目。
3.导入 ZBar的sdk。把ZBar SDK的目录拉入项目,然后选中copy选项
4.在项目文件的target中加入 以下framework
5.在appDelegate文件中加入 标记部分的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; // force view class to load so it may be referenced directly from NIB [ZBarReaderView class]; return YES; }
6.在.h文件中加入 ZBarReaderViewDelegate的实现,代码如下:
// // ViewController.h // FootSafety // // Created by 泽宇 徐 on 12-6-12. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #import "ZBarSDK.h" @interface ViewController : UIViewController<ZBarReaderViewDelegate> { IBOutlet UILabel * label ; ZBarReaderView *readerView; ZBarCameraSimulator *cameraSim; } @property(nonatomic,retain) UILabel * label ; @property (nonatomic, retain) IBOutlet ZBarReaderView *readerView; @end
7.在.m文件中要实现的主要方法是:
- (void) readerView: (ZBarReaderView*) view didReadSymbols: (ZBarSymbolSet*) syms fromImage: (UIImage*) img { // do something useful with results for(ZBarSymbol *sym in syms) { self.label.text = sym.data; break; } }
这里是功能是读取照片信息,把条码放如label显示
-(void) viewDidAppear:(BOOL)animated { // run the reader when the view is visible [readerView start]; }
这个是在显示视图的时候,启动摄像头,开始扫描
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // the delegate receives decode results readerView.readerDelegate = self; // you can use this to support the simulator if(TARGET_IPHONE_SIMULATOR) { cameraSim = [[ZBarCameraSimulator alloc] initWithViewController: self]; cameraSim.readerView = readerView; } }
在初始化的时候,设置托管。
.m文件所有内容是:
// // ViewController.m // FootSafety // // Created by 泽宇 徐 on 12-6-12. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize label; @synthesize readerView; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // the delegate receives decode results readerView.readerDelegate = self; // you can use this to support the simulator if(TARGET_IPHONE_SIMULATOR) { cameraSim = [[ZBarCameraSimulator alloc] initWithViewController: self]; cameraSim.readerView = readerView; } } -(void) viewDidAppear:(BOOL)animated { // run the reader when the view is visible [readerView start]; } - (void) readerView: (ZBarReaderView*) view didReadSymbols: (ZBarSymbolSet*) syms fromImage: (UIImage*) img { // do something useful with results for(ZBarSymbol *sym in syms) { self.label.text = sym.data; break; } } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } -(void) dealloc { [self.readerView release]; [self.label release]; [super dealloc]; } @end
在ViewController.xib文件中 增加一个view,并且修改view的类是ZBarReaderView ,并且指向 .h文件中定义的
ZBarReaderView *readerView;