zoukankan      html  css  js  c++  java
  • activity 之间传递参数

    参考:http://c.biancheng.net/view/2923.html

    mainActivity中  通过intent 传递参数

         class MyClickListener implements View.OnClickListener{
    
            @Override
            public void onClick(View view) {
                //在控制台输出一条语句
                Log.e(TAG,"刚刚点击的按钮时注册了内部类监听器对象的按钮");
                Intent intent = new Intent(MainActivity.this, secondActivity.class);
                //利用 Intent 传递数据。
                intent.putExtra("age",18);
                intent.putExtra("name","kelly");
                intent.putExtra("level",'A');
                //利用 Bundle 传递数据。
                Bundle myBundle = new Bundle();
                myBundle.putString("author","mole");
                intent.putExtras(myBundle);
                startActivity(intent);
            }
        }     
          

    在secondActivity中获取intent

       
        package com.example.myapplication;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.PersistableBundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class secondActivity extends AppCompatActivity {
        private static final String TAG = "secondActivity";
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
            super.onCreate(savedInstanceState, persistentState);
        }
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            
            Intent intent = getIntent();
            String name = intent.getStringExtra("name");
            int age = intent.getIntExtra("age",100);
            char level = intent.getCharExtra("level",'C');
    
            Log.e(TAG, "onCreate: name:" + name);
            Log.e(TAG, "onCreate: age:" + age);
            Log.e(TAG, "onCreate: level:" + level);
            Log.e(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
    
            Bundle myBundle = intent.getExtras();
            String author = myBundle.getString("author");
            Log.e(TAG, "onCreate: author:" + author);
    
            Button bt = findViewById(R.id.button_second);
    
            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(secondActivity.this,"button is clicked",Toast.LENGTH_SHORT).show();
                    Log.e(TAG,"==========匿名内部类======secondActivity====");
                    Intent intent = new Intent(secondActivity.this, MainActivity.class);
                    startActivity(intent);
                }
            });
        }
    }  

  • 相关阅读:
    《Java课程实习》日志(周二)
    spring helloworld
    [Android L or M ]解除SwitchPreference与Preference的绑定事件
    Smobiler实现列表展示—GridView(开发日志十二)
    LA 4329(树状数组)
    CreateDialog Win32 API调用的一个小问题
    Android Gallery2源代码分析
    你男朋友是程序猿吧
    javaEE之--------统计站点在线人数,安全登录等(观察者设计模式)
    AppFuse 3常见问题与解决方法
  • 原文地址:https://www.cnblogs.com/yuguangyuan/p/13218464.html
Copyright © 2011-2022 走看看