国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

Android 多個Activity之間的傳值

2021-02-06 13:29Android開發網 Android

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

下面是主Activity的代碼:

開發:Activity之間的傳值

復制代碼 代碼如下:


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等狀態碼搞混了),可以根據自己的業務需求填寫,匹配,必要時候可以根據這個去判斷。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久99精 | 欧美在线观看黄 | 毛片在线视频 | 欧美一区二区三区在线 | 这里只有精品在线播放 | 精品久久久久久亚洲综合网 | 国产精品三级视频 | 亚洲国产精品一区在线 | 成人国产精品久久 | 精品欧美| 国产精品美女久久久久久久久久久 | 国产黄色小视频 | 免费看黄色小视频 | 97精品久久 | 亚洲国产精品自拍视频 | 在线视频亚洲 | 日韩欧美视频一区 | 毛片a级 | 久久777 | 在线观看视频黄 | 国产精品免费一区二区三区四区 | 日韩在线不卡一区 | 成人影院一区二区 | av日韩在线播放 | 97av在线 | 午夜视频网| 亚洲一区二区三区四区在线 | 韩国精品一区二区 | 色婷婷导航 | 亚洲国产精品成人va在线观看 | 精品成人av| 国产日韩一区 | 欧美日韩视频 | 亚洲视频1 | 免费在线毛片 | 色综合一区 | 久久精品国产v日韩v亚洲 | 羞涩网站 | 亚洲精品午夜视频 | 国产精品三级视频 | 国产精品欧美一区二区三区 |