Activity间传递数据

小Demo

以下是使用Intent传递数据的小Demo

A.Activity

public class A extends Activity {
    EditText nameEdit;
    EditText pwdEdit;
    Button sentBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        nameEdit= (EditText )findViewById(R.id.user_edit);
        pwdEdit = (EditText )findViewById(R.id.pwd_edit); 
        sentBtn = (Button)findViewById(R.id.sent_btn); 
        initActivity();
    }

    public void initActivity() {

        sentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user_name, user_pwd;
                user_name = nameEdit.getText().toString().trim();
                user_pwd = pwdEdit.getText().toString().trim();
                if (user_name.equals("user") && user_pwd.equals("123456")) {
                    Intent intent = new Intent();
                    intent.setClass(A.this, B.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("user_name", user_name);
                    bundle.putString("user_pwd", user_pwd);
                    intent.putExtras(bundle);
                    startActivity(intent);

                } else {
                    Toast.makeText(A.this, "登录失败!", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

activity_a .xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp">

    <TextView
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号:"
        android:textSize="25sp"/>

    <EditText
        android:id="@+id/user_edit"
        android:layout_toRightOf="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:id="@+id/user_pwd"
        android:layout_below="@+id/user_name"
        android:layout_marginTop="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:textSize="25sp"/>

    <EditText
        android:id="@+id/pwd_edit"
        android:layout_below="@+id/user_edit"
        android:layout_toRightOf="@+id/user_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp" />

    <Button
        android:id="@+id/sent_btn"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_below="@+id/user_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"/>

</RelativeLayout>

B.Activity

public class B extends Activity {
    TextView showText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        showText = (EditText )findViewById(R.id.show_text);
        getActivity();
    }

    public void getActivity() {
        Intent intent = this.getIntent();
        String user_name = intent.getExtras().getString("user_name");
        String user_pwd = intent.getExtras().getString("user_pwd");

        showText.setText("用户:" + user_name + "登录成功!" + "且密码为:" + user_pwd);
    }

}

activity_b .xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/show_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="18sp" />

</RelativeLayout>

最后记得在AndroidManifest.xml文件中注册活动A和B

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值