激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - Android 多個Activity之間的傳值

Android 多個Activity之間的傳值

2021-02-06 13:29Android開發(fā)網(wǎng) Android

本篇文章將用一個實例,詳細(xì)的為大家講解怎么注冊并激活一個新的Activity,以及多個Activity之間如何傳值

下面是主Activity的代碼:

開發(fā):Activity之間的傳值

復(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,會默認(rèn)新建一個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ù)這個去判斷。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久撸视频 | 中国美女一级黄色片 | 亚洲第一成人久久网站 | lutube成人福利在线观看 | 成人情欲视频在线看免费 | 成人精品视频网站 | av免费在线观看av | 久久精品亚洲一区二区 | 在线亚洲免费 | 91麻豆精品国产91久久久更新资源速度超快 | 亚洲性夜色噜噜噜7777 | 国产精品久久久久久久久岛 | 欧美hdfree性xxxx | 青青青在线免费 | 操碰 | 电影91| 人人玩人人爽 | 精品国产一区二区三区久久久蜜月 | 亚洲一区二区三区在线免费观看 | 亚洲精品成人久久 | 久久久久久久一区 | 青青草华人在线 | 黄片毛片一级 | 亚洲综合视频在线播放 | 一级大黄毛片 | 午夜精品久久久久久毛片 | 成人在线精品视频 | 黄色高清视频网站 | 99精品视频在线 | 欧美黄色一级带 | 成人毛片免费视频 | 亚洲一区二区三区四区精品 | 欧美成年视频 | 久久久成人999亚洲区美女 | 国产免费一区二区三区最新不卡 | 国产精品.com | www日韩在线观看 | 99久久久国产精品免费观看 | 免费毛片随便看 | 国产一区二区视频精品 | 亚洲精品无码不卡在线播放he |