参考书籍《Android Hacks》
需要改动的文件:
1. HelloActivity.java
2. main.xml
3. strings.xml
4. SubActivity.java
5. AndroidManifest.xml
基本步骤说明:
建立简单的Hello工程,在左侧的目录树上,Ctrl+C HelloActivity.java 然后Ctrl+V 然后会有提示,写SubActivity就好
main.xml也是,复制一个sub.xml
然后,抄代码
1 HelloActivity.java
package com.example.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(HelloActivity.this,SubActivity.class));
}
});
}
}
2 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn" />
</LinearLayout>
3 string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloActivity!</string>
<string name="sub">Sub activity</string>
<string name="btn">Button</string>
<string name="app_name">Hello</string>
</resources>
4 SubAcitivity.java
package com.example.hello;
import android.app.Activity;
import android.os.Bundle;
public class SubActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
}
}
5 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloActivity"
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=".SubActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
这里说明一下。在hello里面放一个button,点一下,切换到sub里面。
用到的技巧有:a) 建立新的activity b)为新的activity建立layout文件 c)添加了activity要修改androidmanifest.xml文件
使用到的新函数有:startActivity()