android activity

来源:互联网 发布:java就业培训班 编辑:程序博客网 时间:2024/05/22 04:58

FirstActivity.java

package mars.activity05;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class FirstActivity extends Activity {/** Called when the activity is first created. */private Button myButton;@Overridepublic void onCreate(Bundle savedInstanceState) {System.out.println("FirstActivity ---> onCreate ");super.onCreate(savedInstanceState);setContentView(R.layout.main);myButton = (Button) findViewById(R.id.myButton);myButton.setOnClickListener(new ButtonListener());}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubSystem.out.println("FirstAcvity --->onDestory");super.onDestroy();}@Overrideprotected void onPause() {// TODO Auto-generated method stubSystem.out.println("FirstAcvity --->onPause");super.onPause();}@Overrideprotected void onRestart() {// TODO Auto-generated method stubSystem.out.println("FirstAcvity --->onRestart");super.onRestart();}@Overrideprotected void onResume() {// TODO Auto-generated method stubSystem.out.println("FirstAcvity --->onResume");super.onResume();}@Overrideprotected void onStart() {// TODO Auto-generated method stubSystem.out.println("FirstAcvity --->onStart");super.onStart();}@Overrideprotected void onStop() {// TODO Auto-generated method stubSystem.out.println("FirstAcvity --->onStop");super.onStop();}class ButtonListener implements OnClickListener {public void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(FirstActivity.this, SecondActivity.class);FirstActivity.this.startActivity(intent);}}}

SecondActivity.java

package mars.activity05;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SecondActivity extends Activity{private Button secondButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {System.out.println("SecondActivity--->onCreate");// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.second);secondButton = (Button)findViewById(R.id.secondButton);secondButton.setOnClickListener(new ButtonListener());}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubSystem.out.println("SecondActivity--->onDestory");super.onDestroy();}@Overrideprotected void onPause() {// TODO Auto-generated method stubSystem.out.println("SecondActivity--->onPause");super.onPause();}@Overrideprotected void onRestart() {// TODO Auto-generated method stubSystem.out.println("SecondActivity--->onRestart");super.onRestart();}@Overrideprotected void onResume() {// TODO Auto-generated method stubSystem.out.println("SecondActivity--->onResume");super.onResume();}@Overrideprotected void onStart() {// TODO Auto-generated method stubSystem.out.println("SecondActivity--->onStart");super.onStart();}@Overrideprotected void onStop() {// TODO Auto-generated method stubSystem.out.println("SecondActivity--->onStop");super.onStop();}class ButtonListener implements OnClickListener{public void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(SecondActivity.this, FirstActivity.class);SecondActivity.this.startActivity(intent);}}}

AndroidManifest.xml

(主要是用来配置Activity)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="mars.activity04"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/firstActivityLabel">        <activity android:name=".FirstActivity"                  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=".SecondActivity" android:label="@string/secondActivityLabel"></activity>    </application>    <uses-sdk android:minSdkVersion="4" /></manifest> 
Layout文件夹下面(主要是用来设置activity外观,java文件里面主要是动作)

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><Buttonandroid:id="@+id/myButton"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/first_button"/></LinearLayout>
second.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    >    <TextView     android:layout_width="fill_parent"    android:layout_height="wrap_content"    ></TextView>   <Button    android:id="@+id/secondButton"   android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:text="@string/second_button"   /></LinearLayout>




原创粉丝点击