zoukankan      html  css  js  c++  java
  • Activity与Activity之间的传值

    //----------Activity1中的布局---------------------------------

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/tv_text1"
            android:text="我是Activity1" />
        <EditText android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/et_edittext1"/>
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="跳转到Activity2"
            android:id="@+id/bt_button1"/>

    </LinearLayout>

    //------------------Activity2的布局文件-------------------------------

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

         <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/tv_text2"
            android:text="我是Activity2" />
        <EditText android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/et_edittext2"/>
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="跳转到Activity1"
            android:id="@+id/bt_button2"/>

    </LinearLayout>

    //---------------Activity1中-------------------------------

    package com.example.test;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity implements OnClickListener {

        private Button bt_button1;
        private TextView tv_text1;
        private EditText et_edittext1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv_text1 = (TextView) findViewById(R.id.tv_text1);
            et_edittext1 = (EditText) findViewById(R.id.et_edittext1);
            bt_button1 = (Button) findViewById(R.id.bt_button1);
            bt_button1.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.bt_button1:
                Intent intent=new Intent(this,TwoActivity.class);
                String text=et_edittext1.getText().toString().trim();
                intent.putExtra("kk", text);
                startActivityForResult(intent, 0);
                break;

            default:
                break;
            }
            
        }
        //此方法用户接受返回的数据
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (resultCode) {
            case 1:
                Bundle b=data.getExtras();
                String str=b.getString("ww");
                tv_text1.setText(str);
                break;

            default:
                break;
            }
            
        }


        
        
    }

    //----------------Activity2中----------------------------

    package com.example.test;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class TwoActivity extends Activity implements OnClickListener {

        private TextView tv_text2;
        private EditText et_edittext2;
        private Button bt_button2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_two);
            //找到控件
            tv_text2 = (TextView) findViewById(R.id.tv_text2);
            et_edittext2 = (EditText) findViewById(R.id.et_edittext2);
            bt_button2 = (Button) findViewById(R.id.bt_button2);
            //获得Activity1传递来的值
            Intent intent=getIntent();
            String str=intent.getStringExtra("kk");
            //显示Activity1传递来的值
            tv_text2.setText(str);
            
            
            bt_button2.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.bt_button2:
                String text=et_edittext2.getText().toString().trim();
                Intent intent=new Intent(this,MainActivity.class);
                intent.putExtra("ww", text);
                //用setResult返回数据
                setResult(1, intent);
                finish();
                break;

            default:
                break;
            }
            
        }

        

    }

  • 相关阅读:
    吴裕雄--天生自然--Go 语言学习笔记--Go 语言指针
    SVN报错“Failed to run the WC DB work queue associated with”解决办法
    【Oracle学习笔记】Oralce随机数与随机取样
    [CUDA]CUDA编程资源汇总
    [算法竞赛]目标检测常用技巧总结
    [CUDA]CUDA编程实战五——dot向量点积运算
    [CUDA]CUDA编程实战四——矩阵乘法
    [CUDA]CUDA编程实战三——矩阵加法的实现
    [CUDA]CUDA编程实战一——了解CUDA及获取GPU信息
    [CUDA]CUDA编程实战二——向量加法
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/6027773.html
Copyright © 2011-2022 走看看