Intent实现消息传递

布局文件--选择页面

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

   <TextView
      android:text="这是第一个Activity"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:textSize="24sp" />

   <TextView
      android:text="您的爱好是:"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/textView2"
      android:textSize="24sp" />

   <CheckBox
      android:text="唱歌"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/checkBox"
      android:textSize="20sp" />

   <CheckBox
      android:text="跳舞"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/checkBox2"
      android:textSize="20sp" />

   <CheckBox
      android:text="运动"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/checkBox3"
      android:textSize="20sp" />

   <CheckBox
      android:text="读书"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/checkBox4"
      android:textSize="20sp" />

   <Button
      android:text="提交"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/button"
      android:layout_gravity="center" />

</LinearLayout>

布局文件--展示页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical">
   <TextView
      android:text="这是第二个Activity"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView3"
      android:textSize="24sp" />
   <LinearLayout
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <TextView
         android:text="您的爱好是:"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/textView4"
         android:textSize="18sp" />
      <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/tx_aihao"
         android:layout_weight="1"
         android:textSize="18sp" />
   </LinearLayout>
   <Button
      android:text="返回"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/back"
      android:layout_gravity="center" />
</LinearLayout>

java文件--选择页面

package com.qst.chapter05.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import com.qst.chapter05.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Adminstrator on 
 */

public class Activity_1 extends AppCompatActivity {
   private CheckBox checkBox,checkBox2,checkBox3,checkBox4;
   private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
   private Button button;
   private String content = "";
   @Override
   protected void onCreate( Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_1);

      checkBox= (CheckBox) findViewById(R.id.checkBox);
      checkBox2= (CheckBox) findViewById(R.id.checkBox2);
      checkBox3= (CheckBox) findViewById(R.id.checkBox3);
      checkBox4= (CheckBox) findViewById(R.id.checkBox4);
      button= (Button) findViewById(R.id.button);

      // 添加到集合中
      checkBoxs.add(checkBox);
      checkBoxs.add(checkBox2);
      checkBoxs.add(checkBox3);
      checkBoxs.add(checkBox4);

      button.setOnClickListener(new View.OnClickListener() {


         public void onClick(View v) {
            Bundle bundle=new Bundle();
            int i=0;
            //将选中的喜好放到bundle中
            for (CheckBox cbx : checkBoxs) {
               if (cbx.isChecked()) {
                  bundle.putString("" + i, cbx.getText().toString());
                  i++;
               }
            }
            //喜好的个数也放到bundle中
            bundle.putInt("num",i);
            Intent intent=new Intent(Activity_1.this,Activity_2.class);
            intent.putExtras(bundle);
            startActivity(intent);
         }
      });

   }
   public void getValues(View v) {


      if ("".equals(content)) {
         content = "请您选择您的爱好";
      }
   }
}

 

java文件--展示页面

package com.qst.chapter05.activity;

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

import com.qst.chapter05.R;

/**
 * Created by Adminstrator on 2016/11/14.
 */

public class Activity_2 extends AppCompatActivity {
   private TextView tx;
   private Button bt;
   @Override
   protected void onCreate( Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_2);
      tx= (TextView) findViewById(R.id.tx_aihao);
      Intent intent=getIntent();
      //先获取用户的喜好个数
      int num=intent.getIntExtra("num",0);
      String str="";
      //遍历喜好的内容
      for (int i=0;i<num;i++){
         str+=intent.getStringExtra(""+i)+" ";
      }
      //显示喜好
      tx.setText(str);

      //初始化
      bt= (Button) findViewById(R.id.back);
      //注册事件
      bt.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            Intent intent=new Intent(Activity_2.this,Activity_1.class);
            //启动Activity
            startActivity(intent);
         }
      });
   }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值