两个java文件 对应两个layout
主Activity通过按钮启动副Activity 副Activity通过多个按钮启动主Activity
主Acticity
1 package lianxi; 2 3 import com.example.jichu_lianxi.R; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class Mainactivity extends Activity{ 13 private Button button_open; //按钮相应第一步 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 // TODO Auto-generated method stub 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.mainactivity); 19 button_open = (Button) findViewById(R.id.button_open);//按钮相应第二步 20 button_open.setOnClickListener(new OnClickListener() { //按钮相应第三步 21 22 @Override 23 public void onClick(View v) { 24 // TODO Auto-generated method stub 25 Intent intent1 = new Intent(); //启动Activity 第一步 26 intent1.setClass(Mainactivity.this, NewActivity.class); 27 //启动Activity 第二步,括号中参数第一个是当前Activity,第二个是要启动的Activity 28 startActivity(intent1);//启动Activity 第三步 29 } 30 }); 31 } 32 }
副Activity
1 package lianxi; 2 3 import com.example.jichu_lianxi.R; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class NewActivity extends Activity implements OnClickListener{ 13 private Button button_1; //按钮相应 第一步 14 private Button button_2; 15 private Button button_3; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 // TODO Auto-generated method stub 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.newactivity); 21 button_1 = (Button) findViewById(R.id.button_1); //按钮相应 第二步 22 button_2 = (Button) findViewById(R.id.button_2); 23 button_3 = (Button) findViewById(R.id.button_3); 24 25 button_1.setOnClickListener(this); //按钮相应 第三步 26 button_2.setOnClickListener(this); 27 button_3.setOnClickListener(this); 28 29 } 30 @Override 31 public void onClick(View v) { //按钮相应 第四步 32 // TODO Auto-generated method stub 33 switch (v.getId()) { 34 case R.id.button_1: 35 Intent intent1 = new Intent(); 36 //启动Activity第一步 37 intent1.setClass(NewActivity.this, Mainactivity.class); 38 //启动Activity第二步 39 startActivity(intent1); 40 //启动Activity第三步 41 42 break; 43 case R.id.button_2: //参考button_1 44 break; 45 case R.id.button_3://参考button_1 46 break; 47 } 48 } 49 }
主Activity 对应layout
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/button_open" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="@string/open" /> 12 13 </LinearLayout>
副Activity 对应的layout
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/button_1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="返回主Activity" /> 12 13 <Button 14 android:id="@+id/button_2" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="返回主Activity" /> 18 19 <Button 20 android:id="@+id/button_3" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:text="返回主Activity" /> 24 25 </LinearLayout>
AndroidManifest.xml 文件代码:
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.jichu_lianxi" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="18" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@drawable/ic_launcher" 13 android:label="@string/app_name" 14 android:theme="@style/AppTheme" > 15 <activity android:name="lianxi.Mainactivity"> 16 <intent-filter 17 > 18 <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 <activity android:name="lianxi.NewActivity"></activity> 22 </application> 23 24 </manifest>
16--19行
设置此Activity为主Activity 并且显示在启动栏中
21行
每在项目中添加一个Aactvity 都要在此xml种声明下