Android 開發學習筆記

來源:互聯網
上載者:User

第二天學習Android開發,目標是參加GoogleAndroid大賽,爭取拿個獎回來,不過獎勵是小,積累開發經驗是大,也好在找工作的日子裡找一個好工作~~

今天學習內容:helloworld 主要涉及內容是 一個Activity是一個視窗的感覺,每次建立一個Activity都,然後在xml中AndroidManifest.xml裡面註冊一下。

Android的資源都在Res裡記錄這,例如目錄values裡面 的string,可以定義程式中使用的所有字串,在layout中,則定義了所有的UI,實現了MVC的涉及模式

而引用這些資源要使用R類為 橋樑來訪問這些資源,例如setContentView(R.layout.main);

如果要使用layout中定義的資源,例如一個button,我們在UI編輯視窗可以編輯這個UI組件的ID,然後在主程式中,通過ID來引用這個組件

例如我們在main layout中定義一個button,編輯它的id是 button1,這個時候,button1在這個id,在R這個類中就自動產生了一個成員,名字和button1是一樣的,我們可以引用這個button1來引用這個資源。

代碼如下:Button button = (Button)findViewById(R.id.button2);

然後就可以對button進行邏輯編輯,例如增加相應函數:

button.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){/* 建立一個Intent對象 */Intent intent = new Intent();/* 指定intent要啟動的類 */intent.setClass(Activity01.this, Activity02.class);/* 啟動一個新的Activity */startActivity(intent);/* 關閉當前的Activity */Activity01.this.finish();}});

這部分代碼就是調用button,來添加click響應函數的

裡面的Intent的含義是一個用來調用從一個activity切換到另一個activity用的。實作類別似介面切換這種功能

例子3給的例子是訪問通訊錄,通訊錄的訪問方式隨著版本的不同發生了一些變化,實踐中,NUMBER欄位已經沒有了,下面的代碼可以獲得電話號碼和使用者

        ContentResolver cr = getContentResolver();                  //取得電話本中開始一項的游標        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);        //向下移動一下游標        while(cursor.moveToNext())         {         //取得連絡人名字        int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);             String contact = cursor.getString(nameFieldColumnIndex);         String contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID));        Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,null,null);        while(phone.moveToNext())        {        int phoneFieldcolumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);        String phoneNumber = phone.getString(phoneFieldcolumnIndex);        string += contact +":" + phoneNumber + "\n";        }
                phone.close();        //string += (contact+":"+number+"\n");        }        cursor.close();//設定TextView顯示的內容tv.setText(string);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.