zoukankan      html  css  js  c++  java
  • Android两个Activity之间的数据交换

    1. 不带数据

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            next=(Button)findViewById(R.id.next);
            next.setOnClickListener(new Button.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent=new Intent();
                    intent.setClass(MainActivity.this, Second.class);
                    startActivity(intent);    
                }
            });
        }

    2. 第一个Activity中数据传给第二个Activity

    发送端

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            next=(Button)findViewById(R.id.next);
            content=(EditText)findViewById(R.id.content);
            next.setOnClickListener(new Button.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String name=content.getText().toString();
                    Intent intent=new Intent();
                    intent.putExtra("name", name);
                    //Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                    intent.setClass(MainActivity.this, Second.class);
                    startActivity(intent);    
                }
            });
        }

    接收端

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            name=(TextView)findViewById(R.id.name);
            age=(EditText)findViewById(R.id.age);
            back=(Button)findViewById(R.id.back);
            
            Intent intent=getIntent();
            String na=intent.getStringExtra("name");
            //Toast.makeText(Second.this, na, Toast.LENGTH_SHORT).show();
            name.setText(na);
        }

    3. 第二个Activity把数据传给第一个Activity

    第一个Activity

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            next=(Button)findViewById(R.id.next);
            content=(EditText)findViewById(R.id.content);
            next.setOnClickListener(new Button.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String name=content.getText().toString();
                    Intent intent=new Intent();
                    intent.putExtra("name", name);
                    //Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                    intent.setClass(MainActivity.this, Second.class);
                    startActivityForResult(intent, RESULT_CODE);    
                }
            });
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode==RESULT_CODE){
                if(resultCode==Second.RESULT_CODE){
                    Bundle bundle=data.getExtras();
                    String con=bundle.getString("age");
                    content.setText(con);
                }
            }
            
        }

    第二个Activity

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            name=(TextView)findViewById(R.id.name);
            age=(EditText)findViewById(R.id.age);
            back=(Button)findViewById(R.id.back);
            back.setOnClickListener(new Button.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String a=age.getText().toString();
                    Intent intent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putString("age", a);
                    intent.putExtras(bundle);
                    setResult(RESULT_CODE, intent);
                    finish();
                }
            });
            
            Intent intent=getIntent();
            String na=intent.getStringExtra("name");
            //Toast.makeText(Second.this, na, Toast.LENGTH_SHORT).show();
            name.setText(na);
        }
  • 相关阅读:
    《对不队》团队项目软件系统设计改进
    《对不队》团队作业五——项目需求改进
    《对不队》团队作业4—基于原型的团队项目需求调研与分析
    《对不队》第三次作业—团队项目的原型设计与开发
    《对不队团队》第二次作业:学术管理系统开题报告
    《对不队团队》第一次作业:团队亮相
    LINUX命令-shell基础命令
    Python实战-数据结构
    Python实战-函数
    lambda
  • 原文地址:https://www.cnblogs.com/wicrecend/p/4868594.html
Copyright © 2011-2022 走看看