随着第一个Activity跳转成功,就开始有编写博客的冲动. 废话少说!
Android开发,界面跳转当然最基础。以下讲述的是开发过程中一些关键的设置。
0. 功能:
启动界面->第二界面
第二界面->启动界面
1. package中,有两个java文件,
a. 主界面为MainActivity.java 当然是运行的第一个文件了。
b. 跳转目标界面为secondActivity.java
2. 两个源文件关键代码如下:
a 启动界面
1 package com.huang.activity02; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.view.Menu; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class MainActivity extends Activity { 12 13 Button mainButton = null; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 mainButton = (Button)findViewById(R.id.mainButton); 21 22 mainButton.setText(R.string.mainButtonText); 23 24 mainButton.setOnClickListener(new OnClickListener() 25 { 26 27 @Override 28 public void onClick(View arg0) { 29 // TODO Auto-generated method stub 30 startActivity(new Intent(MainActivity.this,secondActivity.class)); 31 } 32 }); 33 } 34 35 36 @Override 37 public boolean onCreateOptionsMenu(Menu menu) { 38 // Inflate the menu; this adds items to the action bar if it is present. 39 getMenuInflater().inflate(R.menu.main, menu); 40 return true; 41 } 42 43 }
b 第二界面
1 package com.huang.activity02; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 10 public class secondActivity extends Activity{ 11 12 Button secondButton = null; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 // TODO Auto-generated method stub 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_second); 18 19 secondButton = (Button)findViewById(R.id.secondActivityButtonId); 20 21 secondButton.setText(R.string.secondButtonText); 22 23 24 secondButton.setOnClickListener(new OnClickListener() 25 { 26 27 @Override 28 public void onClick(View arg0) { 29 // TODO Auto-generated method stub 30 startActivity(new Intent(secondActivity.this,MainActivity.class)); 31 } 32 }); 33 34 } 35 36 }
3. res/layout 添加secondActivity的界面编辑文件 activity_second.xml 代码如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".secondActivity" > 10 11 <Button 12 android:id="@+id/secondActivityButtonId" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/secondButtonInitText" 16 /> 17 18 19 </RelativeLayout>
4. res/values/strings.xml文件代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">MainActivity</string> 5 <string name="action_settings">Settings</string> 6 <string name="hello_world">Hello world!</string> 7 <string name="mainButtonText">Jump</string> 8 <string name="secondButtonText">Back</string> 9 <string name="secondActivitylabel">secondActivity</string> 10 <string name="secondButtonInitText">第二给Activity初始文字</string> 11 12 </resources>
5. AndroidManifest.xml文件内容如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.huang.activity02" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="18" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.huang.activity02.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 26 <activity 27 android:name="com.huang.activity02.secondActivity" 28 android:label="@string/secondActivitylabel" 29 ></activity> 30 31 </application> 32 33 </manifest>
6. 注意的地方:
a. 注册第二个界面程序时,使用添加代码的方式没有setContentView函数(secondActivity.java).自动添加
b. layout文件,需要指定到相应的java文件. 比如:(activity_second.xml) “tools:context=".secondActivity" >”
时间: 2024-10-15 04:22:54