美文网首页
Intent实现消息传递

Intent实现消息传递

作者: shellblock | 来源:发表于2021-04-12 09:50 被阅读0次

XML布局文件的开发

main.xml

main.xml 效果图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_vertical|center_horizontal" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="个人信息完善" 
        android:textSize="40dp"
        android:gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_marginTop="20dp"
        android:layout_height="40dp"
        android:gravity="center_horizontal">
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textSize="30dp"
        android:text="姓名" 
        android:gravity="center_horizontal|center_vertical"/>
        <EditText
            android:id="@+id/etName"
            android:layout_width="200dp"
            android:layout_height="fill_parent"/>
    </LinearLayout>
    <Button
        android:id="@+id/btChooseHobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="选择自己的爱好"/>
    <TextView
        android:id="@+id/tvDisplayHobby"
        android:layout_width="300dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textSize="30dp"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
    <Button
        android:id="@+id/btChangeHobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="修改自己的爱好"
        android:visibility="gone"/>
    <Button
        android:id="@+id/btChooseTag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="选择自己的标签"/>
    <TextView
        android:id="@+id/tvDisplayTag"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="30dp"
        android:layout_marginBottom="10dp"
        android:visibility="gone"/>
    <Button
        android:id="@+id/btChangeTag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="修改自己的标签"
        android:visibility="gone"/>
    <LinearLayout 
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <Button
        android:id="@+id/btFinish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="完成"/>
        <Button
        android:id="@+id/btCancel"
        android:layout_marginLeft="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="取消"/>
    </LinearLayout>
</LinearLayout>

second.xml

second.xml 效果图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_vertical|center_horizontal" >
    <TextView
        android:id="@+id/tvSecondHead"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="您好,请选择您的爱好:" 
        android:textSize="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center_horizontal"/>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chkRead"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="看小说" />
        <CheckBox
            android:id="@+id/chkPlayGame"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="玩游戏" />
        <CheckBox
            android:id="@+id/chkSing"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="唱歌" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chkFootball"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="踢足球" />
        <CheckBox
            android:id="@+id/chkBasketball"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="打篮球" />
        <CheckBox
            android:id="@+id/chkVolleyball"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="打排球" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chkSwimming"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="游泳" />
        <CheckBox
            android:id="@+id/chkFitness"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="健身" />
        <CheckBox
            android:id="@+id/chkHeight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="登高" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chkQuiety"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="静坐" />
        <CheckBox
            android:id="@+id/chkChat"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="聊天" />
        <CheckBox
            android:id="@+id/chkPonder"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="思考" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center_horizontal"
        >
         <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="其它" 
        android:gravity="center_horizontal|center_vertical"/>
        <EditText
            android:id="@+id/etOtherHobby"
            android:layout_width="200dp"
            android:layout_height="fill_parent"/>
    </LinearLayout>
    <Button 
        android:id="@+id/btHobbyFinish"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="提交"
        />
</LinearLayout>

third.xml

third.xml 效果图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_vertical|center_horizontal" >
    <TextView
        android:id="@+id/tvThirdHead"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginBottom="20dp"
        android:gravity="center_horizontal"/>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chk90s"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="90后" />
        <CheckBox
            android:id="@+id/chk80s"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="80后" />
        <CheckBox
            android:id="@+id/chk00s"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="00后" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chkPersonality"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="个性" />
        <CheckBox
            android:id="@+id/chkPhilosophy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="哲理" />
        <CheckBox
            android:id="@+id/chkAestheticism"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="唯美" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chkFunny"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="搞笑" />
        <CheckBox
            android:id="@+id/chkloneliness"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="孤独" />
        <CheckBox
            android:id="@+id/chkSharp"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="犀利" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        >
         <CheckBox
            android:id="@+id/chkMemory"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="回忆" />
        <CheckBox
            android:id="@+id/chkAlternative"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="非主流" />
        <CheckBox
            android:id="@+id/chkCampus"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:text="校园" />
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center_horizontal"
        >
         <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="其它" 
        android:gravity="center_horizontal|center_vertical"/>
        <EditText
            android:id="@+id/etOtherTag"
            android:layout_width="200dp"
            android:layout_height="fill_parent"/>
    </LinearLayout>
    <Button 
        android:id="@+id/btTagFinish"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="提交"
        />
</LinearLayout>

Java文件的开发

IntentActivity.java

package com.example.demo;

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

public class IntentActivity extends Activity {
    //界面TextView的声明
    TextView tvDisplayHobby,tvDisplayTag;
    //界面按键的声明
    Button btChooseHobby,btChangeHobby,btChooseTag,btChangeTag;
    //启动第二个Activity的请求码
    private static final int SECOND_REQUESTCODE=2;
    //启动第三个Activity的请求码
    private static final int THIRD_REQUESTCODE=3;
    //启动Activity的Intent的声明
    Intent second_intent,third_intent;
    //启动Activity时,是点击哪个按钮启动的,通过一个整型值来标识,1代表选择按钮,2代表修改按钮
    int second_identification,third_identification;
    //界面文本编辑框的声明
    EditText etName;
    //界面文本编辑框内容的声明
    String etName_string;
    //选择信息保存
    String hobby_number,tag_number,etOtherHobby_string,etOtherTag_string;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //调用自定义初始化方法
        setup();
    }
    //自定义初始化方法
    private void setup(){
        //界面TextView的初始化
        tvDisplayHobby=(TextView)findViewById(R.id.tvDisplayHobby);
        tvDisplayTag=(TextView)findViewById(R.id.tvDisplayTag);
        //界面按键的初始化
        btChooseHobby=(Button)findViewById(R.id.btChooseHobby);
        btChangeHobby=(Button)findViewById(R.id.btChangeHobby);
        btChooseTag=(Button)findViewById(R.id.btChooseTag);
        btChangeTag=(Button)findViewById(R.id.btChangeTag);
        //启动Activity的Intent的初始化
        //显示启动
        second_intent = new Intent(this,SecondActivity.class);
        //隐式启动
        third_intent=new Intent();
        //界面文本编辑框的初始化
        etName=(EditText)findViewById(R.id.etName);
        //界面文本编辑框内容的初始化
        etName_string=etName.getText().toString().trim();
        //Activity的整型标识符的初始化,初始值为1
        second_identification=1;
        third_identification=1;
        //选择信息保存初始化
        hobby_number="";
        tag_number="";
        etOtherHobby_string="";
        etOtherTag_string="";
    }
    //自定义判断字符串内容是否为空的方法
    private boolean JudgeStringEmpty(String s){
        s=s.trim();
        if("".equals(s)||s==null)
            return true;
        return false;
    }
    //按钮的点击事件
    public void doClick(View v){
        //取消按钮和其它按钮不同,无需判断姓名是否输入
        if(v.getId()==R.id.btCancel){
            etName.setText("");
            btChooseHobby.setVisibility(View.VISIBLE);
            tvDisplayHobby.setVisibility(View.GONE);
            btChangeHobby.setVisibility(View.GONE);
            btChooseTag.setVisibility(View.VISIBLE);
            tvDisplayTag.setVisibility(View.GONE);
            btChangeTag.setVisibility(View.GONE);
            //重新初始化一下
            setup();
            return;
        }
        //首先判断是否输入了姓名,如果输入了,继续,否则提示输入姓名
        etName_string=etName.getText().toString().trim();
        if(JudgeStringEmpty(etName_string)){
            Toast.makeText(this,"请先输入姓名",2000).show();
            return;
        }
        switch(v.getId()){
        case R.id.btChooseHobby:
            //将整型标识符,姓名放入Intent,启动第二个Activity
            second_identification=1;
            second_intent.putExtra("second_identification", second_identification);
            second_intent.putExtra("etName_string", etName_string);
            startActivityForResult(second_intent, SECOND_REQUESTCODE);
            break;
        case R.id.btChangeHobby:
            //通过修改键进入爱好选择界面
            second_identification=2;
            second_intent = new Intent(this,SecondActivity.class);
            second_intent.putExtra("second_identification", second_identification);
            second_intent.putExtra("etName_string", etName_string);
            second_intent.putExtra("hobby_number", hobby_number);
            second_intent.putExtra("etOtherHobby_string", etOtherHobby_string);
            startActivityForResult(second_intent, SECOND_REQUESTCODE);
            break;
        case R.id.btChooseTag:
            //通过添加键进入标签选择界面
            third_identification=1;
            third_intent.setAction("com.xunfang.action");
            third_intent.addCategory("com.xunfang.category");
            third_intent.putExtra("third_identification", third_identification);
            third_intent.putExtra("etName_string", etName_string);
            startActivityForResult(third_intent, THIRD_REQUESTCODE);
            break;
        case R.id.btChangeTag:
            //通过修改键进入标签选择界面
            third_identification=2;
            third_intent = new Intent();
            third_intent.setAction("com.xunfang.action");
            third_intent.addCategory("com.xunfang.category");
            third_intent.putExtra("third_identification", third_identification);
            third_intent.putExtra("etName_string", etName_string);
            third_intent.putExtra("tag_number", tag_number);
            third_intent.putExtra("etOtherTag_string", etOtherTag_string);
            startActivityForResult(third_intent, THIRD_REQUESTCODE);
            break;
        case R.id.btFinish:
            if("".equals(hobby_number)){
                Toast.makeText(this,"请选择您的爱好",2000).show();
                return;
            }
            if("".equals(tag_number)){
                Toast.makeText(this,"请选择您的标签",2000).show();
                return;
            }
            Toast.makeText(this,"个人信息完善成功",2000).show();
            break;
        }
    }
    //获取到Activity的返回值后的处理方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        switch(requestCode){
        case SECOND_REQUESTCODE:
            //隐藏选择爱好按钮,显示爱好界面和修改爱好按钮
            btChooseHobby.setVisibility(View.GONE);
            tvDisplayHobby.setVisibility(View.VISIBLE);
            btChangeHobby.setVisibility(View.VISIBLE);
            tvDisplayHobby.setText(data.getStringExtra("hobby_string"));
            hobby_number=data.getStringExtra("hobby_number");
            etOtherHobby_string=data.getStringExtra("etOtherHobby_string");
            break;
        case THIRD_REQUESTCODE:
            btChooseTag.setVisibility(View.GONE);
            tvDisplayTag.setVisibility(View.VISIBLE);
            btChangeTag.setVisibility(View.VISIBLE);
            tvDisplayTag.setText(data.getStringExtra("tag_string"));
            tag_number=data.getStringExtra("tag_number");
            etOtherTag_string=data.getStringExtra("etOtherTag_string");
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

SecondActivity.java

package com.example.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SecondActivity extends Activity {
    //界面复选框的声明
    CheckBox chkRead,chkPlayGame,chkSing,chkFootball,chkBasketball,chkVolleyball,chkSwimming,chkFitness,chkHeight,chkQuiety,chkChat,chkPonder;
    //界面EditText的声明
    EditText etOtherHobby;
    //EditText的内容储存于字符串中
    String etOtherHobby_string;
    //界面TextView的声明
    TextView tv;
    //标识是通过第一个Activity的哪个按钮启动的的整型值的声明
    int identification;
    //启动的Intent的声明
    Intent intent;
    //用于标识选择信息的字符串
    String hobby_string,hobby_number;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        //调用自定义的初始化方法
        setup();
    }
    //自定义的初始化方法
    private void setup(){
        //界面复选框的初始化
        chkRead=(CheckBox)findViewById(R.id.chkRead);
        chkPlayGame=(CheckBox)findViewById(R.id.chkPlayGame);
        chkSing=(CheckBox)findViewById(R.id.chkSing);
        chkFootball=(CheckBox)findViewById(R.id.chkFootball);
        chkBasketball=(CheckBox)findViewById(R.id.chkBasketball);
        chkVolleyball=(CheckBox)findViewById(R.id.chkVolleyball);
        chkSwimming=(CheckBox)findViewById(R.id.chkSwimming);
        chkFitness=(CheckBox)findViewById(R.id.chkFitness);
        chkHeight=(CheckBox)findViewById(R.id.chkHeight);
        chkQuiety=(CheckBox)findViewById(R.id.chkQuiety);
        chkChat=(CheckBox)findViewById(R.id.chkChat);
        chkPonder=(CheckBox)findViewById(R.id.chkPonder);
        //界面EditText的初始化
        etOtherHobby=(EditText)findViewById(R.id.etOtherHobby);
        etOtherHobby_string="";
        //用于标识选择信息的字符串的初始化
        hobby_string="爱好:";
        hobby_number="";
        //界面TextView的初始化
        tv=(TextView)findViewById(R.id.tvSecondHead);
        //获得intent
        intent = getIntent();
        identification=intent.getIntExtra("second_identification", 0);
        String string_head=intent.getStringExtra("etName_string");
        string_head+=",您好,请";
        //根据标识变化头部信息
        switch(identification){
        case 1:
            string_head+="选择";
            break;
        case 2:
            string_head+="修改";
            hobby_number=intent.getStringExtra("hobby_number");
            for(int i=0;i<hobby_number.length();i++){
                String substring = hobby_number.substring(i,i+1);
                if(substring!=null){
                    int j=Integer.valueOf(substring);
                    if(j==1){
                        switch(i){
                        case 0:
                            chkRead.setChecked(true);
                            break;
                        case 1:
                            chkPlayGame.setChecked(true);
                            break;
                        case 2:
                            chkSing.setChecked(true);
                            break;
                        case 3:
                            chkFootball.setChecked(true);
                            break;
                        case 4:
                            chkBasketball.setChecked(true);
                            break;
                        case 5:
                            chkVolleyball.setChecked(true);
                            break;
                        case 6:
                            chkSwimming.setChecked(true);
                            break;
                        case 7:
                            chkFitness.setChecked(true);
                            break;
                        case 8:
                            chkHeight.setChecked(true);
                            break;
                        case 9:
                            chkQuiety.setChecked(true);
                            break;
                        case 10:
                            chkChat.setChecked(true);
                            break;
                        case 11:
                            chkPonder.setChecked(true);
                            break;
                        case 12:
                            etOtherHobby.setText(intent.getStringExtra("etOtherHobby_string"));
                            break;
                        }
                    }
                }
            }
            break;
        }
        string_head+="您的爱好";
        tv.setText(string_head);
    }
    //界面按钮点击事件
    public void doClick(View v){
        switch(v.getId()){
        case R.id.btHobbyFinish:
            hobby_number="";
            if(chkRead.isChecked()){
                hobby_string+="看小说,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkPlayGame.isChecked()){
                hobby_string+="玩游戏,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkSing.isChecked()){
                hobby_string+="唱歌,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkFootball.isChecked()){
                hobby_string+="踢足球,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkBasketball.isChecked()){
                hobby_string+="打篮球,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkVolleyball.isChecked()){
                hobby_string+="打排球,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkSwimming.isChecked()){
                hobby_string+="游泳,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkFitness.isChecked()){
                hobby_string+="健身,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkHeight.isChecked()){
                hobby_string+="登高,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkQuiety.isChecked()){
                hobby_string+="静坐,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkChat.isChecked()){
                hobby_string+="聊天,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            if(chkPonder.isChecked()){
                hobby_string+="思考,";
                hobby_number+="1";
            }else{
                hobby_number+="0";
            }
            etOtherHobby_string=etOtherHobby.getText().toString().trim();
            if(etOtherHobby_string==null||"".equals(etOtherHobby_string)){
                if(hobby_string.length()==3){
                    Toast.makeText(this,"请至少选择或填写一个爱好", 2000).show();
                    return;
                }
                hobby_number+="0";
                hobby_string=hobby_string.substring(0,hobby_string.length()-1);
            }else{
                hobby_string+=etOtherHobby_string;
                hobby_number+="1";
            }
            //设置返回值
            Intent result = new Intent();
            result.putExtra("hobby_number",hobby_number);
            result.putExtra("hobby_string", hobby_string);
            result.putExtra("etOtherHobby_string", etOtherHobby_string);
            setResult(RESULT_OK, result);
            finish();
            break;
        }
    }
}

ThirdActivity.java

package com.example.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ThirdActivity extends Activity {
    //界面复选框的声明
        CheckBox chk90s,chk80s,chk00s,chkPersonality,chkPhilosophy,chkAestheticism,chkFunny,chkloneliness,chkSharp,chkMemory,chkAlternative,chkCampus;
        //界面EditText的声明
        EditText etOtherTag;
        //EditText的内容储存于字符串中
        String etOtherTag_string;
        //界面TextView的声明
        TextView tv;
        //标识是通过第一个Activity的哪个按钮启动的的整型值的声明
        int identification;
        //启动的Intent的声明
        Intent intent;
        //用于标识选择信息的字符串
        String tag_string,tag_number;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third);
            //调用自定义的初始化方法
            setup();
        }
        //自定义的初始化方法
        private void setup(){
            //界面复选框的初始化
            chk90s=(CheckBox)findViewById(R.id.chk90s);
            chk80s=(CheckBox)findViewById(R.id.chk80s);
            chk00s=(CheckBox)findViewById(R.id.chk00s);
            chkPersonality=(CheckBox)findViewById(R.id.chkPersonality);
            chkPhilosophy=(CheckBox)findViewById(R.id.chkPhilosophy);
            chkAestheticism=(CheckBox)findViewById(R.id.chkAestheticism);
            chkFunny=(CheckBox)findViewById(R.id.chkFunny);
            chkloneliness=(CheckBox)findViewById(R.id.chkloneliness);
            chkSharp=(CheckBox)findViewById(R.id.chkSharp);
            chkMemory=(CheckBox)findViewById(R.id.chkMemory);
            chkAlternative=(CheckBox)findViewById(R.id.chkAlternative);
            chkCampus=(CheckBox)findViewById(R.id.chkCampus);
            //界面EditText的初始化
            etOtherTag=(EditText)findViewById(R.id.etOtherTag);
            etOtherTag_string="";
            //用于标识选择信息的字符串的初始化
            tag_string="标签:";
            tag_number="";
            //界面TextView的初始化
            tv=(TextView)findViewById(R.id.tvThirdHead);
            //获得intent
            intent = getIntent();
            identification=intent.getIntExtra("third_identification", 0);
            String string_head=intent.getStringExtra("etName_string");
            string_head+=",您好,请";
            //根据标识变化头部信息
            switch(identification){
            case 1:
                string_head+="选择";
                break;
            case 2:
                string_head+="修改";
                tag_number=intent.getStringExtra("tag_number");
                for(int i=0;i<tag_number.length();i++){
                    String substring = tag_number.substring(i,i+1);
                    if(substring!=null){
                        int j=Integer.valueOf(substring);
                        if(j==1){
                            switch(i){
                            case 0:
                                chk90s.setChecked(true);
                                break;
                            case 1:
                                chk80s.setChecked(true);
                                break;
                            case 2:
                                chk00s.setChecked(true);
                                break;
                            case 3:
                                chkPersonality.setChecked(true);
                                break;
                            case 4:
                                chkPhilosophy.setChecked(true);
                                break;
                            case 5:
                                chkAestheticism.setChecked(true);
                                break;
                            case 6:
                                chkFunny.setChecked(true);
                                break;
                            case 7:
                                chkloneliness.setChecked(true);
                                break;
                            case 8:
                                chkSharp.setChecked(true);
                                break;
                            case 9:
                                chkMemory.setChecked(true);
                                break;
                            case 10:
                                chkAlternative.setChecked(true);
                                break;
                            case 11:
                                chkCampus.setChecked(true);
                                break;
                            case 12:
                                etOtherTag.setText(intent.getStringExtra("etOtherTag_string"));
                                break;
                            }
                        }
                    }
                }
                break;
            }
            string_head+="您的标签";
            tv.setText(string_head);
        }
        //界面按钮点击事件
        public void doClick(View v){
            switch(v.getId()){
            case R.id.btTagFinish:
                tag_number="";
                if(chk90s.isChecked()){
                    tag_string+="90后,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chk80s.isChecked()){
                    tag_string+="80后,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chk00s.isChecked()){
                    tag_string+="00后,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkPersonality.isChecked()){
                    tag_string+="个性,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkPhilosophy.isChecked()){
                    tag_string+="哲理,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkAestheticism.isChecked()){
                    tag_string+="唯美,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkFunny.isChecked()){
                    tag_string+="搞笑,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkloneliness.isChecked()){
                    tag_string+="孤独,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkSharp.isChecked()){
                    tag_string+="犀利,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkMemory.isChecked()){
                    tag_string+="回忆,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkAlternative.isChecked()){
                    tag_string+="非主流,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                if(chkCampus.isChecked()){
                    tag_string+="校园,";
                    tag_number+="1";
                }else{
                    tag_number+="0";
                }
                etOtherTag_string=etOtherTag.getText().toString().trim();
                if(etOtherTag_string==null||"".equals(etOtherTag_string)){
                    if(tag_string.length()==3){
                        Toast.makeText(this,"请至少选择或填写一个标签", 2000).show();
                        return;
                    }
                    tag_number+="0";
                    tag_string=tag_string.substring(0,tag_string.length()-1);
                }else{
                    tag_string+=etOtherTag_string;
                    tag_number+="1";
                }
                //设置返回值
                Intent result = new Intent();
                result.putExtra("tag_number",tag_number);
                result.putExtra("tag_string", tag_string);
                result.putExtra("etOtherTag_string", etOtherTag_string);
                setResult(RESULT_OK, result);
                finish();
                break;
            }
        }
   
}

相关文章

  • Intent实现消息传递

    XML布局文件的开发 main.xml second.xml third.xml Java文件的开发 Intent...

  • 利用Intent消息传递和其背后的事情

    利用Intent消息传递和其背后的事情 Intent的使用 显示调用 显示Intent直接指明要启动activit...

  • 子线程startActivity(),传递intent

    startActivity(Intent) 的底层实现是将 intent分解成任务,传递到mainLooper 轮...

  • Intent

    推荐阅读:Intent详解 Intent是消息传递对象,协助组件之间的通信,Intent不仅可用于应用程序之间,也...

  • 第十三章-- 一些技巧工具

    1.全局获取context的技巧 2.使用Intent传递对象 使用Intent传递对象有两种实现方式:Seria...

  • 8.7 Intent2

    Intent传递简单数据 Intent传递数组 Intent传递集合 Intent传递对象 1.将对象转换为Jso...

  • 关于Intent你知多少?

    什么是Intent? Intent是一个消息传递对象,可以使用它来启动其它应用组件。Intent使组件之间通信更加...

  • Android使用Intent在活动中穿梭

    Intent 是一个消息传递对象,Intent 可以通过多种方式促进组件之间的通信,Intent 分为两种类型:显...

  • Intent和他的过滤器

    什么是Intent intent是一个消息传递对象,可以使用它从其他应用组件请求操作。通常我们用intent来启动...

  • [Android][Intent]

    1.Intent的作用 Intent 是一个消息传递对象,您可以使用它从其他应用组件请求操作。简单来说Intent...

网友评论

      本文标题:Intent实现消息传递

      本文链接:https://www.haomeiwen.com/subject/vsvtlltx.html