網頁

2014年7月30日 星期三

12.Android的getText()方法

在我撰寫Andorid的時候,有時程式的功能會需要取得Button、TextView、EditText等等元件的內容,一般會認為getText()這個方法所回傳的類型是String,但是情況卻不是這樣,參考下面錯誤的範例:

Button btn = (Button) findViewById(R.id.btn);
String str = btn.getText();

這樣的寫法看似正確,但是getText()並不是回傳String的型別,因此需要加上toString()的方法強制轉型為String,將上面的範例改為正確如下:


Button btn = (Button) findViewById(R.id.btn); String str = btn.getText().toString();

2014年7月26日 星期六

10.Android的Widget之輸入類別

Android提供EditText供使用者輸入資料,EditText所撰寫的方法非常的簡單,而它的屬性也很好理解,android:hint用來設定EditText提示的文字,android:password用來設定輸入的內容是否隱藏起來,參考下面的範例。 <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="帳號" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" android:hint="密碼" />
執行結果:

輸入後的結果:
AutoCompleteTextView同樣也是能夠給使用者輸入資料,不過它能夠提供自動提示的輸入功能,例如當輸入a的時候就會跑出apple、address、alert等等的提示,下面是xml檔和執行的結果圖。
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="你最喜歡的程式語言是什麼?" /> <AutoCompleteTextView android:id="@+id/autoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" />
AutoCompleteTextView要有自動提示的文字必須要自己手動加入,這些文字可以在xml檔加入也可以在程式碼當中加入,下面是程式碼範例:
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.*; public class MainActivity extends Activity { final String[] language = {"C","C++","C#","Java","VB","JSP","Android","ASP.NET","PHP","Python"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,language); AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById (R.id.autoCompleteTextView); autoCompleteTextView.setThreshold(1); autoCompleteTextView.setAdapter(adapter); } }
上面的程式範例中將想顯示的自動提示放入陣列中,並使用ArrayAdapter進行實作,ArrayAdapter能夠儲存字串陣列並且放入Android的List物件,最後setThreshold這個屬性是用來設定輸入多少字元會出現提示的文字,在範例中設定的值是1,因此輸入第一個字元之後就會有提示的文字,setAdapter是用來設定使用哪個ArrayAdapter。

MultiAutoCompleteTextView與AutoCompleteTextView都具有自動提示的功能,不同的地方是允許多個值可以自動提示的功能,程式的寫法與AutoCompleteTextView差不多,不過加入了一個分隔符號的屬性setTokenizer設定每個值之間的分隔符號。
new MultiAutoCompleteTextView.CommaTokenizer()是指英文的,號當作MultiAutoCompleteTextView的分隔符號,下面是程式碼範例與結果圖:



xml檔:
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="你最喜歡的程式語言是什麼?" /> <MultiAutoCompleteTextView android:id="@+id/multiAutoCompleteTextView" android:layout_width="match_parent" android:layout_height="wrap_content" />

程式碼: import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.*; public class MainActivity extends Activity { final String[] language = {"C","C++","C#","Java","VB","JSP","Android","ASP.NET","PHP","Python"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item,language); MultiAutoCompleteTextView multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById (R.id.multiAutoCompleteTextView); multiAutoCompleteTextView.setThreshold(1); multiAutoCompleteTextView.setAdapter(adapter); multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }

除了上面介紹了這些輸入的類別之外Android還提供下拉式選單spinner,寫法跟前面所介紹的差不多,非常的簡單使用。

xml檔: <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="你最喜歡的程式語言是什麼?" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" />

主程式:
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.*; public class MainActivity extends Activity { final String[] language = {"C","C++","C#","Java","VB","JSP","Android","ASP.NET","PHP","Python"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,language); Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(adapter); } }

9.Android的Widget之按鈕

Android在android.widget.view底下有許多的元件提供使用者開發,一開始先介紹常用的按鈕Button和ImageButton。Android中的Button和ImageButton都能夠顯示圖片,不過這兩者還是有差別的,請看下面的範例。 <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:drawableLeft="@drawable/ic_launcher" android:onClick="function" /> <ImageButton android:id="@+id/imgBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" />
範例中Button的android:drawableLeft是設定按鈕的圖案,因為Button中android:text屬性有設定文字,因此圖案會往左移動,因此Button會同時顯示圖案和文字,而ImageButton則無法同時顯示圖案和文字,ImageButton是用android:src設定按鈕的圖案,最後這兩個元件都能設定android:onClick來指定程式當中的事件,執行的結果如下。


接下來要介紹ToggleButton,看到這個元件的名稱就可以知道是用來開啟或關閉等等的功能,這個元件主要要設定的是開啟或關閉的文字,分別是android:textOn和android:textOff這兩個屬性設定文字,下面是xml檔的範例和執行的結果。

<ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="on" android:textOff="off" />

CheckBox也是非常常用到的功能按鈕,它能夠提供單選或是複選的功能,它在xml檔中的寫法與前面介紹的按鈕並沒有差別,因此可以直接參考xml檔和執行圖。
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜歡的活動:" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電影" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旅遊" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="逛街" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="運動" />

RadioButton與CheckBox有點類似,但不同的地方在於RadioButton只支援單選,但要使用RadioButton的單選需要RadioGroup配合,不然除了每一個RadioButton都能夠被選擇以外,還無法取消掉所選的選項,最後android:checked是設定是否程式一執行就被勾選,下面是xml檔與執行的結果。
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請選擇性別:" /> <RadioGroup android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup>

2014年7月25日 星期五

8.Android之Intent

之前有說過一個Android的畫面就是一個Activity,在日後開發Android的時候不可能都只有使用一個Activity,因此需要使用Intent這個物件,假設有兩個Activity分別為A和B,A的Activity可以利用Intent跳到B的Activity。

下面這兩張圖片範例分別是A的Activity和B的Activity,當按下A的Activity的Send按鈕就會跳到B的Activity。

A的Activity
B的Activity

A的Activity程式碼如下:
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.*; import android.view.*; import android.content.*; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener(){ public void onClick(View view) { Intent intent = new Intent(MainActivity.this,ActivityB.class); startActivity(intent); } }); }
在這段程式碼中首先先實體化Button物件並找到在Layout中所定義的Button的ID,接下來定義Button按鈕事件並覆寫onClick方法,最後在實體化Intent,在這個例子中Intent需要兩個參數,從哪個Activity到個Activity,因此分別輸入所需的Activity,startActivity會讓Intent跳到下一個Activity。

Intent還有許多的用法,待續...

2014年7月24日 星期四

2.C語言資料型態

C語言的資料型態有字元、整數、浮點數。這些資料的型態方便提供使用者設定變數,每種資料型態所佔用的記憶體大小和資料範圍會不太一樣,所佔用記憶體的大小會跟所使用的編譯器有關係,下面是各種資料型態的表示方法和資料大小的範圍。

字元:
  • char : -128 ~ 127
整數:
  • int : -2147483648 ~ 2147483647
  • short : -32768 ~ 32767
  • long : -2147483648 ~ 2147483647
  • long long
浮點數:
  • float : 10^-38~10^38
  • double : 10^-308~10^308

另外C語言的整數型態包含了正整數和負整數,可以使用 signed和unsigned分別設定設定整數型態為有正負號和無正負號,例如unsigned int為無正負號 ,因為原來的整數型態就有包含正負號了,因此不用在用signed進行設定,下面的範例是顯示資料型態所佔用的位元組大小。
#include<stdio.h> int main(void) { printf("%s%ld\n","char:",sizeof(char)); printf("%s%ld\n","int:",sizeof(int)); printf("%s%ld\n","short:",sizeof(short)); printf("%s%ld\n","long:",sizeof(long)); printf("%s%ld\n","long int:",sizeof(long int)); printf("%s%ld\n","long long:",sizeof(long long)); printf("%s%ld\n","float:",sizeof(float)); printf("%s%ld\n","double:",sizeof(double)); printf("%s%ld\n","unsigned int:",sizeof(unsigned int)); printf("%s%ld\n","unsigned char:",sizeof(unsigned char)); printf("%s%ld\n","unsigned short:",sizeof(unsigned short)); printf("%s%ld\n","unsigned long:",sizeof(unsigned long)); printf("%s%ld\n","unsigned long long:",sizeof(unsigned long long)); return 0; }
C語言在輸入輸出中有對應的輸入輸出格式,資料型態都會有對應的輸入輸出格式:
  • %d : 十進位整數
  • %o : 八進位整數
  • %x : 十六進位整數,超過10的數字以小寫表示,例如0xf
  • %X : 十六進位整數,超過10的數字以大寫表示,例如0xF
  • %u : 不帶符號的十進位整數
  • %c : 輸出字元
  • %s : 輸出字串
  • %e : 使用科學記號,e為小寫
  • %E : 使用科學記號,E為小寫
  • %ld: 長整數十進位整數
  • %f : 輸出浮點數

簡略的範例如下:
#include<stdio.h> int main(void) { printf("%d\n",10); printf("%f\n",10.0); printf("%c\n",'a'); return 0; }

7.Activity生命週期

Android的Activity是具有生命週期的,我們撰寫Android時,在Eclipse裡新增一個Activity裡有onCreate方法,這個onCreate就是Android生命週期當中的其中一種,請參考下面兩張Android生命週期的圖。



根據上面的我們可以知道Activity有許多的生命週期方法,將這些方法整理出來如下:
  • onCreate
  • onStart
  • onRestart
  • onResume
  • onPause
  • onStop
  • onDestroy

當程式執行時會先呼叫方法的順序為onCreate->onStart->onResume,接著在你的裝置上按下回主頁的畫面時呼叫的方法順序為onPause->onStop,再從主畫面切回之前被暫停的程式所發生的事件為onRestart->onStart->onResume,最後如果要結束程式按下返回鍵就可以了,執行方法的順序為onPause->onStop->onDestory。


2014年7月16日 星期三

1.C語言基礎介紹

下面是C語言的hello world範例:

#include<stdio.h> int main(void) { printf("hello world"); return 0; }
執行之後的結果會顯示hello world的文字。

現在來看第一行的程式碼#include<stdio.h>,#是C語言的一種前置處理,C語言再執行時會先執行前置處理再執行程式碼。include是指要導入的檔案,這個include是C語言標準函數庫中的標頭檔,主要提供C語言基本輸入、輸出的功能。

接下來看int main(void)這段程式碼,C語言程式是由main函式為程式執行的起點,將所想寫的程式寫在{}內就可以了。而函式可以有傳回值和無傳回值,int再C語言中是數值的型態,因此main函式需要一個數值的傳回值,void可以表達兩種意思,一種可以代表是函式為無傳回值,另一種表示為無參數,上面範例的main函式參數是main表示沒有參數的意思。

程式碼當中的printf()是一個函數,它的功能是將資料輸出到螢幕上,在上面的範例中是輸出hello world,最後一個是return是C語言的關鍵字,功能是函式結束所回傳的值,因為main函式是int數值類型,所以最後的回傳值是0這個數值,0也代表程式成功執行結束的意思。