Android開發筆記(6)——類的設定與繼承,android筆記

來源:互聯網
上載者:User

Android開發筆記(6)——類的設定與繼承,android筆記

轉載請註明http://www.cnblogs.com/igoslly/p/6838991.html

 

【類】的設定與繼承

當設定相同格式的TextView時,已提出在styles.xml自訂格式統一TextView格式,類似【類】即為自訂方法來統一同類型的變數。

 

1、定義【類】

圖設定了一個名為TextView的【類】,【類】看作【模板】

以TextView類命名可命名多個對象(不同ID),應用【類】設定的格式和函數

Context函數詳細解釋:http://blog.csdn.net/qinjuning/article/details/7310620

 

2、建立類變數

設定類後(假設類名Order),可以命名多個以Order為模板的對象/變數。

TextView、ImageView、Button即是Android Studio開發人員命名的類,前例中我們分別命名了“quantity_text_view”“price_text_view”的TextView類型對象。

 

使用建構函式:

(ObjectDataType) (variable name) = new (ObjectDataType) (Input Args)

TextView priceText = new TextView ( context )

 

Factory method:

(ObjectDataType) (variable name) = (ObjectDataType) . (Factory Method Name) (Input Args)

MediaPlayer player = MediaPlayer .create(context,R.raw.sond);

 

3、調用類函數

通常調用方式:(ObjectVariablename) . (Method name)(Input Args)

titleTextView.setText(“News”);

類內調用

setText(“Hello”)

類內還可讀取私人變數、私人方法

類外調用

TextView.setText(“Hello”)

類外只能讀取公開變數、公用方法

 

4、繼承類

在定義附圖中,紅字標識的“extends View”即標識定義新類TextView並繼承View。

此時,View稱為父類,TextView為子類。繼承後,子類包含父類所有函數、狀態和方法,可直接類內調用函數。

子類使用父類函數,想做個性修改時,使用@override。

 

Google Developer介面最上方往往給出了類的繼承關係:

 

 

執行個體解析:

1、MainActivity extends AppcompatActivity

標識MainActivity類繼承AppcompatActivity類,直接使用該類的各種方法包括onCreate()、onCreateOptionsMenu()、findTextById()等。

2、onCreate()函數

在Java檔案設定Textview:

TextView textview = new TextView (this)     // this表current activitysetContentView(textview)      //將其設定為布局元素(顯示)

 

3、修改TextView顯示內容

TextView quantity = ( TextView ) findTextById (R.id.quantity_text_view)

查看Android Developer文檔,可以找到findTextById能在視圖中尋找名為“quantity_text_view”的對象,並作為View返回。

為匹配類型可設定

View textview = findTextById (R.id.quantity_text_view)

例中,quantity為TextView類型,需使用(TextView)變數轉換。

 

 

申明:

1、本筆記為文字及圖片均為個人原創,轉載請註明部落格園-igoslly

2、Android開發課程於2017年4年參與GoogleDeveloper進行學習,筆記原版http://www.studyjamscn.com/thread-20580-1-1.html#pid272486

聯繫我們

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