zoukankan      html  css  js  c++  java
  • 二十、Activity之间通信

    在一个Activity中可以使用系统提供的startActivity(Intent intent)方法打开新的Activity,在打开新的Activity前,你可以决定是否为新的Activity传递参数。

                 

    第一种:打开新的Activity,不传递参数

    public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btnOpen
    =(Button)this.findViewById(R.id.btnOpen);
    btnOpen.setOnClickListener(
    new View.OnClickListener(){
    public void onClick(View v) {
    //新建一个显式意图,第一个参数为当前Activity类对象,第二个参数为你要打开的Activity类
    startActivity(new Intent(MainActivity.this, OtherActivity.class));
    }
    });

    }

    }

                       

                       

    第二种:打开新的Activity,并传递若干个参数给它

    package com.ljq.activitys;

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

    public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btnOpen
    =(Button)this.findViewById(R.id.btnOpen);
    btnOpen.setOnClickListener(
    new View.OnClickListener(){
    public void onClick(View v) {
    //第一种:打开新的Activity,不传递参数
    //新建一个显式意图,第一个参数为当前Activity类对象,第二个参数为你要打开的Activity类
    //startActivity(new Intent(MainActivity.this, OtherActivity.class));

    //第二种:打开新的Activity,并传递若干个参数给它
    Intent intent=new Intent(MainActivity.this, OtherActivity.class);
    //Bundle类用作携带数据
    Bundle bundle=new Bundle();
    bundle.putString(
    "name", "linjiqin");
    bundle.putInt(
    "age", 24);
    //附带上额外的数据
    intent.putExtras(bundle);
    startActivity(intent);

    }
    });

    }

    }

    在新的Activity中接收前面Activity传递过来的参数

    package com.ljq.activitys;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;

    public class OtherActivity extends Activity {
    private final static String TAG="OtherActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.other);

    //在新的Activity中接收前面Activity传递过来的参数
    Bundle bundle=this.getIntent().getExtras();
    String name
    =bundle.getString("name");
    Integer age
    =bundle.getInt("age");
    Log.i(TAG, name
    +" : "+age);

    }

    }
  • 相关阅读:
    Different AG groups have the exactly same group_id value if the group names are same and the ‘CLUSTER_TYPE = EXTERNAL/NONE’
    An example of polybase for Oracle
    use azure data studio to create external table for oracle
    Missing MSI and MSP files
    You may fail to backup log or restore log after TDE certification/key rotation.
    Password is required when adding a database to AG group if the database has a master key
    Use KTPASS instead of adden to configure mssql.keytab
    ardunio+舵机
    android webview 全屏100%显示图片
    glide 长方形图片显示圆角问题
  • 原文地址:https://www.cnblogs.com/linjiqin/p/2071196.html
Copyright © 2011-2022 走看看