從頭學Android之第一個Activity程式

來源:互聯網
上載者:User

一、類階層:
 
二、什麼是Activity,如何理解Activity
1、  使用者與應用程式的互動的介面
2、  控制項的容器,我們要把控制項擺放在這個容器中
 
 
三、如何建立一個Activity
建立一個類:
1、  繼承Activity類

package com.jiahui.activity; 
 
  
 
import android.app.Activity; 
 
import android.os.Bundle; 
 
  
 
public class MyActivity01Activity extends Activity { 
 
    /** Called when the activity is first created. */ 
 
    @Override 
 
    public void onCreate(Bundle savedInstanceState) { 
 
        super.onCreate(savedInstanceState); 
 
        setContentView(R.layout.main); 
 
    } 
 

2、  重寫onCreate()方法,Activity第一次運行時就會調用這個方法,這個方法的調用者是由應用程式架構系統調用
3、  在AndroidMainfest.xml中註冊(至於AndroidMainfest.xml)
 

<activity android:name=".MyActivity01Activity" 
 
             android:label="@string/app_name"> 
 
       <intent-filter> 
 
           <action android:name="android.intent.action.MAIN" /> 
 
           <category android:name="android.intent.category.LAUNCHER" /> 
 
       </intent-filter> 
 
   </activity> 

這樣一個Activity就被建立了
為了顯示好看,我們可以為Activity中添加一些必要的控制項,在這裡我們添加一個TextView
開啟Layout檔案下的main.xml中所有的控制項都必須在這裡註冊

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
   android:orientation="vertical" android:layout_width="fill_parent" 
 
   android:layout_height="fill_parent"> 
 
   <TextView android:id="@+id/myText" android:layout_width="fill_parent" 
 
      android:layout_height="wrap_content" android:text="@string/hello" /> 
 
   <Button android:id="@+id/myButton" android:layout_width="fill_parent" 
 
      android:layout_height="wrap_content" /> 
 
</LinearLayout> 
 
  

回到前面在onCreate方法裡
可以通過findViewById()方法得到每個控制項,這個方法返回的是一個View對象所以我們要向下強轉成所要的控制項類
 

package com.jiahui.activity; 
 
  
 
import android.app.Activity; 
 
import android.os.Bundle; 
 
import android.widget.Button; 
 
import android.widget.TextView; 
 
  
 
public class MyActivity01Activity extends Activity { 
 
        
 
       public void onCreate(Bundle savedInstanceState) { 
 
  
 
              super.onCreate(savedInstanceState); 
 
  
 
              setContentView(R.layout.main);// 設定這個Activtiy使用的布局檔案 
 
  
 
              TextView myTextView = (TextView) findViewById(R.id.myText); 
 
              Button myButton = (Button) findViewById(R.id.myButton); 
 
  
 
              myTextView.setText("我的第一個TextView"); 
 
              myButton.setText("我的第一個按鈕"); 
 
  
 
       } 
 

 
  

運行結果:

摘自:jiahui524專欄

聯繫我們

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