欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 多個(gè)Activity之間的傳值

 更新時(shí)間:2013年11月01日 09:06:25   作者:  
本篇文章將用一個(gè)實(shí)例,詳細(xì)的為大家講解怎么注冊(cè)并激活一個(gè)新的Activity,以及多個(gè)Activity之間如何傳值

下面是主Activity的代碼:

開(kāi)發(fā):Activity之間的傳值 - 51CTO.COM

復(fù)制代碼 代碼如下:

package com.chaoyang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.style.BulletSpan;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity 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.button);
        button.setOnClickListener(new View.OnClickListener() {

   //給按鈕注冊(cè)點(diǎn)擊事件,打開(kāi)新的Acticity
         @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
          //為Intent設(shè)置要激活的組件(將要激活TheOtherActivity這個(gè)Activity)
    Intent intent =new Intent(MainActivity.this,TheOtherActivity.class);//
    //寫(xiě)法一 intent.setClass(MainActivity.this, OtherActivity.class);//設(shè)置要激活的組件
    //寫(xiě)法二 intent.setComponent(new ComponentName(MainActivity.this, TheOtherActivity.class));//設(shè)置要激活的組件

    //第一種傳值方式(代碼看起來(lái)更加更簡(jiǎn)潔)
    /*
    intent.putExtra("name", "dinglang");
      intent.putExtra("age", 22);
      */
    //第二種傳值方式
    Bundle bundle =new Bundle();
    bundle.putString("name", "dinglang");
    bundle.putInt("age", 22);
    intent.putExtras(bundle);
    /*
     Intent提供了各種常用類(lèi)型重載后的putExtra()方法,如: putExtra(String name, String value)、 putExtra(String name, long value),在putExtra()方法內(nèi)部會(huì)判斷當(dāng)前Intent對(duì)象內(nèi)部是否已經(jīng)存在一個(gè)Bundle對(duì)象,如果不存在就會(huì)新建Bundle對(duì)象,以后調(diào)用putExtra()方法傳入的值都會(huì)存放于該Bundle對(duì)象
                                            這些其實(shí)可以通過(guò)看源碼的,內(nèi)部實(shí)現(xiàn)的原理都是一樣的
     */
    //startActivity(intent);//不需要接收組件的返回值,就可以直接這樣激活了
    //需要接收返回結(jié)果。注意返回的結(jié)果碼
    startActivityForResult(intent, 100);
         }
  });
    }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub

  Toast.makeText(this, data.getStringExtra("result"), 1).show();//得到返回結(jié)果
  super.onActivityResult(requestCode, resultCode, data);
 }
}


下面是otherActivity部分代碼:

在相同包下,新建一個(gè)類(lèi),繼承至Activity這個(gè)類(lèi),重寫(xiě)onCreate方法...

復(fù)制代碼 代碼如下:

package com.chaoyang.activity;

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

public class TheOtherActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.other);//設(shè)置該Activity所對(duì)應(yīng)的xml布局文件
  Intent intent =this.getIntent();//得到激活她的意圖
  String name =intent.getStringExtra("name");
  int age=intent.getExtras().getInt("age");//第二種取值方式
  TextView textView = (TextView)this.findViewById(R.id.result);
  textView.setText("姓名:"+ name+"  年齡:"+ age);
  Button button = (Button)this.findViewById(R.id.close);
  button.setOnClickListener(new View.OnClickListener() {

   //返回結(jié)果給前面的Activity
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent =new Intent();
    intent.putExtra("result", "這是處理結(jié)果");
    setResult(20, intent);//設(shè)置返回?cái)?shù)據(jù)
    finish();//關(guān)閉activity
   }
  });
 }

}


新建Activity之間,注意要在layout文件夾中新建一個(gè)XML的布局文件。(新建Android項(xiàng)目時(shí)如果選擇了創(chuàng)建Activity,會(huì)默認(rèn)新建一個(gè)XML的布局文件)

下面是布局文件main.xml:

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

    <Button 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="打開(kāi)OtherActivity"
     android:id="@+id/button"
     />
</LinearLayout>

下面是布局文件other.xml
復(fù)制代碼 代碼如下:

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

  <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="這是OtherActivity"
    android:id="@+id/result"
    />

      <Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="關(guān)閉Activity"
    android:id="@+id/close"
    />
</LinearLayout>

最后,注意修改項(xiàng)目清單文件。在里面添加,注冊(cè)新的Acticity名稱
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.chaoyang.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".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>
        <!-- 注意項(xiàng)目清單文件中要加上 -->
<activity android:name="TheOtherActivity" android:label="the other Activity"/>
    </application>
</manifest>


需要注意的知識(shí)點(diǎn):

使用Intent組件附件數(shù)據(jù)時(shí)候,為Activity之間傳值的兩種寫(xiě)法。

值得一提的是Bundle類(lèi)的作用
Bundle類(lèi)用作攜帶數(shù)據(jù),它類(lèi)似于Map,用于存放key-value名值對(duì)形式的值。相對(duì)于Map,它提供了各種常用類(lèi)型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle對(duì)象放入數(shù)據(jù),getXxx()方法用于從Bundle對(duì)象里獲取數(shù)據(jù)。Bundle的內(nèi)部實(shí)際上是使用了HashMap<String, Object>類(lèi)型的變量來(lái)存放putXxx()方法放入的值。

還有就是在onActivityResult這個(gè)方法中,第一個(gè)參數(shù)為請(qǐng)求碼,即調(diào)用startActivityForResult()傳遞過(guò)去的值 ,第二個(gè)參數(shù)為結(jié)果碼,結(jié)果碼用于標(biāo)識(shí)返回?cái)?shù)據(jù)來(lái)自哪個(gè)新Activity。都是起簡(jiǎn)單的標(biāo)識(shí)作用的(不要和http協(xié)議中的404,200等狀態(tài)碼搞混了),可以根據(jù)自己的業(yè)務(wù)需求填寫(xiě),匹配,必要時(shí)候可以根據(jù)這個(gè)去判斷。

這里就不做深入的講解了。

相關(guān)文章

  • 詳解Flutter掃碼識(shí)別二維碼內(nèi)容

    詳解Flutter掃碼識(shí)別二維碼內(nèi)容

    這篇文章主要介紹了Flutter掃碼識(shí)別二維碼內(nèi)容的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Android Okhttp斷點(diǎn)續(xù)傳面試深入解析

    Android Okhttp斷點(diǎn)續(xù)傳面試深入解析

    這篇文章主要給大家介紹了關(guān)于Android Okhttp斷點(diǎn)續(xù)傳面試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 下載、編譯、運(yùn)行android 7.1系統(tǒng)詳解(ubuntu 16.0.4)

    下載、編譯、運(yùn)行android 7.1系統(tǒng)詳解(ubuntu 16.0.4)

    Android 7的系統(tǒng)版本新增的很多的新功能,本篇文章主要介紹了基于ubuntu 16.0.4環(huán)境的下載、編譯、運(yùn)行android 7.1系統(tǒng),有興趣的可以了解一下。
    2017-01-01
  • Android自定義播放器控件VideoView

    Android自定義播放器控件VideoView

    這篇文章主要介紹了Android自定義播放器控件VideoView的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android數(shù)據(jù)緩存框架內(nèi)置ORM功能使用教程

    Android數(shù)據(jù)緩存框架內(nèi)置ORM功能使用教程

    這篇文章主要為大家介紹了Android數(shù)據(jù)緩存框架內(nèi)置ORM功能使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android實(shí)現(xiàn)布局全屏

    Android實(shí)現(xiàn)布局全屏

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)布局全屏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android通過(guò)實(shí)現(xiàn)GridView的橫向滾動(dòng)實(shí)現(xiàn)仿京東秒殺效果

    Android通過(guò)實(shí)現(xiàn)GridView的橫向滾動(dòng)實(shí)現(xiàn)仿京東秒殺效果

    這篇文章主要介紹了Android通過(guò)實(shí)現(xiàn)GridView的橫向滾動(dòng)實(shí)現(xiàn)仿京東秒殺效果,實(shí)現(xiàn)代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • Android SeekBar控制視頻播放進(jìn)度實(shí)現(xiàn)過(guò)程講解

    Android SeekBar控制視頻播放進(jìn)度實(shí)現(xiàn)過(guò)程講解

    這篇文章主要介紹了Android SeekBar控制視頻播放進(jìn)度實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-04-04
  • Android實(shí)現(xiàn)Service重啟的方法

    Android實(shí)現(xiàn)Service重啟的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)Service重啟的方法,涉及Android操作Service組件實(shí)現(xiàn)服務(wù)重啟的功能,需要的朋友可以參考下
    2015-05-05
  • 詳解Android使用Gradle統(tǒng)一配置依賴管理

    詳解Android使用Gradle統(tǒng)一配置依賴管理

    本篇文章主要介紹了詳解Android 使用 Gradle 統(tǒng)一配置依賴管理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01

最新評(píng)論