1.基本控件和布局 + 基本適配器 + Activity生命周期及傳值和現(xiàn)場保護(hù)

1.android的項(xiàng)目目錄結(jié)構(gòu)

? ? ? ? ?Android學(xué)習(xí)首先要大致了解項(xiàng)目目錄账阻,有些看不懂的就先記下,等之后遇到了再回頭來看,印象肯定會更加深刻。

src ? ? Java源碼

gen? ? 自動生成的java文件

R.java (Resource)靜態(tài)內(nèi)部類

assets? 資產(chǎn)? 包括:音頻視頻 比較大的 不需要修改資源

不會再R.java中自動生成,必須通過路徑來訪問

bin? ? 二進(jìn)制文件(apk)

res? ? Resource 資源

drawable_hdpi 高分辨率的圖片

drawable_ldpi 低分辨率的圖片

drawable_mdpi 中分辨率的圖片

drawable_xhdpi 超高清辨率的圖片

drawable_xxhdpi 特高清分辨率的圖片

layout 布局

xml的文件锰悼,告訴Activity顯示什么樣的內(nèi)容

menu 菜單? xml的文件

values? 數(shù)據(jù)文件? ? 沒有對應(yīng)的靜態(tài)的內(nèi)部類

dimens.xml 尺寸

strings.xml 常量值

styles.xml? 樣式

AndroidMainfest.xml? 項(xiàng)目清單文件

package="com.qf.day01_helloworld" 包名,應(yīng)用程序的唯一標(biāo)示

android:versionCode="1" 版本號

application:

android:icon="@drawable/ic_launcher"? 圖標(biāo)

android:label="@string/app_name" 標(biāo)簽名稱

android:theme="@style/AppTheme" 樣式

2.DVM(Dalvik)和JVM的區(qū)別:

(1)Dalvik基于寄存器? jvm基于棧? 基于寄存器的虛擬虛擬機(jī)對于更大的程序來說,

在他們編譯的時(shí)候,花費(fèi)的時(shí)間更短

(2)Dalvik執(zhí)行.dex格式的字節(jié)碼

JVM執(zhí)行.class格式的字節(jié)碼

(3).class的文件存在很多重要信息,.dex工具會去除重復(fù)的信息

3.android五大布局

(1)線性布局? ? LinearLayout

android:layout_width="match_parent" 設(shè)置布局的寬度(通用屬性)

android:layout_height="match_parent" 設(shè)置布局的高度(通用屬性)

屬性值:match_parent 填充父容器的寬度和高度

fill_parent? 填充父容器的寬度和高度(2.2之前的版本)

wrap_content 包裹內(nèi)容的寬度和高度

android:orientation=""? 設(shè)置線性布局的排列方式

屬性值:? vertical 垂直排列

horizontal 水平排列(默認(rèn)的)

android:gravity="center"? 父容器內(nèi)所有子控件的位置

android:gravity="bottom|right" 多個(gè)屬性直接必須“|”分割

權(quán)重? android:layout_weight="1" 子控件在父容器剩余空間中所占的比例

屬性值越大悼泌,所占的比例就越大

(2)相對布局? RelativeLayout

指子控件與父容器、控件與控件 相對位置的來進(jìn)行布局

<1>夹界,子控件相對于父容器的位置

居中對齊:

android:layout_centerHorizontal = "true"? 子控件相對于父容器水平居中android:layout_centerVertical="true"? ? ? 子控件相對于父容器垂直居中

android:layout_centerInParent = "true" ? 子控件相對于父容器完全居中

位置對齊:

android:layout_alignParentRight="true" ? ? 子控件相對于父容器的右邊對齊android:layout_alignParentLeft="true" ? ? 子控件相對于父容器的左邊對齊android:layout_alignParentBottom="true"? ? ? 子控件相對于父容器的底部對齊android:layout_alignParentTop="true" ? ? 子控件相對于父容器的頂部對齊

<2>馆里,子控件與兄弟控件之間的位置

放置的位置:

android:layout_toRightOf="@id/button_id"? ? 子控件顯示在ID為button_id的右側(cè)

android:layout_toLeftOf="@id/button_id" ? ? 子控件顯示在ID為button_id的左側(cè)

android:layout_below="@id/button_id" ? ? 子控件顯示在ID為button_id的下方

android:layout_above="@id/button_id"? ? ? ? 子控件顯示在ID為button_id的上方

對齊的方式:

android:layout_alignRight="@id/button_id"? 子控件與ID為button_id的右邊對齊

android:layout_alignLeft="@id/button_id"? ? 子控件與ID為button_id的左邊對齊

android:layout_alignBottom="@id/button_id"? 子控件與ID為button_id的底部對齊

android:layout_alignTop = "@id/button_id"? 子控件與ID為button_id的頂部對齊

android:layout_alignBaseLine = "@id/button_id" 子控件與ID為button_id的基準(zhǔn)線對齊

(3),幀布局 ? ??

(4)可柿,表格布局

(5)鸠踪,絕對布局 ? ?這三個(gè)布局很少被用到,這里不做過多講解复斥,了解即可营密。用上面的線性布局和相對布局幾乎可以達(dá)到大部分效果。另外有些老代碼可能會用到幀布局目锭,有興趣的小伙伴可以自行找資料學(xué)習(xí)评汰。

(6)纷捞,網(wǎng)格布局GridLayout (4.0后出現(xiàn)的)

1,設(shè)置組件的排列方式

android:orientation=""

屬性值: vertical 垂直

horizontal 水平

2被去,設(shè)置布局為幾行幾列

android:rowCount="4"? ? 設(shè)置網(wǎng)格布局有4行

android:columnCount="4"? 設(shè)置網(wǎng)格布局有4列

3主儡,設(shè)置控件位于幾行幾列

注意:都是從0開始算的

android:layout_row = "1"? 設(shè)置控件位于第二行顯示

android:layout_column = "2" 設(shè)置控件位于第三列顯示

4,設(shè)置某個(gè)組件橫跨幾行幾列

android:layout_rowSpan="2"? ? ? 行的跨度

android:layout_columnSpan = "3" 列的跨度

android:layout_gravity ="fill"? 填充你所跨的整行或整列

4.TextView? extends? View

android:textStyle="bold" 文字的樣式(可以疊加使用,必須用"|"分割)

屬性值:? bold? 加粗

? ? ? ? ? ? ? ?Italic 斜體

android:singleLine = "true" 是否單行顯示

android:ellipSize = "" 用省略號顯示超出文本寬度的內(nèi)容

屬性值: none? 無

? ? ? ? ? ? ?start? 開始位置省略

? ? ? ? ? ? ?middle 中間

? ? ? ? ? ? ?end? ? 結(jié)束位置

? ? ? ? ? ? ?marquee 滾動顯示

跑馬燈:

? ? ? ? ? step1:? android:ellipSize = "marquee"

? ? ? ? ? step2:? 設(shè)置焦點(diǎn)

? ? ? ? ? ? ? ? ? ? ? ?android:focusable? = "true"

? ? ? ? ? ? ? ? ? ? ? ?android:focusableInTouchMode = "true"

? ? ? ? ? step3:? android:marqueeRepeatLimit = "marquee_forever"循環(huán)滾動

? ? ? ? ? step4:? android:singleLine = "true"

android:autoLink = " " 自動連接(根據(jù)內(nèi)容類型的不同,點(diǎn)擊打開相應(yīng)的應(yīng)用程序

屬性值:? none? 無

? ? ? ? ? ? ? ?web? 網(wǎng)址

? ? ? ? ? ? ? ?email 郵箱

? ? ? ? ? ? ? ?Phone 電話號碼

? ? ? ? ? ? ? ?map? 地理位置

? ? ? ? ? ? ? ?all? 包括以上所有

5.EditText? extends? TextView

android:hint = "提示信息"? ?

?android:textColorHint = " " 提示信息的顏色? ??

?android:inputType? = "" 允許用戶輸入的內(nèi)容? ? ? ? ? ??

?屬性值: text? 默認(rèn)? ? ? ? ? ??

? ? ? ? ? ? ? ?number 只能輸入數(shù)字? ??

? ? ? ? ? ? ? ?textpassword? 文本密碼? ??

? ? ? ? ? ? ? ?date? 日期? ?

? ? ? ? ? ? ? ?Phone? 電話號碼? ? ? ? ??

android:layout_alignBaseline = ""在一條基準(zhǔn)線上

<requestFocus/>請求焦點(diǎn)

6.Button? extends? TextView

android:drawableRight? 圖片放在文本的右側(cè)

android:drawableLeft? 圖片放在文本的左側(cè)

android:drawableBottom? 圖片放在文本的下方

android:drawableTop? 圖片放在文本的上方

ImageButton

android:src="@drawable/ic_launcher"? 圖片路徑

android:background="@null"? 去除灰色背景

7.按鈕監(jiān)聽器

監(jiān)聽三部曲:

1惨缆,定義控件并且初始化

2糜值,定義監(jiān)聽器(得到監(jiān)聽器對象)

3,為控件設(shè)置監(jiān)聽器

監(jiān)聽器實(shí)現(xiàn)的方式:4種

第一種方式: 匿名內(nèi)部類

第二種方式: 成員內(nèi)部類的方式

第三中方式: 讓當(dāng)前的類 implements? OnClickListener 接口坯墨,重寫OnClick(),為控件設(shè)置監(jiān)聽器

第四種方式: Button屬性?

? ? ? ? ? ? 1,標(biāo)簽中定義屬性:android:onClick ="onClickButton"

? ? ? ? ? ? 2,? 在代碼中寫監(jiān)聽方法:

? ? ? ? ? ? ? ? ?2.1 必須是public

? ? ? ? ? ? ? ? ?2.2? 返回值必須是void

? ? ? ? ? ? ? ? ?2.3? 方法名稱必須和xml中onClick屬性的屬性值一致

? ? ? ? ? ? ? ? ?2.4? 必須有一個(gè)View類型的參數(shù)

8.ImageView extends View

android:scaleType = ""圖片顯示的方式

屬性值: matrix:? ? 不縮放,從左上角開始截取和ImageView紅箭,(可以在代碼中設(shè)置Matrix對象)

(常用)fitXY:? ? ? 不按比例縮放圖片,目標(biāo)是把圖片塞滿整個(gè)ImageView

fitStart:? 按比例縮放圖片的大小,置于頂部顯示,完全顯示圖片

(常用)fitCenter:? 按比例縮放圖片的大小,居中顯示,完全顯示圖片(默認(rèn))

fitEnd:? ? 按比例縮放圖片的大小,置于底部顯示,完全顯示圖片

center:? ? 保持圖片原來的size,居中顯示,超出部分不顯示

centerCrop? 按比例擴(kuò)大圖片的size,居中顯示,小邊填滿容器,大邊超出部分不顯示

centerInside按比例縮小圖片的size,居中顯示,超出部分不顯示

代碼中設(shè)置圖片資源:

imageView.setImageResource(R.drawabe.xxx);

9.CheckBox? 多選框 extends CompoundButton --->Button

android:checked = "true" 設(shè)置為被選中狀態(tài)

java代碼中的方法

checkBox.isChecked() 獲取多選框的狀態(tài)

checkBox.setChecked(true) 設(shè)置多選框被選中

狀態(tài)改變監(jiān)聽器 OnCheckedChangeListener (需要導(dǎo)入 android.widget.CompoundButton.OnCheckedChangeListener包,注意要與RadioButton區(qū)分)

10.RadioButton 單選按鈕? extends CompoundButton --->Button

(1)由于是單選按鈕,所以使用組來管理(RadioGroup)

(2)特點(diǎn):多個(gè)取其一(RadioButton 必須制定id)

(3)屬性

RadioGroup:

android:orientation = ""排列方式(默認(rèn)為垂直排列)

android:checked = "" 按鈕是否被點(diǎn)亮

(4)radioGroup.isChecked();判斷是否被選中,默認(rèn)為true

(5)從radioGroup組上獲取其中的radioButton,判斷是否被選中

((RadioButton)radioGroup.getChildAt(index)).ischecked();

(6)監(jiān)聽事件:OnCheckedChangeListener

只能給RadioGroup設(shè)置OncheckedChangeListener

需要導(dǎo)入android.widget.RadioGroup.OncheckedChangeListener包

與checkBox區(qū)分

11.Spinner? 下拉菜單? ? ? ??

android:spinnerMode=""? Spinner的展示方式? ? ? ? ? ??

? ? ? ? ? ? 屬性值:drodown? 下拉方式? ? ? ??

? ? ? ? ? ? ? ? ? ? ? ? ? dialog? ? 對話框? ? ? ?

android:prompt="" 在對話框方式下設(shè)置標(biāo)題(標(biāo)題內(nèi)容只能應(yīng)用,不能寫死)? ? ? ? ??

android:entries = "@array/citys" 下拉菜單的內(nèi)容? ? 數(shù)據(jù)源的填充:? ? ? ? ? ? ? ? ? ??

<1>數(shù)據(jù)源必須是String[]或List(純文本)

<2>通過屬性填充:android:entries=""

<3>在代碼中使用ArrayAdapter數(shù)組適配器填充數(shù)據(jù)

12.ArrayAdapter數(shù)組適配器:

(1)三個(gè)參數(shù)的(數(shù)據(jù)源為數(shù)組)? ? ??

? ? ? ? ? ? this? 上下文對象? ? ?

? ? ? ? ? ? resource item的布局資源? ? ??

? ? ? ? ? ? Object 數(shù)據(jù)源----數(shù)組? ? ?

? ? ? ? ? ? ArrayAdapteradapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,data);? ? ??

(2)三個(gè)參數(shù)的(數(shù)據(jù)源為集合)? ? ??

? ? ? ? ? ? this? 上下文對象 ? ? ?

? ? ? ? ? ? resource item的布局資源? ? ??

? ? ? ? ? ? Object 數(shù)據(jù)源----List集合? ? ??

? ? ? ? ? ? ArrayAdapteradapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,data);? (3)四個(gè)參數(shù) ? ? ??

this? 上下文對象? ? ??

resource item的布局資源? ? ?

?textViewResourceId? item布局資源中要用的TextView的ID? ? ? ? ? ? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 當(dāng)item的布局資源中只有一個(gè)TextView時(shí),此參數(shù)可省略? ? ??

Object 數(shù)據(jù)源----List集合或者? ? ??

ArrayAdapteradapter = new ArrayAdapter(this,R.layout.xxx,R.id.textview,data);??

?(4)API推薦? ? ? ? ? ??

this? 上下文對象? ? ?

textArrayResId? 字符串?dāng)?shù)組的ID(數(shù)組寫在Xml文件中)? ? ??

textViewResId? Item樣式的資源文件? ? ??

ArrayAdapteradapter = new ArrayAdapter(this,R.array.xxx,R.id.textview);

13.Spinner監(jiān)聽器? OnItemSelectedListener 用來監(jiān)聽用戶點(diǎn)擊下來列表的事件

spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

/**

*AdapterView parent? 整個(gè)下拉菜單的視圖(即spinner對象)

*View view? ? ? ? ? ? ? 每個(gè)Item視圖的對象

*int position? ? ? ? ? 當(dāng)前點(diǎn)擊的條目下標(biāo)

*long Id? ? ? ? ? ? ? ? 當(dāng)前點(diǎn)擊條目的Id

*/

public void onItemSelected(AdapterView parent,View view,int position,long id){

//從parent中獲取內(nèi)容

String str2 = parent.getItemAtPosition(position).toString();

//從adapter中獲取內(nèi)容

String str3 = adapter.getItem(position);

//從spinner中獲取內(nèi)容

String str4 = spinner01.getItemAtPosition(position).toString();

}

};

14.SimpleAdapter 適配器(可實(shí)現(xiàn)圖文混排:圖片只能是本地的)? ??

(1)只用一個(gè)構(gòu)造方法? ??

(2)構(gòu)造方法介紹:? ??

?/**? ? *context 上下文對象? ??

?*data? ? 數(shù)據(jù)源? 數(shù)據(jù)源的格式為:List>

*resource Item的布局資源

*from? ? String[]數(shù)據(jù)源中的key

*to? ? ? int[] 布局資源的id

*/

SimpleAdapter adapter = new SimpleAdapter(this,

data,

R.layout.item_spinner,

new String[]{"icon","city"},

new int[]{R.id.icon,R.id.city});

15.AutoCompleteTextView 自動提醒的編輯框 extends EditText

(1)

android:completetionHint 設(shè)置下拉菜單中的提示標(biāo)題

android:completionThreshold? 設(shè)置用戶至少輸入多少個(gè)字符才會顯示提醒

android:dropDownhorizontalOffset? 下拉菜單于文本框之間的水平偏移寂汇。默認(rèn)與文本框左對齊

android:dropDownheight? 下拉菜單的高度

android:dropDownWidth? 下拉菜單的寬度

android:dropDownVerticalOffset 垂直偏移量

(2)設(shè)置OnItemClickListener用來監(jiān)聽用戶點(diǎn)擊下來列表的事件

spinner.setOnItemClickListener(new OnItemSelectedListener(){

? ? ? ? ? ? ? ? ? ? ? ?/**

? ? ? ? ? ? ? ? ? ? ? *AdapterView parent? 整個(gè)下拉菜單的視圖(即AutoCompleteTextView對象)

? ? ? ? ? ? ? ? ? ? ? *View view? ? ? ? ? ? ? 點(diǎn)擊的Item視圖的對象

? ? ? ? ? ? ? ? ? ? ? *int position? ? ? ? ? 當(dāng)前點(diǎn)擊的條目下標(biāo)

? ? ? ? ? ? ? ? ? ? ?*long Id? ? ? ? ? ? ? ? 當(dāng)前點(diǎn)擊條目的Id

? ? ? ? ? ? ? ? ? ? */

? ? ? ? ? ? ? ? ? ? public void onItemClick(AdapterView parent,View view,int position,long id){

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //從parent中獲取內(nèi)容

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String str2 = parent.getItemAtPosition(position).toString();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//從adapter中獲取內(nèi)容

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String str3 = adapter.getItem(position);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//從spinner中獲取內(nèi)容

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String str4 = spinner01.getItemAtPosition(position).toString();

? ? ? ? ? ? ? ? ? ? ?}

?};

(3)添加TextChangedListener監(jiān)聽器 用來監(jiān)聽輸入框文本的變化

autoView.addTextChangedListener(new TextWatcher(){

? ? ? ? ? ? ? /**

? ? ? ? ? ? ?*文本正在變化

? ? ? ? ? ? ?*charSequence s? 改變之前的文本

? ? ? ? ? ? ?*start: 改變之前文本的下標(biāo)

? ? ? ? ? ? ?*before 改變之前文本的數(shù)量

? ? ? ? ? ? ?*count? 改變的文本個(gè)數(shù)

? ? ? ? ? ? ?*/

? ? ? ? ? ? ?public void onTextChanged(CharSequence s,int start,int before,int count){

? ? ? ? ? ? ?}

? ? ? ? ? ? ?/**

? ? ? ? ? ? ?*文本改變之前觸發(fā)次方法

? ? ? ? ? ? ?*s:變化之前的文本

? ? ? ? ? ? ?*start 變化之前文本的下標(biāo)

? ? ? ? ? ? ?*count 變化之前文本的個(gè)數(shù)

? ? ? ? ? ? ?*after 變化之后文本的個(gè)數(shù)

? ? ? ? ? ? ?*/

? ? ? ? ? ? ?public void beforeTextChanged(Charsequence s,int start,int count,int after){

? ? ? ? ? ? ?}

? ? ? ? ? ? /**

? ? ? ? ? ? ?*文本變化之后觸發(fā)次方法

? ? ? ? ? ? ?*s:s.toString() 變化之后的文本

? ? ? ? ? ? ?*/

? ? ? ? ? ? ?public void afterTextChanged(Editable s){

? ? ? ? ? ? ?}

};

16.兩個(gè)Activity之間傳值(4種)

(1)通過意圖Intent傳值

<1>A:? Intent intent = new Intent();

? ? ? ? ? ? intent.putExtra("key", value);

? ? ? ? ? ? ? ? ? ? ?putExtra();該方法的value支持所有基本數(shù)據(jù)類型和String對象? 以及他們所對應(yīng)的數(shù)組類 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 型除此之外還支持實(shí)現(xiàn)Serializable、Parcelable接口的類對象(也就是說,要想 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 在Activit之間傳對象,則該對象的類需要實(shí)現(xiàn)Serializable捣染、Parcelable接口)

? ? ? ? ? ?intent.setClass(this,xxxActivity.class);

? ? ? ? ? ?startActivity(intent);

? ? ?B:? Intent intent = getIntent();

? ? ? ? ? intent.getXXXExtra(key); 拿到對應(yīng)的值

<2>類似于上面

A: Bundle? bundle = new Bundle();

? ? ?bundle.putxxx("key",value); 與上面intent.putExtra("key",value)用法一樣

? ? ?intent.putExtras(bundle);

B: Bundle? getIntent().getBundleExtra()? 拿到bundle對象

? ? ?bundle.getxxx("key");? 拿到值

(2)靜態(tài)成員傳值(不建議使用)

在A中聲明一個(gè)公共的靜態(tài)成員,在合適的地方存入數(shù)據(jù)

在B類中直接訪問A類的靜態(tài)成員,讀取相應(yīng)的數(shù)據(jù)

(3)全局變量傳值(android建議使用)(★)

? ? ? ? ? ? ? ----Application組件中(一般存放適用于全局的數(shù)據(jù),比如用戶賬戶)? ??

<1>創(chuàng)建Application的子類,并聲明全局的變量(類成員)? ??

<2>在AndroidManiFest.xml(清單文件)中標(biāo)簽中,

? ? ? ?通過android:name="包名+類名"屬性將創(chuàng)建的Application子類引入項(xiàng)目

<3>在合適的位置存入數(shù)據(jù)

<4>在需要用的地方讀取相應(yīng)的數(shù)據(jù)

(4)回傳數(shù)據(jù)

A:startActivityForResult(Intent,110)啟動界面

B:setResult(RESULT_OK,data);設(shè)置回傳的數(shù)據(jù)(然后直接finish()關(guān)閉當(dāng)前頁面)? data為Intent類型,data中不需要setClass()

A:重寫onActivityResult(int requesCode,int resultCode,Intent data)方法

? ? ? ? ? ? ? 該方法傳回?cái)?shù)據(jù)時(shí)系統(tǒng)調(diào)用

? ? ? ? ? ? ? 參數(shù):requesCode---請求碼對應(yīng)上面的110

? ? ? ? ? ? ? ? ? ? ? ?resultCode----回復(fù)碼對應(yīng)上面的RESULT_OK

? ? ? ? ? ? ? ? ? ? ? ?data? ------傳回的數(shù)據(jù)

17.Activity的生命周期(7個(gè))從創(chuàng)建到消亡

onCreate()? ? ? 第一次創(chuàng)建時(shí)執(zhí)行? ? 初始化Activity

onStratet()? ? ? 顯示窗口時(shí)執(zhí)行? ? ? 顯示Activity

onResume()? ? ? 獲取焦點(diǎn)是執(zhí)行? ? ? 與用戶交互

onPause()? ? ? ? 失去焦點(diǎn)時(shí)執(zhí)行? ? ? 停止與用戶交互

onStop()? ? ? ? 關(guān)閉窗口時(shí)執(zhí)行? ? ? 關(guān)閉Activity

onRestart()? ? ? 重新顯示窗口? ? ? ? 再次顯示Activity

onDestroy()? ? ? 銷毀時(shí)執(zhí)行? ? ? ? ? 銷毀Activity

(1)在A中啟動B時(shí)執(zhí)行聲明周期方法的順序

A:onPause()---B:onCreate()---B:onStart()----B:onResume()---A:onStop()

從B中返回A

B:onPause()---A:onRestart----A:onStart()---A:onResume()---B:onStop()---B:onDestroy()

(2)在A中啟動B(以對話框形式顯示的Activity)

A:onParse()---B:onCreate()---B:onStart()---B:onResume()

從B中返回A

B:onParse()---A:onResume()----B:onStop()---:onDastroy()

18.現(xiàn)場保護(hù)(保持狀態(tài))

應(yīng)對橫豎屏切換時(shí)重新創(chuàng)建Activity,因此需要保持業(yè)務(wù)數(shù)據(jù)來保護(hù)現(xiàn)場

//重寫onSaveInstanceState()方法來存入數(shù)據(jù)

protected void onSaveInstanceState(Bundle outState){

? ? ? ? ? ? ?super.onSaveInstanceState(ontState);

? ? ? ? ? ? ?outState.putInt("index",index);

}

//在onCreate() 方法中獲取保護(hù)的數(shù)據(jù)

protected void onCreate(Bundle savedInstanceState){

? ? ? ? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? ? ? ? setContentView(R.layout.activity_main);

? ? ? ? ? ? ? if(savedinstanceState){

? ? ? ? ? ? ? ? ? ? ? ? ? index = savedInstanceState.getInt("index");

? ? ? ? ? ? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末骄瓣,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子液斜,更是在濱河造成了極大的恐慌累贤,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件少漆,死亡現(xiàn)場離奇詭異臼膏,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)示损,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門渗磅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人检访,你說我怎么就攤上這事始鱼。” “怎么了脆贵?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵医清,是天一觀的道長。 經(jīng)常有香客問我卖氨,道長会烙,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任筒捺,我火速辦了婚禮柏腻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘系吭。我一直安慰自己五嫂,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著沃缘,像睡著了一般躯枢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上孩灯,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天闺金,我揣著相機(jī)與錄音,去河邊找鬼峰档。 笑死败匹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的讥巡。 我是一名探鬼主播掀亩,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼欢顷!你這毒婦竟也來了槽棍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤抬驴,失蹤者是張志新(化名)和其女友劉穎炼七,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體布持,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡豌拙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了题暖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片按傅。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖胧卤,靈堂內(nèi)的尸體忽然破棺而出唯绍,到底是詐尸還是另有隱情,我是刑警寧澤枝誊,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布况芒,位于F島的核電站,受9級特大地震影響叶撒,放射性物質(zhì)發(fā)生泄漏绝骚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一痊乾、第九天 我趴在偏房一處隱蔽的房頂上張望皮壁。 院中可真熱鬧椭更,春花似錦哪审、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽滴须。三九已至,卻和暖如春叽奥,著一層夾襖步出監(jiān)牢的瞬間扔水,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工朝氓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留魔市,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓赵哲,卻偏偏與公主長得像待德,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子枫夺,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容