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

Android 多個Activity之間的傳值

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

下面是主Activity的代碼:

開發(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() {

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

    //第一種傳值方式(代碼看起來更加更簡潔)
    /*
    intent.putExtra("name", "dinglang");
      intent.putExtra("age", 22);
      */
    //第二種傳值方式
    Bundle bundle =new Bundle();
    bundle.putString("name", "dinglang");
    bundle.putInt("age", 22);
    intent.putExtras(bundle);
    /*
     Intent提供了各種常用類型重載后的putExtra()方法,如: putExtra(String name, String value)、 putExtra(String name, long value),在putExtra()方法內(nèi)部會判斷當(dāng)前Intent對象內(nèi)部是否已經(jīng)存在一個Bundle對象,如果不存在就會新建Bundle對象,以后調(diào)用putExtra()方法傳入的值都會存放于該Bundle對象
                                            這些其實可以通過看源碼的,內(nèi)部實現(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部分代碼:

在相同包下,新建一個類,繼承至Activity這個類,重寫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所對應(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è)置返回數(shù)據(jù)
    finish();//關(guān)閉activity
   }
  });
 }

}


新建Activity之間,注意要在layout文件夾中新建一個XML的布局文件。(新建Android項目時如果選擇了創(chuàng)建Activity,會默認新建一個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="打開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>

最后,注意修改項目清單文件。在里面添加,注冊新的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>
        <!-- 注意項目清單文件中要加上 -->
<activity android:name="TheOtherActivity" android:label="the other Activity"/>
    </application>
</manifest>


需要注意的知識點:

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

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

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

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

相關(guān)文章

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

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

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

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

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

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

    Android 7的系統(tǒng)版本新增的很多的新功能,本篇文章主要介紹了基于ubuntu 16.0.4環(huá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功能使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Android實現(xiàn)布局全屏

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

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

    Android通過實現(xiàn)GridView的橫向滾動實現(xiàn)仿京東秒殺效果

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

    Android SeekBar控制視頻播放進度實現(xiàn)過程講解

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

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

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

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

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

最新評論