iSpectrum-用Java编写iPhone程序 2

本文通过一个Java编写的iPhone应用示例介绍了程序的基本结构和实现方式,包括程序入口、主视图创建及交互逻辑,并探讨了使用Java进行iOS开发的可能性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iSpectrum-用Java编写iPhone程序 1

    官方提供的demo截图

    先分析一下iSpectrum官方demo的框架

1 Launcher是程序入口,类似于main.m,实际上代码解构也差不多

 

	public void applicationDidFinishLaunching(UIApplication app)
	{ 
		UIWindow window = new UIWindow();
		window.initWithFrame(UIScreen.mainScreen().bounds());

		nav = new UINavigationController();
		
		UIViewController ctr = new UIViewController();
		ctr.init();
		ctr.setTitle("Action sheet");
		ctr.setView(new MainView(nav));
		
		nav.initWithRootViewController(ctr);
		
		window.addSubview(nav.view());
		window.makeKeyAndVisible();
	}

 2 看看mainview,没有delloc确实很爽,不过由于没有nib,所以只能用initWithFrame很原始的添加组件了。

 

public class MainView extends UIView {
	/**
	 * Fields
	 */
	private UIActionSheet actionSheet;
	private UINavigationController nav;
	
	/**
	 * 
	 * @param n		UINavigationController containing this view.
	 */
	public MainView(UINavigationController n){
		super();
		init();
		nav = n;
		
		/*
		 * ActionSheet
		 */
		actionSheet = new UIActionSheet();
		actionSheet.init();
		actionSheet.setActionSheetStyle(UIActionSheetStyle.UIActionSheetStyleBlackOpaque);
		actionSheet.setDelegate(new ActionSheetDelegate(this));
		actionSheet.setTitle("How many items do you want to display ?");
		actionSheet.addButtonWithTitle("Cancel");
		actionSheet.setCancelButtonIndex(0);
		actionSheet.dismissWithClickedButtonIndexAnimated(0, true);
		actionSheet.addButtonWithTitle("One item");
		actionSheet.addButtonWithTitle("Two items");
		actionSheet.addButtonWithTitle("Three items");
		actionSheet.addButtonWithTitle("Four items");
		
		/*
		 * Button
		 * Being used to open the UIActionSheet.
		 */
		UIButton button = new UIButton(){
			//@Override
			public void controlEvent() {
				// Display the UIActionSheet
				actionSheet.showInView(this.superview());
			}
		};
		button.initWithFrame(CGRect.CGRectMake(85, 30, 150, 31));
		button.setBackgroundImageForState(UIImage.imageNamed("buttonBackground.tiff"), 0);
		button.setTitleForState("Create view", 0);
		button.setTitleColorForState(UIColor.blackColor(), 0);
		button.addTargetActionForControlEvents(UIControlEvents.UIControlEventTouchUpInside);
		addSubview(button);
	}
	
	/**
	 * 
	 * @return	nav		Current UINavigationController.
	 */
	public UINavigationController getNavigationController(){
		return nav;
	}

}

 3 同样也有delegation类

 

public class ActionSheetDelegate extends UIActionSheetDelegate {
	/**
	 * Field
	 */
	private MainView view;
	
	/**
	 * 
	 * @param v		MainView that create this object.
	 */
	public ActionSheetDelegate(MainView v){
		super();
		init();
		view = v;
	}
	
	/**
	 * Method called by system when user select a button on action sheet.
	 * @param actionSheet		ActionSheet calling this method.
	 * @param buttonIndex		Index of selected button.
	 * 
	 */
	//@Override
	public void actionSheetClickedButtonAtIndex(UIActionSheet actionSheet,
			int buttonIndex) {
		if(buttonIndex == 0){
			// Click on "cancel" button
			return;
		}
		else {
			UIView newView = new UIView().init();
			
			// Add to the new UIView as many subviews as user wants.
			for(int i=1; i<=buttonIndex; i++){
				UIImageView imageView = new UIImageView();
				imageView.initWithFrame(CGRect.CGRectMake(i*40, 100, 30, 30));
				imageView.setImage(UIImage.imageNamed("img"+i+".tiff"));
				newView.addSubview(imageView);
			}
			
			UIViewController viewController = new UIViewController().init();
			if(buttonIndex == 1)
				viewController.setTitle(buttonIndex + " added item");
			else
				viewController.setTitle(buttonIndex + " added items");
			viewController.setView(newView);
			
			// Push this new view into UINavigationController stack.
			view.getNavigationController().pushViewControllerAnimated(viewController, false);
		}
	}
	
}

     从简单的demo中似乎看不到有用java的必要,因为完全无法用到java中很多功能和开源的代码,而且受到插件开发者的限制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值