Android studio学习 简单的数据传递

Android studio学习 简单的数据传递实现

Activity之间的数据传递

先创建一两Activity 用于两个Activity之间传递

主Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="用户注册"
        android:textAlignment="center"
        android:textSize="30dp"></TextView>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="text"
        android:hint="输入用户名"
        android:id="@+id/lastName"></EditText>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:inputType="textWebPassword"
        android:hint="请输入密码"
        android:id="@+id/password"
        ></EditText>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:text="确定"
            android:id="@+id/qur"></Button>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:text="取消"
            android:id="@+id/quxiao"></Button>

    </LinearLayout>



</LinearLayout>
第二个Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".login_Activity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="1dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:text="用户名"
            android:textSize="30dp"></TextView>
        <TextView
            android:layout_width="1dp"
            android:layout_weight="3"
            android:layout_height="50dp"
            android:id="@+id/Name"
            android:textSize="30dp"></TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="1dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:text="用户名"
            android:textSize="30dp"></TextView>
        <TextView
            android:layout_width="1dp"
            android:layout_weight="3"
            android:layout_height="50dp"
            android:id="@+id/mima"
            android:textSize="30dp"></TextView>
    </LinearLayout>

设置主页面后代码

package com.example.skypan.wordframe;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText editText1;
    EditText editText2;
    Button bt1;
    String[] str=new String[2];
    Bundle bundle=new Bundle();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    editText1=(EditText) findViewById(R.id.lastName);
    editText2=(EditText)findViewById(R.id.password);
    bt1=(Button)findViewById(R.id.qur);



//按钮监听事件
    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(MainActivity.this,login_Activity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });

    editText2.addTextChangedListener(new TextWatcher() {
        //监听EditText中的 变化(changed)    TextWatcher(  )文本监听者
//其有三个重写方法
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //文本改变之前
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //文本改变中

        }

        @Override
        public void afterTextChanged(Editable s) {
            //文本改变之后
            bundle.putString("password",s.toString());
			
        }
    });
	
        //Edit的监听事件
    editText1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
			
        }

        @Override
        public void afterTextChanged(Editable s) {
            bundle.putString("lastName",s.toString());
            //将数据传递到bundle上
        }
    });
    }



}

Intent中的方法

​ putExtra(); //方法利用new一个Bundle来传递数据

​ 我们也可以Bundle bundle=new Bundle();

将数据传递到里一个Activity

​ 方法:

​ ActivityA:Intent intent=new Intent(ActivityA.this,ActivityB.class);

​ Bundle bundle=new Bundle();

​ Bundle.putString(String key,value);

​ Intent.putExtras(bundle);

​ startActivity(intent);

​ //数据利用Intent将数据传输到跳转的Activity里

ActivityB:

​ Bundle bundle=getIntent().getExtras();

​ String name=Bundle.getString(“name”); //获取bundle数据vlue

​ Int join=bundle.getInt(key); //获取bundle数据的key

这里获取用bundle传递的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值