零基础编写android项目:[1]入门篇
1、 有了学习的大致思路,要想进行android开发就一定要有android开发环境。因此,要事先下载好eclipse、android-sdk-windows、ADT安卓开发环境,一般百度都会搜到。以下是安装方法:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/fdb4f00d3aceaad7e3ec4751eee7340f6578b8de.jpg)
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/1562a0b9763e21c22151aa63e6e89a618725b1de.jpg)
2、 安装成功后配置android虚拟设备,环境搭建完成。如图:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/d04eec260d9a310eb01ea29931b842406bfea2de.jpg)
1、 android开发环境搭建完成后,编写android项目肯定会需要一个目录结构。具体结构如图:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/6bbfdd14f1c595eeee46fc9427530688902c9ade.jpg)
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/b666b2530688912c114d975b1b4800fc76f797de.jpg)
2、 这里需要注意的是资源文件R.java不能修改,如果编写编码过程中R文件报错,一定是其他资源文件中有错误,注意排查。如果都没问题,可以根据此步骤最后2张图片所示,clean或fix一下项目即可。
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/a48bc2e8904800fccf70d3bdd42043715edb93de.jpg)
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/76b6860e5f204371e35c7d93323acd8921c58fde.jpg)
1、 我们开始设想的编写一个社交软件的项目,值传递只是该项目的毛毛雨。比如,用户登陆后下一个Activity需要获得该用户的信息。
因此,首先确定我们的思路是要从一个Activity传值到另一个Activity中,创建MainActivity.java和NextButtonActivity.java两个Activity。
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/51cd85cec7f88a778c5ac3ff6e4a2f27e6eff8de.jpg)
2、 其次我们再把这2个布局表现出来,在这里使用RelativeLayout布局(第2个布局暂不放任何内容,只判断能取到值即可)。
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/2e66f9ef28066b014a5a78f33df39187021cf3de.jpg)
3、 然后进行业务判断,这里用到了Intent对象,它也叫意图,你现在想传值过去,意思就是你的意图就是传值,所以创建Intent对象后给参数传值,再用startActivity跳转到第2个Activity。如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/aebdff86242fa8727698f557bfdaf05e4b23e9de.jpg)
4、 在MainActivity.java传完值之后,该到NextButtonActivity.java这边获取值了,也是同样用意图Intent对象的get方法进行获取。如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/ff5c88d8181961204b4a47dd70f202b374d7dfde.jpg)
5、 最后为了直观的显示出所传值,我们用到Log来在系统后台显示日志,这个方法在以后编程测试中常常用到。如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/bab5c45872dade499788c0e526042e6816e9d5de.jpg)
1、 我们以类似社交应用中支付功能的订单确认页面为例,从MainActivity把加法传值跳转到NextButtonActivity中,在再NextButtonActivity中把结果输入到文本框中点击确定,再把值传回到MainActivity中。
首先,我们一样先把视图界面表现出来,同样使用RelativeLayout布局。代码如下:
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/nextButton"
android:layout_below="@+id/nextButton"
android:ems="2" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nextButton"
android:layout_marginLeft="30dp"
android:text=" + "
android:textSize="30dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nextButton"
android:layout_marginLeft="60dp"
android:ems="2" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nextButton"
android:layout_marginLeft="90dp"
android:text=" = "
android:textSize="30dp" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nextButton"
android:layout_marginLeft="120dp"
android:ems="2" />
<Button
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:text="传值" />
然后,NextButtonActivity也用RelativeLayout布局,代码如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/50a010f85856d53d57a3935a47d2bb665059cade.jpg)
2、 接下来该传值到NextButtonActivity中去了,在这里不只是传值,而且要保证后续要从NextButtonActivity中获得结果还要传回到MainActivity中,所以跳转Activity时所用的方法为:startActivityForResult,并携带意图Intent和请求编码参数过去。代码如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/116b1ae23ea23a42a9f2eba33733ec3835bbc0de.jpg)
3、 跟第五步一样,要先接受数据,然后再点击确定按钮后要先获取文本框内的结果,并返回MainActivity。在这里需要用到setResult方法,并携带意图Intent和返回编码参数。代码如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/a31e1214c27bd2829501eaf53cb1eef97ebd36df.jpg)
4、 最后,再将NextButtonActivity中的结果显示到MainActivity中。在这里需要重写onActivityResult方法,并且当且仅当请求编码和返回编码都和刚才设置的编码一样时才能获得NextButtonActivity中的结果。代码如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/4d3d2ab33c4133bacfc948ce6a37c97623bc2fdf.jpg)
1、 当你在玩手游的时候或者在使用社交应用时正在跟一个人聊天并且打开了聊天窗口,这是突然有人给你打电话,打完电话后还能回到原来的界面,这就需要Activity保存现场状态。
在Activity保存现场状态中,需要重写onSaveInstanceState,中文意思也就是保存现场状态,其中含有参数outState,用outState就可以保存参数,可以是对象、也可以是值这种数据,一般要把现场保存到数据库或文件中,实现数据持久化。
这里值得注意的是重写onRestoreInstanceState方法可以重新保存现场状态,这种情况是在手机系统杀掉该进程后重新保存现场状态。
由于是入门篇,所以目前只演示保存到Activity的临时状态。代码如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/23fd63c5cf672b5ff1ce4e253314f4d0b40327df.jpg)
2、 现在从现场状态中取值是关键,取值一定要有outState参数,所以只能在onCreate方法中取值。但要注意onSaveInstanceState如果没有保存现场状态的话,是为空的,所以要判断onSaveInstanceState不为空的情况再取出现场状态,这样就可以在每次启动onCreate时取出现场状态的数据了。
其代码和生命周期如图所示:
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/04d7a614f4d0b503e15a0d4dcc4ec28332bf20df.jpg)
![零基础编写android项目:[1]入门篇](https://exp-picture.cdn.bcebos.com/c3c22dbf3bef354f6030ff8d23db574afb321bdf.jpg)