Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android--Activity在跳轉時攜帶數據

Android--Activity在跳轉時攜帶數據

編輯:關於Android編程

首先看看兩種傳遞方法示例:(一個簡單姻緣計算器)

主Activity

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void click(View v){
    	Intent intent = new Intent(this, SecondActivity.class);
    	//把數據封裝至intent對象中
//    	intent.putExtra("malename", "張三");
//    	intent.putExtra("femalename", "芙蓉姐姐");
    	
    	//把數據封裝至bundle對象中
    	Bundle bundle = new Bundle();
    	bundle.putString("malename", "張三");
    	bundle.putString("femalename", "芙蓉姐姐");
    	
    	//把bundle對象封裝至intent對象中
    	intent.putExtras(bundle);
    	startActivity(intent);
    }
    
}
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		Intent intent = getIntent();
		//從intent對象中把封裝好的數據取出來
//		String maleName = intent.getStringExtra("malename");
//		String feMaleName = intent.getStringExtra("femalename");
		
		Bundle bundle = intent.getExtras();
		String maleName = bundle.getString("malename");
		String feMaleName = bundle.getString("femalename");
		
		Random rd = new Random();
		int yinyuan = rd.nextInt(100);
		
		TextView tv = (TextView) findViewById(R.id.tv);
		tv.setText(maleName + "和" + feMaleName + "的姻緣值為" + yinyuan);
	}
}

activity_main.xml:
<linearlayout 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" android:orientation="vertical">

    <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是姻緣計算器,很准的喲">

    <edittext android:id="@+id/et_malename" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="張三">
    <edittext android:id="@+id/et_femalename" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="芙蓉姐姐"><button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="計算" android:onclick="click"></button></edittext></edittext></textview></linearlayout>
activity_second.xml:
<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">

    <textview android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是第二個Activity">

</textview></relativelayout>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved