當我們有很多個Activity時,要將某一個Activity設定第一個進入時
執行的內容,只要在AndroidManifest.xml中設定
如下面這個例子:
要將SecondActivity設為主要的Activity,只要將紅色的內容放進去就可以
但是,請注意,只能放在一個Activity中
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
</activity>
<activity android:name="SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
2012年11月23日 星期五
顏色的設定
當我們在設計時,可以有許多顏色的變化,而這邊有2種設定顏色的方式
將文字設為白色
TextView.setTextColor(Color.WHITE)
將文字設為綠色
TextView.setTextColor(Color.GREEN)
在 預設的顏色中會有12種可以選擇
(路徑:/res/values/color.xml)
在xml底下的程式如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="自訂名稱">#ARGB</color>
<color name="Red">#DC0300</color>
<color name="Pink">#3fFF19FC</color>
<color name="Black">#000000</color>
<color name="White">#ffffff</color>
</resources>
而在上面的例子中,總共有4種顏色的設定,
而顏色的設定由ARGB來變化
顏色的設定由A(透明)、R(紅色)、G(綠色)、B(藍色)
來設定,而它的數值由0~256,但是這裡必須用16進位
因此256=ff,而顏色的變化為8個數值
所以我們以黑色來看(沒有任何顏色):# 000000或#000
那為什麼這裡只有6個數值呢,因為它不透明,所以省略
也就是說如果你不用透明度,那你的顏色只要6個數值就好
而白色為#ffffff或#fff
如果要加上透明度,就如果上面的pink:#3fFF19FC
3f為它透明的程度,00是不透明,ff是完全透明
當我們設定xml之後,就可以在程式碼中加入設定的顏色
TextView.setTextColor(getResources().getColor(R.color.Pink))
第一種是android的預設顏色:
以Textview來看:將文字設為白色
TextView.setTextColor(Color.WHITE)
將文字設為綠色
TextView.setTextColor(Color.GREEN)
在 預設的顏色中會有12種可以選擇
而第二種為自訂顏色:
當我們自訂顏色時,要自訂一個color.xml放在/res/values下(路徑:/res/values/color.xml)
在xml底下的程式如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="自訂名稱">#ARGB</color>
<color name="Red">#DC0300</color>
<color name="Pink">#3fFF19FC</color>
<color name="Black">#000000</color>
<color name="White">#ffffff</color>
</resources>
而在上面的例子中,總共有4種顏色的設定,
而顏色的設定由ARGB來變化
顏色的設定由A(透明)、R(紅色)、G(綠色)、B(藍色)
來設定,而它的數值由0~256,但是這裡必須用16進位
因此256=ff,而顏色的變化為8個數值
所以我們以黑色來看(沒有任何顏色):# 000000或#000
那為什麼這裡只有6個數值呢,因為它不透明,所以省略
也就是說如果你不用透明度,那你的顏色只要6個數值就好
而白色為#ffffff或#fff
如果要加上透明度,就如果上面的pink:#3fFF19FC
3f為它透明的程度,00是不透明,ff是完全透明
當我們設定xml之後,就可以在程式碼中加入設定的顏色
TextView.setTextColor(getResources().getColor(R.color.Pink))
2012年11月16日 星期五
程式與界面連結
當我們建立一個專案,開始進來時android都會預設一些method()
而我們在規畫主程式時,執行的核心通常都在onCreate()之中
下面以一個簡單例子來說明,讓我們的程式能與我之前說明的layout產生關聯
程式源碼:
//這裡為你專案設定的名稱
package com.example.helloworld;
//將需要的method載入
import android.os.Bundle;
.
.
略
//宣告方式有public(公開)、private(個別)、protected(受限制的)
但主要或共用method一定要public
//extends代表繼承,在這裡我們設定的內容是個活動(activity)所以要繼承Activity
這樣子我們才能始用預設好的Activity方法
public class MainActivity extends Activity
{
//開始宣告一些變數,這些變數與誰連結、屬於那一類別、要做什麼功能
都與這些變數有關
//宣告tv1,tv2為TextView類別的變數
TextView tv1, tv2;
//宣告bt1為Button類別的變數
Button bt1;
//宣告lay1為View類別的變數(這個可用於控制layout)
View lay1;
//註解Override為覆載,也就是說執行這個method時一定要看父類別中是否有onCreate
這個函數,如果沒有則無法使用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設定要顯示的內容視窗
setContentView(R.layout.activity_main);
// 指定tv1與介面的textview1連結
tv1 = (TextView) findViewById(R.id.textView1);
// 指定bt1與介面的button1連結
bt1 = (Button) findViewById(R.id.button1);
// 指定介面的layout與lay1連結
lay1 = (View) findViewById(R.id.lay1);
}
而我們在規畫主程式時,執行的核心通常都在onCreate()之中
下面以一個簡單例子來說明,讓我們的程式能與我之前說明的layout產生關聯
程式源碼:
//這裡為你專案設定的名稱
package com.example.helloworld;
//將需要的method載入
import android.os.Bundle;
.
.
略
//宣告方式有public(公開)、private(個別)、protected(受限制的)
但主要或共用method一定要public
//extends代表繼承,在這裡我們設定的內容是個活動(activity)所以要繼承Activity
這樣子我們才能始用預設好的Activity方法
public class MainActivity extends Activity
{
//開始宣告一些變數,這些變數與誰連結、屬於那一類別、要做什麼功能
都與這些變數有關
//宣告tv1,tv2為TextView類別的變數
TextView tv1, tv2;
//宣告bt1為Button類別的變數
Button bt1;
//宣告lay1為View類別的變數(這個可用於控制layout)
View lay1;
//註解Override為覆載,也就是說執行這個method時一定要看父類別中是否有onCreate
這個函數,如果沒有則無法使用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設定要顯示的內容視窗
setContentView(R.layout.activity_main);
// 指定tv1與介面的textview1連結
tv1 = (TextView) findViewById(R.id.textView1);
// 指定bt1與介面的button1連結
bt1 = (Button) findViewById(R.id.button1);
// 指定介面的layout與lay1連結
lay1 = (View) findViewById(R.id.lay1);
}
監聽你的Button或界面(On Click)
當我們在程式中的變數與界面中連結後,在界面中的所有動作可以
適當的傳給程式,然後由程式去判斷要做什麼動作,但通常只會
做一次,除非我們有設定監聽器,才會針對每次的變化去做處理
最常見的是Button,它按下後我們會看見圖案的變化,但實際上,
每個物件都可以設變化
下面的例子將簡單說明監聽器的使用方法
//直接在這個活動中植入監聽器(implement OnClickListener)
public class MainActivity extends Activity implements OnClickListener {
TextView tv1, tv2;
Button bt1;
View lay1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
bt1 = (Button) findViewById(R.id.button1);
/*
* this代表這個activity的內容 setOnClickListenr代表設定監聽器
* 而連結的監聽器已用implements內建在Activity之中
*/
//將bt1這個變數設監聽器,只要界面中與它連結的button被按下,就會反應
bt1.setOnClickListener(MainActivity.this);
//將tv1這個變數設監聽器,只要界面中與它連結的textview被按下,就會反應
tv1.setOnClickListener(this);
// 指定介面的layout與lay1連結,並設定監聽器Listener
lay1 = (View) findViewById(R.id.lay1);
lay1.setOnClickListener(this);
}
// 每個監聽器都必須有OnClick來指定後續的動作,View則代表所要監聽的視窗
public void onClick(View v) {
// if 如果符合條件,則做if裡面的動作,只做一次
//bt1==v表示如果視窗中按下的是button
if (bt1 == v) {
//文字的內容顯示為"TEST BUTTON", 而文字顏色為紅色
tv1.setText("TEST BUTTON");
tv1.setTextColor(Color.RED);
}
//tv1==v表示如果視窗按下的是texeview
if (tv1 == v) {
//文字的顏色為綠色,並且將string中的文字讀取出來
tv1.setText(R.string/TextViewShow);
tv1.setTextColor(Color.GREEN);
}
//lay1==v表示如果視窗按下的是layout而不是button或textview
if (lay1 == v) {
//文字的顏色為灰色,並且顯示的內容為"TEST VIEW"
tv1.setText("TEST VIEW");
tv1.setTextColor(Color.GRAY);
}
}
適當的傳給程式,然後由程式去判斷要做什麼動作,但通常只會
做一次,除非我們有設定監聽器,才會針對每次的變化去做處理
最常見的是Button,它按下後我們會看見圖案的變化,但實際上,
每個物件都可以設變化
下面的例子將簡單說明監聽器的使用方法
//直接在這個活動中植入監聽器(implement OnClickListener)
public class MainActivity extends Activity implements OnClickListener {
TextView tv1, tv2;
Button bt1;
View lay1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
bt1 = (Button) findViewById(R.id.button1);
/*
* this代表這個activity的內容 setOnClickListenr代表設定監聽器
* 而連結的監聽器已用implements內建在Activity之中
*/
//將bt1這個變數設監聽器,只要界面中與它連結的button被按下,就會反應
bt1.setOnClickListener(MainActivity.this);
//將tv1這個變數設監聽器,只要界面中與它連結的textview被按下,就會反應
tv1.setOnClickListener(this);
// 指定介面的layout與lay1連結,並設定監聽器Listener
lay1 = (View) findViewById(R.id.lay1);
lay1.setOnClickListener(this);
}
// 每個監聽器都必須有OnClick來指定後續的動作,View則代表所要監聽的視窗
public void onClick(View v) {
// if 如果符合條件,則做if裡面的動作,只做一次
//bt1==v表示如果視窗中按下的是button
if (bt1 == v) {
//文字的內容顯示為"TEST BUTTON", 而文字顏色為紅色
tv1.setText("TEST BUTTON");
tv1.setTextColor(Color.RED);
}
//tv1==v表示如果視窗按下的是texeview
if (tv1 == v) {
//文字的顏色為綠色,並且將string中的文字讀取出來
tv1.setText(R.string/TextViewShow);
tv1.setTextColor(Color.GREEN);
}
//lay1==v表示如果視窗按下的是layout而不是button或textview
if (lay1 == v) {
//文字的顏色為灰色,並且顯示的內容為"TEST VIEW"
tv1.setText("TEST VIEW");
tv1.setTextColor(Color.GRAY);
}
}
Layout的基本設計
預設的專案中,android已給定一個Relative的界面
上面這個界面很簡單,而裡面包含了3個物件~layout、textview、button
由這個結構可看出RelativeLayout包含了2個物件TextView與Button在裡面
結構簡單表示:
<RelativeLayout
<TextView />
<Button />
/>
以下為它的xml源碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
(給定layout一個名稱lay1,而自訂id一定要用@+id/名稱)
android:id="@+id/lay1"
(設定它的寬度 match_parent表示符合父層的寬)
android:layout_width="match_parent"
(設定它的高度 match_parent表示符合父層的高)
android:layout_height="match_parent" >
<TextView
(給定textview一個名稱textView1,而自訂id一定要用@+id/名稱)
android:id="@+id/textView1"
(設定它的寬度 wrap_content表示符合輸入內容的寬)
android:layout_width="wrap_content"
(設定它的高度 wrap_content表示符合輸入內容的高)
android:layout_height="wrap_content"
(alignParentTop代表要以父層的最高處為參考線)
android:layout_alignParentTop="true"
(centerHorizontal代表是否要在水平的中央處)
android:layout_centerHorizontal="true"
(marginTop代表離上邊多遠處,48dp代表距離)
android:layout_marginTop="48dp"
(文字內容可直接寫入,要代入字串則用@string/字串名稱)
android:text="@string/TextView"
(文字的外觀:下面是android預設的字形(大的))
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<Button
(給定textview一個名稱Button1,而自訂id一定要用@+id/名稱)
android:id="@+id/button1"
(設定它的寬度 wrap_content表示符合輸入內容的寬)
android:layout_width="wrap_content"
(設定它的高度 match_parent表示符合父層的高)
android:layout_height="wrap_content"
(alignParentBottom代表要以父層的最底處為參考線)
android:layout_alignParentBottom="true"
(alignParentLeft代表要以父層的左邊為參考線)
android:layout_alignParentLeft="true"
(marginBottom表示要離底部多遠)
android:layout_marginBottom="136dp"
(marginLeft表示要離左邊多遠)
android:layout_marginLeft="37dp"
android:text="@string/Button"
/>
</RelativeLayout>
上面這個界面很簡單,而裡面包含了3個物件~layout、textview、button
由這個結構可看出RelativeLayout包含了2個物件TextView與Button在裡面
結構簡單表示:
<RelativeLayout
<TextView />
<Button />
/>
以下為它的xml源碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
(給定layout一個名稱lay1,而自訂id一定要用@+id/名稱)
android:id="@+id/lay1"
(設定它的寬度 match_parent表示符合父層的寬)
android:layout_width="match_parent"
(設定它的高度 match_parent表示符合父層的高)
android:layout_height="match_parent" >
<TextView
(給定textview一個名稱textView1,而自訂id一定要用@+id/名稱)
android:id="@+id/textView1"
(設定它的寬度 wrap_content表示符合輸入內容的寬)
android:layout_width="wrap_content"
(設定它的高度 wrap_content表示符合輸入內容的高)
android:layout_height="wrap_content"
(alignParentTop代表要以父層的最高處為參考線)
android:layout_alignParentTop="true"
(centerHorizontal代表是否要在水平的中央處)
android:layout_centerHorizontal="true"
(marginTop代表離上邊多遠處,48dp代表距離)
android:layout_marginTop="48dp"
(文字內容可直接寫入,要代入字串則用@string/字串名稱)
android:text="@string/TextView"
(文字的外觀:下面是android預設的字形(大的))
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<Button
(給定textview一個名稱Button1,而自訂id一定要用@+id/名稱)
android:id="@+id/button1"
(設定它的寬度 wrap_content表示符合輸入內容的寬)
android:layout_width="wrap_content"
(設定它的高度 match_parent表示符合父層的高)
android:layout_height="wrap_content"
(alignParentBottom代表要以父層的最底處為參考線)
android:layout_alignParentBottom="true"
(alignParentLeft代表要以父層的左邊為參考線)
android:layout_alignParentLeft="true"
(marginBottom表示要離底部多遠)
android:layout_marginBottom="136dp"
(marginLeft表示要離左邊多遠)
android:layout_marginLeft="37dp"
android:text="@string/Button"
/>
</RelativeLayout>
訂閱:
文章 (Atom)