Android Activity(二)Activity间的传值

MainActivity类:

package ccv.turbosnail.bo_intent_two;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private EditText one,two,three;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();     //初始化控件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();      //进行传值处理
            }
        });
    }

    private void getData() {
        String one_string = one.getText().toString().trim();      //得到输入框的值
        String two_string = two.getText().toString().trim();
        String three_string = three.getText().toString().trim();

        Intent intent = new Intent(MainActivity.this,TwoActivity.class);    //参数一:所在类,参数二:要传递的类
        // 单个变量传值
        intent.putExtra("one",""+one_string);      //这里加双引号值是为了让它被认为是字符串,其没用任何作用
        intent.putExtra("two",""+two_string);
        intent.putExtra("three",""+three_string);

        // 传递数组
        intent.putExtra("shuzu",new String[]{"上课","学习","玩游戏"});

        //传递集合
        ArrayList<String> list = new ArrayList<String>();
        list.add("撩妹");
        list.add("坚持");
        list.add("到手");
        intent.putStringArrayListExtra("jihe",list);


        startActivity(intent);      //启动intent
    }

    private void initView() {
        one = findViewById(R.id.edt_one);
        two = findViewById(R.id.edt_two);
        three = findViewById(R.id.edt_three);
        button = findViewById(R.id.btn_button);
    }
}

TwoActivity 类:


package ccv.turbosnail.bo_intent_two;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
public class TwoActivity extends AppCompatActivity {

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

        Intent intent = getIntent();        //得到传递过来的intent
        // 接收单个值
        String one = intent.getStringExtra("one");      //第一个参数是key,第二个参数是没有值时,默认值就为0
        int one_int = Integer.parseInt(one);             //将字符串转为整型

        String two = intent.getStringExtra("two");

        String three = intent.getStringExtra("three");
        float three_float = Float.parseFloat(three);     //将字符串转为浮点型

        // 接收数组值
        String[] shuzuArray = intent.getStringArrayExtra("shuzu");
        String s = Arrays.toString(shuzuArray);

        // 接受集合值
        ArrayList<String > list = intent.getStringArrayListExtra("jihe");
        String str = Arrays.toString(list.toArray());

        TextView mone = findViewById(R.id.tv_one);
        TextView mtwo = findViewById(R.id.tv_two);
        TextView mthree = findViewById(R.id.tv_three);
        TextView mfour = findViewById(R.id.tv_four);
        TextView mfive = findViewById(R.id.tv_five);
        mone.setText(""+one_int);               //这里加双引号值是为了让它被认为是字符串,其没用任何作用
        mtwo.setText(""+two);
        mthree.setText(""+three_float);
        mfour.setText(""+s);
        mfive.setText(""+str);

    }
}

布局代码activity_main:

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="15dp"
        android:text="1.请问你今年几岁了?"/>

    <EditText
        android:id="@+id/edt_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#03bbfd"
        android:textColor="#fff"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:hint="请输入年龄"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:textSize="15dp"
        android:text="2.请大声告诉我你喜欢的女孩是谁?"/>

    <EditText
        android:id="@+id/edt_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#03bbfd"
        android:textColor="#fff"
        android:textSize="20dp"
        android:hint="请输入你喜欢的女孩"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginTop="20dp"
        android:textSize="15dp"
        android:text="3. 52.06 % 10 = ?"/>

    <EditText
        android:id="@+id/edt_three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textColor="#fff"
        android:background="#03bbfd"
        android:textSize="20dp"
        android:hint="请输入答案"/>

    <Button
        android:id="@+id/btn_button"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:background="#03bbfd"
        android:layout_gravity="center"
        android:textSize="20dp"
        android:text="提交"/>
</LinearLayout>

布局代码:activity_Two:


<?xml version="1.0" encoding="utf-8"?>
<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=".TwoActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="15dp"
        android:text="1.请问你喜欢吃什么?"/>

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#03bbfd"
        android:textColor="#fff"
        android:textSize="18dp"
        android:padding="10dp"
        android:text="请输入年龄"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:textSize="15dp"
        android:text="2.请大声告诉我你喜欢的女孩是谁?"/>

    <TextView
        android:id="@+id/tv_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:background="#03bbfd"
        android:textColor="#fff"
        android:textSize="18dp"
        android:text="请输入你喜欢的女孩"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:textSize="15dp"
        android:text="3. 52.06 % 10 = ?"/>

    <TextView
        android:id="@+id/tv_three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#03bbfd"
        android:textColor="#fff"
        android:padding="10dp"
        android:textSize="18dp"
        android:text="请输入答案"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:textSize="15dp"
        android:text="4.数组是:"/>

    <TextView
        android:id="@+id/tv_four"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#03bbfd"
        android:textColor="#fff"
        android:padding="10dp"
        android:textSize="18dp"
        android:text="请输入答案"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:textSize="15dp"
        android:text="5. 集合是:"/>

    <TextView
        android:id="@+id/tv_five"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#03bbfd"
        android:textColor="#fff"
        android:padding="10dp"
        android:textSize="18dp"
        android:text="请输入答案"/>
</LinearLayout>

如果觉得我写的好的话,请关注我,你们的每一个关注,是我努力写下去最大的动力,也是最大的欣慰。谢谢你们的喜欢。!我们一起要加油啊!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王睿丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值