Activity1.2

Starting an Activity

You can start another activity by calling startActivity()

The intent specifies either the exact activity you want to start or describes the type of action you want to perform

 

An intent can also carry small amounts of data to be used by the activity that is started.

 

package com.cmge;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

/**
 * @desc	一个简单的Activity
 * @author	ljt
 * @time	2014年8月24日 下午10:42:19
 */
public class MainActivity extends Activity {

	private Button button;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) this.findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,NextActivity.class);
				startActivity(intent);
			}
		});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

 

package com.cmge;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class NextActivity extends Activity {
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
	
}

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

	<Button 
	    android:id="@+id/button1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="点击"
	    />
    
</RelativeLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

 

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.cmge.MainActivity"
            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="com.cmge.NextActivity">
        </activity>
    </application>

 

Intent intent = new Intent(MainActivity.this,NextActivity.class);
				intent.putExtra("name", "cmge");
				intent.putExtra("age", 111);
				
				Bundle bundle = new Bundle();
				bundle.putString("name1", "cmge");
				bundle.putInt("age1", 111);
				
				intent.putExtra("list",bundle);
				startActivity(intent);

 

Intent intent = getIntent();
        System.out.println("=="+intent.getStringExtra("name"));
        System.out.println("=="+intent.getIntExtra("age", 0));
        
        Bundle bundle = intent.getBundleExtra("list");
        System.out.println("=="+bundle.get("name1"));
        System.out.println("=="+bundle.get("age1"));

 

08-24 23:35:28.365: I/System.out(5382): ==cmge
08-24 23:35:28.365: I/System.out(5382): ==111
08-24 23:35:28.365: I/System.out(5382): ==cmge
08-24 23:35:28.365: I/System.out(5382): ==111

 

 Intent intent = getIntent();
//      System.out.println("=="+intent.getStringExtra("name"));
//      System.out.println("=="+intent.getIntExtra("age", 0));
        
        Log.i(TAG, "=="+intent.getStringExtra("name"));
        Log.i(TAG, "=="+intent.getIntExtra("age"));
        
        Bundle bundle = intent.getBundleExtra("list");
//      System.out.println("=="+bundle.get("name1"));
//      System.out.println("=="+bundle.get("age1"));
        Log.i(TAG, "=="+bundle.get("name1"));
        Log.i(TAG, "=="+bundle.get("age1"));

 

08-24 23:39:02.400: I/NextActivity(5567): ==cmge
08-24 23:39:02.400: I/NextActivity(5567): ==null
08-24 23:39:02.400: I/NextActivity(5567): ==cmge
08-24 23:39:02.400: I/NextActivity(5567): ==111

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值