Android程式設計 作業三-設定Activity多分頁-設定背景顏色-使用Button ...

kingfishblacksmithΚινητά – Ασύρματες Τεχνολογίες

14 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

157 εμφανίσεις

Android
程式設計

作業三
-
設定
Activity
多分頁
-
設定背景顏色
-
使用
Button
















班級
:
資工三甲

姓名
:
謝瑋倫

學號
:499G0004

指導老師
:
吳建中







第一頁
MainAcvtivity

package tw.edu.stust.test_layout;


import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import
android.view.Menu;

import android.view.View;

import android.widget.Button;


public class MainActivity extends Activity {



@Override


protected void onCreate(Bundle savedInstanceState) {



super.onCreate(savedInstanceState);



setContentView(R.layout.activ
ity_main);




Button b1 = (Button) findViewById(R.id.button1);




b1.setOnClickListener(new Button.OnClickListener(){




public void onClick(View v)




{




/* new
一個
Intent
物件,並指定要啟動的
class */




Intent intent = new Intent();





intent.setClass(MainActivity.this,Activity2.class);








/*
呼叫一個新的
Activity */




startActivity(intent);




/*
關閉原本的
Activity */




MainActivity.this.finish();




}




});




Button b2 = (Button) findViewById(
R.id.button2);




b2.setOnClickListener(new Button.OnClickListener(){




public void onClick(View v)




{




/* new
一個
Intent
物件,並指定要啟動的
class */




Intent intent = new Intent();




intent.setClass( MainActivity.this, Activit
y3 .class);








/*
呼叫一個新的
Activity */




startActivity(intent);




/*
關閉原本的
Activity */




MainActivity.this.finish();




}




});


}



@Override


public boolean onCreateOptionsMenu(Menu menu) {



// Inflate the menu;
this adds items to the action bar if it is present.



getMenuInflater().inflate(R.menu.activity_main, menu);



return true;


}


}


Layout

<
RelativeLayout

xmlns:android
=
"http://schemas.android.com/apk/res/android"


xmlns:tools
=
"http://schemas.android.com/tools"


android:layout_width
=
"match_parent"


android:layout_height
=
"match_parent"


tools:context
=
".MainActivity"

>



<
TextView


android:id
=
"@+id/textView1"


android:layout_width
=
"wrap_content"


a
ndroid:layout_height
=
"wrap_content"


android:layout_alignParentTop
=
"true"


android:layout_centerHorizontal
=
"true"


android:layout_marginTop
=
"119dp"


android:text
=
"@string/hello_world"


android:textSize
=
"30sp"

/>



<
Button


android:id
=
"@+id/button2"


android:layout_width
=
"wrap_content"


android:layout_height
=
"wrap_content"


android:layout_alignRight
=
"@+id/textView1"


android:layout_below
=
"@+id/button1"


android:layout_marginTo
p
=
"18dp"



android:text
=
"goto Activity3"

/>



<
Button


android:id
=
"@+id/button1"


android:layout_width
=
"wrap_content"


android:layout_height
=
"wrap_content"


android:layout_alignLeft
=
"@+id/button2"


android:
layout_centerVertical
=
"true"



android:text
=
"goto Activity2"

/>


</
RelativeLayout
>












第二頁
Activity

package tw.edu.stust.test_layout;


import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import
android.view.Menu;

import android.view.View;

import android.widget.Button;


public class Activity2 extends Activity {



@Override


protected void onCreate(Bundle savedInstanceState) {



super.onCreate(savedInstanceState);



setContentView(R.layout.mylayout
);




Button b3 = (Button) findViewById(R.id.button3);




b3.setOnClickListener(new Button.OnClickListener(){




public void onClick(View v)




{




/* new
一個
Intent
物件,並指定要啟動的
class */




Intent intent = new Intent();




inte
nt.setClass( Activity2.this,Activity3 .class);








/*
呼叫一個新的
Activity */




startActivity(intent);




/*
關閉原本的
Activity */




Activity2.this.finish();




}




});





Button b4 = (Button) findViewById(R.id.button4);





b4.setOnClickListener(new Button.OnClickListener(){





public void onClick(View v)





{





/* new
一個
Intent
物件,並指定要啟動的
class */





Intent intent = new Intent();





intent.setClass( Activity2.this,MainActivity .class);










/*
呼叫一個新的
Activity */





startActivity(intent);





/*
關閉原本的
Activity */





Activity2.this.finish();





}





});


}



@Override


public boolean onCreateOptionsMenu(Menu menu) {



// Inflate

the menu; this adds items to the action bar if it is present.



getMenuInflater().inflate(R.menu.activity_main, menu);



return true;


}


}



Layout


<
RelativeLayout

xmlns:android
=
"http://schemas.android.com/apk/res/android"


xmlns:tools
=
"http://schemas.android.com/tools"


android:layout_width
=
"match_parent"


android:layout_height
=
"match_parent"


android:background
=
"@color/black"


tools:context
=
".MainActivity"

>



<
TextView


android:id
=
"@+id/textView1"


android
:layout_width
=
"wrap_content"


android:layout_height
=
"wrap_content"


android:layout_alignParentTop
=
"true"


android:layout_centerHorizontal
=
"true"


android:layout_marginTop
=
"119dp"


android:text
=
"Acitivity2"


android:textSize
=
"30sp"

/>



<
Button


android:id
=
"@+id/button4"


android:layout_width
=
"wrap_content"


android:layout_height
=
"wrap_content"


android:layout_centerHorizontal
=
"true"


android:layout_centerVertical
=
"true"



android:text
=
"goto Activity1"

/>



<
Button


android:id
=
"@+id/button3"


android:layout_width
=
"wrap_content"


android:layout_height
=
"wrap_content"


android:layout_alignLeft
=
"@+id/button4"


android:layout_below
=
"@+id/button4"


android:layout_marginTop
=
"39dp"


android:text
=
"goto Activity3"

/>


</
RelativeLayout
>









第三頁
Activity

package tw.edu.stust.test_layout;


import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.Button;


public class Activity3 extends Activity {



@Override


protected void onCreate(Bundle savedInstanceState) {



super.onCreate(savedInstanceState);



setContentView(R.layout.
mylayout2);




Button b5 = (Button) findViewById(R.id.button5);




b5.setOnClickListener(new Button.OnClickListener(){




public void onClick(View v)




{




/* new
一個
Intent
物件,並指定要啟動的
class */




Intent intent = new Intent();





intent.setClass( Activity3.this, MainActivity.class);








/*
呼叫一個新的
Activity */




startActivity(intent);




/*
關閉原本的
Activity */




Activity3.this.finish();




}




});




Button b6 = (Button) findViewById(
R.id.button6);




b6.setOnClickListener(new Button.OnClickListener(){




public void onClick(View v)




{




/* new
一個
Intent
物件,並指定要啟動的
class */




Intent intent = new Intent();




intent.setClass( Activity3.this, Activity2
.class);








/*
呼叫一個新的
Activity */




startActivity(intent);




/*
關閉原本的
Activity */




Activity3.this.finish();




}




});


}



@Override


public boolean onCreateOptionsMenu(Menu menu) {



// Inflate the menu; this ad
ds items to the action bar if it is present.



getMenuInflater().inflate(R.menu.activity_main, menu);



return true;


}


}

Layout

<
RelativeLayout

xmlns:android
=
"http://schemas.android.com/apk/res/android"


xmlns:tools
=
"http://schemas.android.com/tools"


android:layout_width
=
"match_parent"


android:layout_height
=
"match_parent"


android:background
=
"@color/purple"


tools:context
=
".MainActivity"

>



<
TextView


android:id
=
"@+id/textView1"


android:layout_width
=
"wrap_content"



android:layout_height
=
"wrap_content"


android:layout_alignParentTop
=
"true"


android:layout_centerHorizontal
=
"true"


android:layout_marginTop
=
"119dp"


android:text
=
"Activity3"


android:textSize
=
"30sp"

/>



<
Button


android:id
=
"@+id/button6"


android:layout_width
=
"wrap_content"


android:layout_height
=
"wrap_content"


android:layout_centerHorizontal
=
"true"


android:layout_centerVertical
=
"true"


android:text
=
"goto Activity2"

/>



<
Button


android:id
=
"@+id/button5"


android:layout_width
=
"wrap_content"


android:layout_height
=
"wrap_content"


android:layout_alignLeft
=
"@+id/button6"


android:layout_below
=
"@+id/button6"


android:
layout_marginTop
=
"27dp"


android:text
=
"goto Activity1"

/>


</
RelativeLayout
>












增加背景顏色

<?
xml

version
=
"1.0"

encoding
=
"utf
-
8"
?>

<
resources
>


<
color

name
=
"purple"
>
#C991FF
</
color
>


<
color

name
=
"black"
>
#000000
</
color
>



</
resources
>
































權限

AndroidMainfest.xml

<?
xml

version
=
"1.0"

encoding
=
"utf
-
8"
?>

<
manifest

xmlns:android
=
"http://schemas.android.com/apk/res/android"


package
=
"tw.edu.stust.test_layout"


android:versionCode
=
"1"


android:versionName
=
"1.0"

>



<
uses
-
sdk


android:minSdkVersion
=
"8"


android:targetSdkVersion
=
"17"

/>



<
application


android:allowBackup
=
"true"


android:icon
=
"@drawable/ic_launcher"


android:label
=
"@string/app_name"


android:theme
=
"@style/AppTheme"

>


<
activity


android:name
=
"tw.edu.stust.test_layout.MainActivity"


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


android:name
=
"tw.edu.stust.test_layout.Activity2"


android:label
=
"@string/app_name"

>


</
activity
>


<
activity


android:name
=
"tw.edu.stust.test_layout.Activity3"


android:label
=
"@string/app_name"

>


</
activity
>



</
application
>


</
manifest
>