移动应用开发


Activity组件通信

一、实验目的及要求:
(1) 掌握显示启动和隐式启动的方式
(2) 掌握Activity间的数据通信

二、实验内容及步骤任务:根据下述要求实现对应程序
1、 完成启动界面的设计,要求采用合理布局,使界面效果与图1所示结果保持一致。
( “男”单选按钮默认选中,学院下拉列表框的内容为:信息技术学院、外国语学院、机电学院、商学院、艺术设计学院、珠宝学院、新闻传播学院);左列标签名字体大小为24sp;点击“使用显示启动”按钮和“使用隐式启动”按钮,均能跳转至界面
2,如图2所示,前者使用显示启动方式,后者使用隐式启动方式,两种启动方式均将界面1中的数据传递至界面2中。
在这里插入图片描述

界面布局代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:text="学号:"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/cdy1"
        android:text="  "
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="姓名:"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <EditText
        android:id="@+id/cdy2"
        android:text="  "
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

<RadioGroup
    android:id="@+id/rg"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:text="性别:"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RadioButton
        android:id="@+id/rb1"
        android:text="男"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RadioButton
        android:id="@+id/rb2"
        android:text="女"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RadioGroup>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:text="学院"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Spinner
        android:id="@+id/sp1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:prompt="@string/info"
        android:entries="@array/xueyuan">

    </Spinner>

</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="专业:"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/cdy3"
        android:text="  "
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="班级:"
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/cdy4"
        android:text="  "
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/bt1"
        android:text="使用显示启动"
        android:textSize="22sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt2"
        android:text="隐式"
        android:textSize="22sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

JAVA代码:
package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Spinner spinner= (Spinner) findViewById(R.id.sp1);
    final EditText editText1=(EditText)findViewById(R.id.cdy1);
    final EditText editText2=(EditText)findViewById(R.id.cdy2);
    final EditText editText3=(EditText)findViewById(R.id.cdy3);
    final EditText editText4=(EditText)findViewById(R.id.cdy4);
    final Spinner spinner1=(Spinner)findViewById(R.id.sp1);
    String array[]={"信息技术学院","外国语学院","机电学院","商学院","艺术设计学院","珠宝学院","新闻传播学院"};
    ArrayAdapter<String>arrayAdapter1=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,array);
    spinner.setAdapter(arrayAdapter1);
    Button bt1=(Button)findViewById(R.id.bt1);
    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String string1 = editText1.getText().toString();
            String string2 = editText2.getText().toString();
            String string3 = editText3.getText().toString();
            String string4 = editText4.getText().toString();
            String string5 = spinner1.getSelectedItem().toString();
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rg);
            switch (radioGroup.getCheckedRadioButtonId()) {
                case R.id.rb1:
                    intent.putExtra("data6", "男");
                    break;
                case R.id.rb2:
                    intent.putExtra("data6", "女");
                    break;
            }
            intent.putExtra("data1", string1);
            intent.putExtra("data2", string2);
            intent.putExtra("data3", string3);
            intent.putExtra("data4", string4);
            intent.putExtra("data5", string5);

            startActivity(intent);


        }
    });//隐式


    Button bt2=(Button)findViewById(R.id.bt2);
    bt2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent =new Intent();
            //对应AndroidManifest.xml
            String string1 = editText1.getText().toString();
            String string2 = editText2.getText().toString();
            String string3 = editText3.getText().toString();
            String string4 = editText4.getText().toString();
           String string5=spinner1.getSelectedItem().toString();
            Intent intent1 = new Intent(MainActivity.this,Main2Activity.class);
            RadioGroup rg =(RadioGroup) findViewById(R.id.rg);
            switch(rg.getCheckedRadioButtonId()) {
                case R.id.rb1:
                    intent1.putExtra("data6", "男");
                    break;
                case R.id.rb2:
                    intent1.putExtra("data6", "女");
                    break;

            }


            intent1.putExtra("data1", string1);
            intent1.putExtra("data2", string2);
            intent1.putExtra("data3", string3);
            intent1.putExtra("data4", string4);
            intent1.putExtra("data5", string5);


            startActivity(intent1);
            intent1.setAction("text.example1.com");
            startActivity(intent1);
        }
    });

}
@Override
protected void onStart(){
    super.onStart();
    Log.i("main", "onstart");
}

@Override
protected void onRestart(){
    super.onRestart();
    Log.i("main","Restart");
}
@Override
protected void onResume(){
    super.onResume();
    Log.i("main","onResume");
}
@Override
protected void onPause(){
    super.onPause();
    Log.i("main","onPause");
}
@Override
protected void onStop(){
    super.onStop();
    Log.i("main","onStop");
}
@Override
protected void onDestroy() {
    super.onDestroy();
    Log.i("main", "onStop");
}

}

2、 点击界面1中的按钮后,跳转至界面2,将界面1中输入的内容传递至界面2的ListView中显示,效果如图2所示。(要求:列表文字大小为28sp,图片使用Vector格式,从上到下依次使用credit_card_black,account_box_black,wc_black,account_balance_black,school_black,assignment_ind_black)
在这里插入图片描述
界面布局代码:

<?xml version="1.0" encoding="utf-8"?>


Java代码:
package com.example.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main2Activity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    ListView listView = (ListView)findViewById(R.id.lvdata1);
    List<Map<String,Object>> lisetdata=new ArrayList<>();
    Map<String,Object> map1= new HashMap<>();
    Map<String,Object> map2= new HashMap<>();
    Map<String,Object> map3= new HashMap<>();
    Map<String,Object> map4= new HashMap<>();
    Map<String,Object> map5= new HashMap<>();
    Map<String,Object> map6= new HashMap<>();

    Intent intent = getIntent();
    map1.put("txt",intent.getStringExtra("data1"));
    map2.put("txt",intent.getStringExtra("data2"));
    map3.put("txt",intent.getStringExtra("data3"));
    map4.put("txt",intent.getStringExtra("data4"));
    map5.put("txt",intent.getStringExtra("data5"));
    map6.put("txt",intent.getStringExtra("data6"));
    map1.put("img",R.drawable.ic_3d_rotation_24dp);
    map2.put("img",R.drawable.ic_account_balance_24dp);
    map3.put("img",R.drawable.ic_assignment_late_24dp);
    map4.put("img",R.drawable.ic_credit_card_24dp);
    map5.put("img",R.drawable.ic_description_24dp);
    map6.put("img",R.drawable.ic_extension_24dp);
    lisetdata.add(map1);
    lisetdata.add(map2);
    lisetdata.add(map3);
    lisetdata.add(map4);
    lisetdata.add(map5);
    lisetdata.add(map6);


    SimpleAdapter simpleAdapter=new SimpleAdapter(Main2Activity.this,lisetdata,R.layout.listitern,new String[]{"txt","img"},new int[]{R.id.itemtxt,R.id.iteming});
    listView.setAdapter(simpleAdapter);


}

}

注册表代码:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity2">
        <intent-filter>
            <action android:name="test.example1.com"></action>
            <category android:name="android.intent.category.DEFAULT"></category>
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

在这里插入图片描述

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值