国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

34.activity攜帶數據顯示跳轉

2019-11-09 18:25:54
字體:
來源:轉載
供稿:網友

隱式傳遞數據通常使用intent.setData(Uri.parse("ldw:canshu"))攜帶參數。

顯示傳遞數據有2種攜帶數據的方法一種是intent,另一種是bundle攜帶參數。

intent方式:

intent方式攜帶參數:

    	//數據封裝到intent中,前面是參數名后面是參數的值name-value的形式    	intent.putExtra("name1", "A");    	intent.putExtra("name2", "B");intent方式獲取參數:

	//從intent對象中取出封裝好的數據	String name1 = intent.getStringExtra("name1");	String name2 = intent.getStringExtra("name2");bundle方式:

bundle方式攜帶參數:

    	//把數據封裝到bundle對象中,bundle是以keyvalue的形式傳遞數據    	Bundle bundle = new Bundle();    	bundle.putString("name1", "A");    	bundle.putString("name2", "B");    	//把bundle對象封裝到intent對象中    	intent.putExtras(bundle);bundle方式獲取參數:

	Bundle bundle = intent.getExtras();	String name1 = bundle.getString("name1");	String name2 = bundle.getString("name2");

顯示跳轉需要配置清單文件,這里面我們準備跳轉到SecondActivity

<activity android:name="com.ldw.lifecycle.SecondActivity"/>第一個activity的代碼如下:

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/name1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="A"        />    <EditText         android:id="@+id/name2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="B"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳轉"         android:onClick="click"        /></LinearLayout>MainActivity.java

package com.ldw.lifecycle;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity {    @Override    PRotected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        System.out.println("第一個onCreate方法調用");    }    public void click(View v){    	    	Intent intent = new Intent(this,SecondActivity.class);    	//數據封裝到intent中,前面是參數名后面是參數的值    	intent.putExtra("name1", "A");    	intent.putExtra("name2", "B");    	startActivity(intent);    } }第二個activity的代碼如下:activity_second.xml

<?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/res"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳轉到第一個activity"         android:onClick="click1"        /></LinearLayout>SecondActivity.java

package com.ldw.lifecycle;import java.util.Random;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class SecondActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState){		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_second);				Intent intent = getIntent();		//從intent對象中取出封裝好的數據		String name1 = intent.getStringExtra("name1");		String name2 = intent.getStringExtra("name2");		Random rd = new Random();		int result = rd.nextInt(100);				TextView res = (TextView) findViewById(R.id.res);		res.setText(name1 + "和" + name2 + "的測試結果是" +result);	}	}使用bundle傳遞數據的時候,代碼如下,布局文件不需要更改

MainActivity.java

package com.ldw.lifecycle;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        System.out.println("第一個onCreate方法調用");    }    public void click(View v){    	    	Intent intent = new Intent(this,SecondActivity.class);    	//數據封裝到intent中,前面是參數名后面是參數的值name-value的形式    	//intent.putExtra("name1", "A");    	//intent.putExtra("name2", "B");    	    	//把數據封裝到bundle對象中,bundle是以keyvalue的形式傳遞數據    	Bundle bundle = new Bundle();    	bundle.putString("name1", "A");    	bundle.putString("name2", "B");    	//把bundle對象封裝到intent對象中    	intent.putExtras(bundle);    	    	startActivity(intent);    } }SecondActivity.java

package com.ldw.lifecycle;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){		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_second);				Intent intent = getIntent();		//從intent對象中取出封裝好的數據		//String name1 = intent.getStringExtra("name1");		//String name2 = intent.getStringExtra("name2");				Bundle bundle = intent.getExtras();		String name1 = bundle.getString("name1");		String name2 = bundle.getString("name2");		Random rd = new Random();		int result = rd.nextInt(100);				TextView res = (TextView) findViewById(R.id.res);		res.setText(name1 + "和" + name2 + "的測試結果是" +result);	}	}


上一篇:adb shell 小結

下一篇:工廠模式起步

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潮安县| 维西| 东阿县| 云霄县| 江门市| 共和县| 长寿区| 红河县| 荔波县| 仪征市| 石林| 三门县| 大竹县| 安平县| 栾川县| 阳谷县| 东阿县| 吕梁市| 榆社县| 沙湾县| 怀柔区| 静海县| 定结县| 枝江市| 普安县| 栖霞市| 丁青县| 天水市| 孟州市| 威海市| 二手房| 呼伦贝尔市| 新泰市| 辽宁省| 永济市| 安义县| 鹿泉市| 河东区| 镇宁| 邵武市| 当涂县|