如何从一个activity启动另外一个activity Android Eclipse

参考书籍《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()



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值