麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

Android 多個Activity之間的傳值

2020-04-11 12:01:50
字體:
來源:轉載
供稿:網友

下面是主Activity的代碼:

開發:Activity之間的傳值 - 51CTO.COM

復制代碼 代碼如下:

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設置要激活的組件(將要激活TheOtherActivity這個Activity)
    Intent intent =new Intent(MainActivity.this,TheOtherActivity.class);//
    //寫法一 intent.setClass(MainActivity.this, OtherActivity.class);//設置要激活的組件
    //寫法二 intent.setComponent(new ComponentName(MainActivity.this, TheOtherActivity.class));//設置要激活的組件

    //第一種傳值方式(代碼看起來更加更簡潔)
    /*
    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()方法內部會判斷當前Intent對象內部是否已經存在一個Bundle對象,如果不存在就會新建Bundle對象,以后調用putExtra()方法傳入的值都會存放于該Bundle對象
                                            這些其實可以通過看源碼的,內部實現的原理都是一樣的
     */
    //startActivity(intent);//不需要接收組件的返回值,就可以直接這樣激活了
    //需要接收返回結果。注意返回的結果碼
    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();//得到返回結果
  super.onActivityResult(requestCode, resultCode, data);
 }
}


下面是otherActivity部分代碼:

在相同包下,新建一個類,繼承至Activity這個類,重寫onCreate方法...

復制代碼 代碼如下:

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);//設置該Activity所對應的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() {

   //返回結果給前面的Activity
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent =new Intent();
    intent.putExtra("result", "這是處理結果");
    setResult(20, intent);//設置返回數據
    finish();//關閉activity
   }
  });
 }

}


新建Activity之間,注意要在layout文件夾中新建一個XML的布局文件。(新建Android項目時如果選擇了創建Activity,會默認新建一個XML的布局文件)

下面是布局文件main.xml:

復制代碼 代碼如下:

<?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
復制代碼 代碼如下:

<?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="關閉Activity"
    android:id="@+id/close"
    />
</LinearLayout>

最后,注意修改項目清單文件。在里面添加,注冊新的Acticity名稱
復制代碼 代碼如下:

<?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組件附件數據時候,為Activity之間傳值的兩種寫法。

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

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

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲午夜1000理论片aa | 久久免费视频一区二区三区 | 久久精品国产99久久久古代 | 久国产| 久久久精品视频免费看 | 超碰人人做人人爱 | 麻豆小视频在线观看 | 91短视频在线视频 | 一边吃奶一边摸下娇喘 | 国产精品视频导航 | 免费a级作爱片免费观看欧洲 | 国产精品av久久久久久久久久 | 精品一区二区电影 | 国产精品高潮99久久久久久久 | 国产噜噜噜噜噜久久久久久久久 | av色哟哟| 热99精品视频 | 欧洲色阁中文字幕 | 久久精品一区二区三区不卡牛牛 | 久久久久久久久久91 | 特级毛片免费视频 | 国产亚洲精品久久午夜玫瑰园 | 一级毛片大片 | 免费在线观看成年人视频 | 亚洲片在线观看 | 久久精精品| 精品偷拍久久 | 天天看逼 | 精品国产99久久久久久宅男i | 成人在线视频一区 | 亚洲精品久久久久久 | 91久久久久久久久久久久久久 | 中国av一级片 | 色网免费观看 | 五月激情久久 | 视频一区二区在线观看 | 欧美日韩亚洲国产 | 久久国产成人午夜av浪潮 | 久久毛片免费 | 在线天堂中文在线资源网 | 性明星video另类hd |