新版Android程式碼分析,產生的程式碼都有什麼用?

來源:互聯網
上載者:User

標籤:

剛開始學習,新手,歡迎拍磚和鄙視。

直接入題,新版Android的主要程式碼分析:

 1 package com.bslk.helloworld; 2 
/*1.這裡的匯入包,會有警示告的地方,因為沒有使用,已幹掉了,其他不管,現在應該0Err0Warn*/
/*2.這是一些匯入的包,具體的不解釋,不糾結,不懂自己度娘,問了我會鄙視你的*/ 3 import android.support.v7.app.ActionBarActivity; 4 import android.support.v4.app.Fragment; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.View;10 import android.view.ViewGroup;11
/*3.新版的是繼承的ActionBarActivity,老版的教程是Activity,為了相容,現在都強制用了,百度會告訴你怎麼改回去,
個人覺得這樣的無所謂,看得懂就行,能用就行,管他官方用什麼樣的實現,想改的都是自己習慣了吧大概*/12 public class MainActivity extends ActionBarActivity {13
/*4.這是Activity建立的時候一定會調用的onCreate()方法,
     第一行是聲明
然後是繼承原來的onCreate()方法,

然後是載入activity_main這個視圖,一般來說是在XML裡定義的*/14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 /*在activity的生命週期中,只要離開了可見階段,或者說失去了焦點,activity就很可能被進程終止了,被KILL掉了!
這時候,就需要有種機制,能儲存當時的狀態,這就是savedInstanceState的作用。*/
super.onCreate(savedInstanceState);
/*載入介面*/
17 setContentView(R.layout.activity_main);18 /*添加預留位置片段,把一個屏切成幾塊這樣,像平板的話就可以顯示很多東西了。看這個吧->http://blog.csdn.net/kernel_learner/article/details/8449126*/ 19 if (savedInstanceState == null) {20 getSupportFragmentManager().beginTransaction()21 .add(R.id.container, new PlaceholderFragment()).commit();22 }23 }24
/*添加菜單*/25 @Override26 public boolean onCreateOptionsMenu(Menu menu) {27 28 // Inflate the menu; this adds items to the action bar if it is present.
/*按照真實的功能表項目的檔案添加功能表項目*/
29 getMenuInflater().inflate(R.menu.main, menu);30 return true;31 }32
/*菜單響應*/
33 @Override34 public boolean onOptionsItemSelected(MenuItem item) {35 // Handle action bar item clicks here. The action bar will36 // automatically handle clicks on the Home/Up button, so long37 // as you specify a parent activity in AndroidManifest.xml.38 /*擷取按下功能表項目的對應ID號*/
int id = item.getItemId();
/*根據菜單ID,做出對應的反應,產生的settings按了沒反應,就是這裡什麼都沒有實現*/
39 if (id == R.id.action_settings) {40 return true;41 }
/*返回*/
42 return super.onOptionsItemSelected(item);43 }44 45 /**46 * A placeholder fragment containing a simple view.47 */
/*這個在上面調用了,預留位置片段?什麼鬼,好像是添加多個fragment,以適應平板的多塊顯示,提升使用者體驗,而在手機等終端會一個個顯示。*/
48 public static class PlaceholderFragment extends Fragment {49 50 public PlaceholderFragment() {51 }52 53 @Override54 public View onCreateView(LayoutInflater inflater, ViewGroup container,55 Bundle savedInstanceState) {56 View rootView = inflater.inflate(R.layout.fragment_main, container,57 false);58 return rootView;59 }60 }61 62 }
部分還不清楚,一遍學一遍完善吧!哈哈

 

新版Android程式碼分析,產生的程式碼都有什麼用?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.