AndroidWidget控制項開發教程-2

來源:互聯網
上載者:User

AndroidWidget控制項開發教程-2

一、EditText編輯框的基礎知識

由於EditText是由TextView類繼承而來的,所以TextView類的方法它都有,這裡我們要注意幾個特殊的地方:

EditablegetText();返回編輯框內的內容,相當於MFC中的UpdateData(TRUE)

voidsetText();參數是字串類型或者String類型,設定編輯框內的內容

【注】java中String與int之間的相互轉換

String轉換為int:

Intn = Integer.parseInt(String a);

Int轉換為String:

這裡我們有三個方法:

Stringstr = int n + “”;//直接加上一個Null 字元串

Stringstr = Integer.toString(int n);

Stringstr = String.valueOf(int n);

二、Menu的基礎知識

1、在應用程式中添加Menu菜單選項

這需要重載booleanonCreatOptionMenu(Menu menu){}函數

並且在裡面添加:

menu.add(int,int,int,intid);

//androiddeveloper中對其的簡單解釋:

abstract MenuItem
add(intgroupId,
int itemId, int order, int titleRes)

Variationon add(int,int,
int, CharSequence) thattakes a string resource identifier instead of the string itself.

2、為菜單中的Item選項添加響應點擊的事件:

這需要重載booleanonOptionsItemSelected(MenuItem item){}

Item.getItemId()== int{};

三、Activity的生命週期

Android Activity生命週期中的各個階段簡介

onCreate()

Calledwhen the activity is first created.

在Activity第一次建立的時候被調用

onStart()

Calledwhen the activity is becoming visible to the user.

當Activity變得對使用者可見的時候被調用

onResume()

Calledwhen the activity will start interacting with the user.

當Activity開始與使用者互動的時候被調用

onPause()

Calledwhen the system is about to start resuming a previous activity.

當系統將要開始建立另一個Activity的時候被調用

onStop()

Calledwhen the activity is no longer visible to the user, becauseanother activity has been resumed and is covering this one.

噹噹前的Activity變得不可見的時候被調用

onDestroy()

Thefinal call you receive before your activity is destroyed.

在你的Activity被銷毀之前被調用

兩種情況1、系統資源不夠

2、調用了activity的finish()方法

onRestart()

Calledafter your activity has been stopped, prior to it being startedagain.

沒有被destroy的情況下,再次啟動被調用



不難看出,其實這些方法都是兩兩對應的,onCreate建立與onDestroy銷毀;onStart可見與onStop不可見;onResume可編輯(即焦點)與onPause;這6個方法是相對應的,那麼就只剩下一個onRestart方法了,這個方法在什麼時候調用呢?答案就是:在Activity被onStop後,但是沒有被onDestroy,在再次啟動此Activity時就調用onRestart(而不再調用onCreate)方法;如果被onDestroy了,則是調用onCreate方法。

Task與activity之間的關係

入棧與出戰

對話方塊形式的activity的編寫:

在androidmanifest

中添加

Android:theme= “@android:style/Theme.Dialog”

聯繫我們

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