-调研
话说移动互联网正值风起云涌期间,各路编程高手都是摩拳擦掌,何况企业公司都开始接受现实,走移动办公,信息云端,大数据处理的步伐,在这本该三足鼎立的时刻,微软显得有点步履蹒跚,导致移动端最值得进军的平台被iOS 和 Android 几乎瓜分,这不符合历史轨迹啊,希望 WP 能厚积薄发,重回当年PC时代的辉煌。
-前序
这里就不再指点江山,直奔主题吧,来看看做同样一个功能,在iOS平台和Android平台都是具体如何实现的,代码是如何写的,这里有个分支就是iOS平台开发又分为Objective-C 和 Swift 两种语言,下面就具体来分析实现一个功能,在两个平台里,使用三种语言都是具体怎么来实现的,希望给入门无法判断开始那种语言的朋友一个指引,也能给转型iOS或Android跨平台开发的朋友一点借鉴,看看到底 Objective-C,Swift 和 Java 到底谁才更具有潜力,注意,具体代码实现不讲解,需要有一定的编程经验。
-需求
1,A页面输入 文字 ,点击下一页Push进来B页面,此时B页面显示A页面输入的文字;
2,B页面输入 文字,点击返回Pop到A页面,此时A页面显示B页面输入的文字;
3,即页面传值功能,暂且叫它礼尚往来,全部用纯代码来写;
-实现
一,iOS 之 Objective-C
1,A页面代码;
//MainViewController.m
#import "MainViewController.h" #import "SubViewController.h" #define kIPHONE_WIDTH [UIScreen mainScreen].bounds.size.width @interface MainViewController ()<ExchangeGiftDelegate> { UITextField *inputTextField; UILabel *giftLbl; } @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"礼尚"; self.view.backgroundColor = [UIColor whiteColor]; inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 120, kIPHONE_WIDTH-140, 40)]; inputTextField.placeholder = @"输入要赠送礼品名称"; inputTextField.borderStyle = UITextBorderStyleLine; inputTextField.clearButtonMode = UITextFieldViewModeWhileEditing; [self.view addSubview:inputTextField]; UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom]; sendBtn.frame = CGRectMake(kIPHONE_WIDTH-100, 120, 80, 40); sendBtn.backgroundColor = [UIColor redColor]; [sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; sendBtn.titleLabel.textAlignment = NSTextAlignmentRight; sendBtn.titleLabel.font = [UIFont systemFontOfSize:18]; [sendBtn setTitle:@"送礼" forState:UIControlStateNormal]; [sendBtn addTarget:self action:@selector(sendBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:sendBtn]; giftLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 170, kIPHONE_WIDTH-40, 40)]; giftLbl.backgroundColor = [UIColor lightGrayColor]; giftLbl.textColor = [UIColor greenColor]; giftLbl.textAlignment = NSTextAlignmentCenter; [self.view addSubview:giftLbl]; } -(void)sendBtnClick:(id)sender { NSLog(@"开始送礼"); if (inputTextField.text.length>0) { [self sendGiftName:inputTextField.text]; inputTextField.text = @""; }else{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"黄色警告" message:@"没有礼物送,不厚道吧" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil]; [alertView show]; [inputTextField becomeFirstResponder]; } } -(void)sendGiftName:(NSString*)name { NSLog(@"开始送礼:%@",name); SubViewController *subViewController = [[SubViewController alloc] init]; subViewController.giftNameStr = name; subViewController.delegate = self; [self.navigationController pushViewController:subViewController animated:YES]; } #pragma mark- ExchangeGiftDelegate -(void)exchangeGiftName:(NSString *)name { giftLbl.text = name; }
2,B页面代码;
//SubViewController.h
#import <UIKit/UIKit.h> @protocol ExchangeGiftDelegate <NSObject> -(void)exchangeGiftName:(NSString *)name; @end @interface SubViewController : UIViewController @property (nonatomic, strong) NSString *giftNameStr; @property (nonatomic, weak) id<ExchangeGiftDelegate> delegate; @end
//SubViewController.m
#import "SubViewController.h" #define kIPHONE_WIDTH [UIScreen mainScreen].bounds.size.width @interface SubViewController () { UITextField *inputTextField; UILabel *giftLbl; } @end @implementation SubViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title = @"往来"; self.view.backgroundColor = [UIColor whiteColor]; inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 120, kIPHONE_WIDTH-140, 40)]; inputTextField.placeholder = @"输入回送的礼品名称"; inputTextField.borderStyle = UITextBorderStyleLine; inputTextField.clearButtonMode = UITextFieldViewModeWhileEditing; [self.view addSubview:inputTextField]; UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom]; sendBtn.frame = CGRectMake(kIPHONE_WIDTH-100, 120, 80, 40); sendBtn.backgroundColor = [UIColor redColor]; [sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; sendBtn.titleLabel.textAlignment = NSTextAlignmentRight; sendBtn.titleLabel.font = [UIFont systemFontOfSize:18]; [sendBtn setTitle:@"回礼" forState:UIControlStateNormal]; [sendBtn addTarget:self action:@selector(sendBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:sendBtn]; giftLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 170, kIPHONE_WIDTH-40, 40)]; giftLbl.backgroundColor = [UIColor lightGrayColor]; giftLbl.textColor = [UIColor purpleColor]; giftLbl.textAlignment = NSTextAlignmentCenter; giftLbl.text = self.giftNameStr; [self.view addSubview:giftLbl]; } -(void)sendBtnClick:(id)sender { NSLog(@"开始回礼"); if (inputTextField.text.length>0) { [self sendGiftName:inputTextField.text]; [self.navigationController popViewControllerAnimated:YES]; }else{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"红色警告" message:@"没有礼物回赠,好小气呀" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil]; [alertView show]; [inputTextField becomeFirstResponder]; } } -(void)sendGiftName:(NSString*)name { if ([self.delegate respondsToSelector:@selector(exchangeGiftName:)]) { [self.delegate exchangeGiftName:inputTextField.text]; } }
二,iOS 之 Swift
1,A页面代码;
//MainViewController.swift
import UIKit class MainViewController: UIViewController,ExchangeGiftDelegate { let kIPHONE_WIDTH = UIScreen.mainScreen().bounds.size.width var inputTextField : UITextField! var giftLbl:UILabel = UILabel() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.title = "礼尚" self.view.backgroundColor = UIColor.whiteColor() inputTextField = UITextField(frame: CGRectMake(20, 120, kIPHONE_WIDTH-140, 40)) inputTextField.placeholder = "输入要赠送礼品名称" inputTextField.borderStyle = UITextBorderStyle.Line inputTextField.textAlignment = NSTextAlignment.Center inputTextField.clearButtonMode = UITextFieldViewMode.WhileEditing self.view.addSubview(inputTextField) let sendBtn:UIButton = UIButton() sendBtn.frame = CGRectMake(kIPHONE_WIDTH-100, 120, 80, 40) sendBtn.backgroundColor = UIColor.redColor() sendBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) sendBtn.setTitle("送礼", forState: UIControlState.Normal) sendBtn.addTarget(self, action: "sendBtnClick:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(sendBtn) //let giftLbl : UILabel = UILabel() giftLbl.frame = CGRectMake(20, 170, kIPHONE_WIDTH-40, 40) giftLbl.backgroundColor = UIColor.lightGrayColor() giftLbl.textColor = UIColor.greenColor() giftLbl.textAlignment = NSTextAlignment.Center self.view.addSubview(giftLbl) } func sendBtnClick(sneder:UIButton) { println("开始送礼"); sendGiftName() } func sendGiftName(){ if inputTextField.text.isEmpty { var alert : UIAlertView = UIAlertView(title: "黄色警告", message: "没有礼物送,不厚道吧", delegate: nil, cancelButtonTitle: "知道了") alert.show() inputTextField.becomeFirstResponder() }else{ sendGiftName(inputTextField.text) inputTextField.text = "" } } func sendGiftName(name:NSString){ var subVC :SubViewController = SubViewController() subVC.giftNameStr = self.inputTextField.text; subVC.delegate = self; self.navigationController?.pushViewController(subVC, animated: true) } //ExchangeGiftDelegate func exchangeGiftName(name: NSString) { giftLbl.text = name } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
2,B页面代码;
//SubViewController.swift
import UIKit @objc protocol ExchangeGiftDelegate{ optional func exchangeGiftName(name : NSString) } class SubViewController: UIViewController { let kIPHONE_WIDTH = UIScreen.mainScreen().bounds.size.width var inputTextField : UITextField! var giftLbl : UILabel = UILabel() var giftNameStr : NSString! var delegate:ExchangeGiftDelegate? override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.title = "往来" self.view.backgroundColor = UIColor.whiteColor() inputTextField = UITextField(frame: CGRectMake(20, 120, kIPHONE_WIDTH-140, 40)) inputTextField.placeholder = "输入回送的礼品名称" inputTextField.borderStyle = UITextBorderStyle.Line inputTextField.textAlignment = NSTextAlignment.Center inputTextField.clearButtonMode = UITextFieldViewMode.WhileEditing self.view.addSubview(inputTextField) let sendBtn:UIButton = UIButton() sendBtn.frame = CGRectMake(kIPHONE_WIDTH-100, 120, 80, 40) sendBtn.backgroundColor = UIColor.redColor() sendBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) sendBtn.setTitle("回礼", forState: UIControlState.Normal) sendBtn.addTarget(self, action: "sendBtnClick:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(sendBtn) giftLbl.frame = CGRectMake(20, 170, kIPHONE_WIDTH-40, 40) giftLbl.backgroundColor = UIColor.lightGrayColor() giftLbl.textColor = UIColor.purpleColor() giftLbl.textAlignment = NSTextAlignment.Center giftLbl.text = giftNameStr self.view.addSubview(giftLbl) } func sendBtnClick(sneder:UIButton) { sendGiftName() } func sendGiftName(){ if inputTextField.text.isEmpty { var alert : UIAlertView = UIAlertView(title: "红色警告", message: "没有礼物回赠,好小气呀", delegate: nil, cancelButtonTitle: "知道了") alert.show() inputTextField.becomeFirstResponder() }else{ delegate?.exchangeGiftName!(inputTextField.text) self.navigationController?.popViewControllerAnimated(true) } }
三,Android 之 Java
1,A页面代码;
//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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/minput" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/msendBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/mname" /> </LinearLayout> <TextView android:id="@+id/mgiftname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/minput" android:layout_marginTop="20dp" android:textSize="40px" android:textColor="@color/background_material_dark" android:gravity="center" /> </LinearLayout>
//MainActivity.java
public class MainActivity extends ActionBarActivity implements View.OnClickListener { private static final int REQUST_CODE = 0; private EditText inputEditText; private Button sendBtn; private TextView giftTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("礼尚"); inputEditText = (EditText)findViewById(R.id.minput); sendBtn = (Button)findViewById(R.id.msendBtn); giftTextView = (TextView)findViewById(R.id.mgiftname); sendBtn.setOnClickListener(this); } @Override public void onClick(View v) { Log.i("TAG","送礼"); String giftName = inputEditText.getText().toString(); if (giftName.length()>0){ Intent intent = new Intent(MainActivity.this,SubActivity.class); Bundle bundle = new Bundle(); bundle.putString("name", giftName); intent.putExtras(bundle); startActivityForResult(intent,0); inputEditText.setText(""); }else { Toast.makeText(this,"没有礼物送,不厚道吧",Toast.LENGTH_SHORT).show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case REQUST_CODE: { switch (resultCode){ case RESULT_OK: { Bundle bundle = data.getExtras(); giftTextView.setText(bundle.getString("name")); } break; } } break; } super.onActivityResult(requestCode, resultCode, data); } }
2,B页面代码;
//activity_sub.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" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/sinput" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/ssendBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sname" /> </LinearLayout> <TextView android:id="@+id/sgiftname" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/minput" android:layout_marginTop="20dp" android:textSize="40px" android:textColor="@color/material_blue_grey_900" android:gravity="center" /> </LinearLayout>
//SubActivity.java
public class SubActivity extends ActionBarActivity implements View.OnClickListener { private EditText inputEditText; private Button sendBtn; private TextView giftTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sub); inputEditText = (EditText)findViewById(R.id.sinput); sendBtn = (Button)findViewById(R.id.ssendBtn); giftTextView = (TextView)findViewById(R.id.sgiftname); sendBtn.setOnClickListener(this); Bundle bundle = this.getIntent().getExtras(); giftTextView.setText(bundle.getString("name")); } @Override public void onClick(View v) { Log.i("TAG", "回礼"); String giftName = inputEditText.getText().toString(); if (giftName.length()>0){ Intent intent = new Intent(SubActivity.this,MainActivity.class); Bundle bundle = new Bundle(); bundle.putString("name", giftName); intent.putExtras(bundle); setResult(RESULT_OK,intent); finish(); }else { Toast.makeText(this, "没有礼物回赠,好小气呀", Toast.LENGTH_SHORT).show(); } } }
//strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">礼尚往来</string> <string name="action_settings">Settings</string> <string name="mname">送礼</string> <string name="title_activity_sub">往来</string> <string name="sname">回礼</string> </resources>
-附图(Swift 运行效果)
-总结
代码量:Swift 明显占优势,但是对于习惯OC的人来说,有点乱七八糟,但是非常适合入门朋友,特别是从JS转过来的;
逻辑:OC 和 Swift 基本一致,通过协议委托方式来完成传值,但是安卓采用隐式方式来传递,逻辑上较iOS来显得更合理和易于理解;
总之,三种语言都有自己的风格和写法,具体选择那种,往往和自己之前的工作经验有关系,萝卜青菜,各有所爱吧,希望看完对大家有所启示就好,另外,笔者只是一个代码传道者而已,若有笔误,敬请谅解,原创博客,转载请注明出处,谢谢。