activity_main.xml

<TableLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="用户名:" />

<EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:ems="10" >

<requestFocus />
        </EditText>

</TableRow>
    <TableRow >
       <TextView />
        
    </TableRow>

<TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

<TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:text="密    码:" />

<EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:padding="3dp"
            android:inputType="textPassword" />

</TableRow>
    
    <TableRow >
        
               <TextView />
    </TableRow>
    
    <TableRow >
        <TextView />
        
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="startActivity" />

<TextView />
    </TableRow>
    
    <TableRow >
        
               <TextView />
    </TableRow>
    
    <TableRow >
        <TextView />
        
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="startActivityForResult" />

<TextView />
    </TableRow>

<TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</TableLayout>

activity_second.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SecondActivity" >

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="31dp"
        android:layout_marginTop="42dp"
        android:text="用户名:" />

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="24dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="" />

<TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:text="密    码:" />

<TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/textView2"
        android:text="" />

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="29dp"
        android:layout_toRightOf="@+id/textView3"
        android:text="返回" />

<TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="70dp"
        android:layout_toLeftOf="@+id/button1"
        android:text="" />

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Button" />

</RelativeLayout>

MainActivity.java

package com.example.login;

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

public class MainActivity extends Activity {

Button btn,btn2;
    
    EditText edtName,edtPass;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
        setTitle("登录");
        
        
        
        btn=(Button)findViewById(R.id.button1);
        
        btn.setOnClickListener( new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
                edtName=(EditText)MainActivity.this.findViewById(R.id.editText1);
                
                edtPass=(EditText)MainActivity.this.findViewById(R.id.editText2);
                
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                
                Bundle bundle = new Bundle();
                
                bundle.putString("action", "1");
                
                bundle.putString("name", edtName.getText().toString());
                
                bundle.putString("pass", edtPass.getText().toString());
                
                intent.putExtras(bundle);
                
                startActivity(intent);
                
                
                
            }
        });
        
        
        btn2=(Button)MainActivity.this.findViewById(R.id.button2);
        
        btn2.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    edtName=(EditText)MainActivity.this.findViewById(R.id.editText1);
                
                edtPass=(EditText)MainActivity.this.findViewById(R.id.editText2);
                
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                
                Bundle bundle = new Bundle();
                
                bundle.putString("action", "2");
                
                bundle.putString("name", edtName.getText().toString());
                
                bundle.putString("pass", edtPass.getText().toString());
                
                intent.putExtras(bundle);
                
                startActivityForResult(intent,0);
            }
        });

}

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        return true;
    }

/* (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        TextView tv=(TextView)this.findViewById(R.id.textView3);
        if(requestCode==0)
            if(resultCode==RESULT_OK)
            {
                tv.setText(data.getExtras().getString("ret"));
            }
    }

}

SecondActivity.java

package com.example.login;

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

public class SecondActivity extends Activity {

Button btn, btn2;

TextView tv1, tv2, tv5;

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

setTitle("主界面");

Bundle bundle = getIntent().getExtras();

String act = bundle.getString("action");

String name = bundle.getString("name");

String pass = bundle.getString("pass");

tv1 = (TextView) findViewById(R.id.textView2);

tv2 = (TextView) findViewById(R.id.textView4);

tv5 = (TextView) findViewById(R.id.textView5);

tv1.setText(name);

tv2.setText(pass);

tv5.setText(act);

btn = (Button) findViewById(R.id.button1);

btn.setOnClickListener(new View.OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(SecondActivity.this,
                        MainActivity.class);

startActivity(intent);
            }
        });

btn2 = (Button) findViewById(R.id.button2);

btn2.setOnClickListener(new View.OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
                Intent intent=getIntent();
                
                Bundle bundle = new Bundle();
                
                bundle.putString("ret", "from setresult");
                
                intent.putExtras(bundle);
                
                setResult(RESULT_OK, intent);
                
                finish();
            }
        });
    }

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }

}

Activity 间 bundle 传递参数的更多相关文章

  1. Android,使用Intent或Bundle传递参数,跳转页面。

    (1)使用Intent跳转页面: 第一个activity:MainActivity.java中: Intent myIntent = new Intent(); myIntent.putExtra(& ...

  2. 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限

     1  Broadcast是Android中的四大组件之一,他的用途很大,比如系统的一些广播:电量低.开机.锁屏等一些操作都会发送一个广播. 2  广播被分为两种不同的类型:"普通广播( ...

  3. Android零基础入门第83节:Activity间数据传递方法汇总

    在Activity间传递的数据一般比较简单,但是有时候实际开发中也会传一些比较复杂的数据,本节一起来学习更多Activity间数据的传递. 一.常用数据类型 在前面几节我们只学习了一些常用类型的数据传 ...

  4. activity间数据传递

    传递简单数据 创建两个activity,FirstActivity和TwoActivity,这里将会将数据从FisrtActivity传给TwoActivity. 创建完activity的目录界面如下 ...

  5. Android 用Intent和Bundle传递参数

    传递方: //点击btn_sub传递 fieldHeight.getText()和 fieldWeight.getText() private void setListeners()    {    ...

  6. Activity通过bundle传递数据

    从AActivity.java向BActivity.java传递数据: 建立AActivity.java文件建立bundle: 1 public class AActivity extends App ...

  7. 第一课android开发之在activity间传递参数

    一.活动间简单参数传递:1.在布局中添加按钮,用<Button,用id设置id名称,id="@+id/这儿填写你要设置成的名称":用text设置按钮上显示的文字.text=& ...

  8. 使用Bundle在Activity间传递数据

    使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...

  9. Activity间用Intent、Bundle、onActivityResult进行传值

    其实Activity间的传值就是通过Bundle,intent中也是自动生成了Bundle来传值,里面还有个onActivityResult()方法也可以传送数值. 如果一个Activity是由sta ...

随机推荐

  1. 利用QtGraphicalEffects来使得自己的图像显示更加生动

    有兴趣的开发人员能够參阅连接http://doc.qt.io/qt-5/qtgraphicaleffects-qmlmodule.html来深度学习Qt对Graphics方面的处理. 在今天的这篇文章 ...

  2. mysql_ado的demo

    winform程序 http://pan.baidu.com/s/1nvxm5br

  3. js 版本号

    在web项目开发过程中,我们经常会引用css.js文件,更新文件后常出现缓存问题(明明更改了代码,在浏览器上访问的时候却没有发生变化),这种情况我们通常采用以下两种解决方案: 1.手动清除浏览器缓存 ...

  4. C++关键字之friend

    原则上, 类的私有(private)和受保护(protected)成员不能从声明它们的同一类外部访问.但是, 此规则不适用于友元 "friends". 以friend关键字修饰的函 ...

  5. C#中Uri类的解释

    URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源.而URL是uniform resource locator,统一资源定位器,它是一种具体的UR ...

  6. Linux atop监控工具部署

    一.atop简介 atop是一款用于监控Linux系统资源与进程的工具,它以一定的频率记录系统的运行状态,所采集的数据包含系统资源(CPU.内存.磁盘和网络)使用情况和进程运行情况,并能以日志文件的方 ...

  7. Tomcat服务器的安装与配置

    安装 输入网址进入Tomcat的官网            在左边导航栏选择对应下载的版本            下载安装包形式             下载并解压到我们欲放入的目录中 配置      ...

  8. gulp报错160

    gulp报错: 这种提示,说明端口被占用,并且要改端口号,首先,我需要把Apache服务器关掉, 然后在gulpfile.js里: 把8080的端口号加进去.就解决了

  9. docker registry的https错误解决

    从docker1.3.2版本开始默认docker registry使用的是https,当你用docker pull 非https的docker regsitry的时候会报下面错误: Error: In ...

  10. c#基础 第八讲

    static void Main(string[] args) { while (true)//一直循环 { Random r = new Random();//创建随机函数r int[] caipi ...