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;
            }
            
        }

        

    }

  • 相关阅读:
    Java堆外内存管理
    Java内存模型和JVM内存管理
    C++经典面试题(最全,面中率最高)
    115道Java经典面试题(面中率最高、最全)
    Sublime Text 3中文乱码问题的解决(最有效)
    面试笔记3
    IntelliJ IDEA使用教程(很全)
    Intellij IDEA 创建Web项目并在Tomcat中部署运行
    IDEA调试总结(设置断点进行调试)
    Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误
  • 原文地址:https://www.cnblogs.com/changyiqiang/p/6027773.html
Copyright © 2011-2022 走看看