移动开发技术(Android)——实验6 Activity的使用(Intent)

一、实验目的

  1. 理解Activity和Intent的基本概念;
  2. 掌握Activity的创建方法;
  3. 掌握使用Intent实现Activity之间的跳转和数据传递;
  4. 熟悉Activity生命周期状态及方法;
  5. 了解Activity中的任务栈,掌握Activity的4种启动模式。

二、实验内容

创建一个Android项目,项目名称为“shiyan0602_专业_×××(学生姓名)”,要求:

1.创建3个Activity

创建3个Activity,名称分别为MainActivity、InforSetActivity和ShowActivity,对应的布局文件名称分别为activity_main.xml、activity_inforset.xml和activity_show。
在这里插入图片描述

2.界面构成

① 主界面activity_main:实现用户登录

  • 3个文本框,分别用来显示“用户名”、“密码”、“用户详细信息”;
  • 2个编辑框,分别用来接收用户输入的用户名、密码;
  • 1个按钮,文本显示为“登录”。
    在这里插入图片描述
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/ET_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="17sp" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码"
            android:textSize="17sp" />

        <EditText
            android:id="@+id/ET_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPassword"
            android:textSize="17sp" />
    </TableRow>

    <Button
        android:id="@+id/BT_login"
        android:text="登录" />

    <TextView
        android:id="@+id/TV_show"
        android:gravity="center"
        android:text="此处显示信息" />

</TableLayout>

② activity_infor_set:实现用户信息设置

  • 4个文本框,分别用来显示“昵称”、“年龄”、 “性别选择”、“爱好选择”等提示信息;
  • 2个编辑框,用来接收用户输入的昵称和年龄;
  • 2个单选按钮表示性别;
  • 多个复选框表示爱好;
  • 1个下拉列表表示职业;
  • 3个按钮,文本显示分别为“保存”、“重置”、“返回”。
    在这里插入图片描述
【arrays.xml文件】
此文件应放在res——values文件夹下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="str_profession">
        <item>学生</item>
        <item>教师</item>
        <item>警察</item>
        <item>医生</item>
        <item>其他</item>
    </string-array>
</resources>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".InforSetActivity" >

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="昵称"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/ED_nicheng"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="20sp" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/ED_age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="20sp" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8px"
            android:text="性别"
            android:textSize="20sp" />

        <RadioGroup
            android:id="@+id/RG_sex"
            android:orientation="horizontal" >

            <RadioButton android:text="" />

            <RadioButton android:text="" />
        </RadioGroup>
    </TableRow>

    <TableRow
        android:layout_marginTop="8px"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8px"
            android:text="爱好"
            android:textSize="20sp" />

        <LinearLayout
            android:id="@+id/LinearLayout_hobby"
            android:layout_marginTop="3px" >

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="看书" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="跑步" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="音乐" />
        </LinearLayout>
    </TableRow>

    <TableRow android:layout_marginTop="8px" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="职业"
            android:textSize="20sp" />

        <Spinner

            android:id="@+id/SP_pro"
            android:entries="@array/str_profession" />
    </TableRow>

    <Button
        android:id="@+id/BT_save"
        android:text="保存" />

    <Button
        android:id="@+id/BT_reset"
        android:text="重置" />

    <Button
        android:id="@+id/BT_return"
        android:text="返回" />

</TableLayout>

③ activity_show:实现用户注册信息的显示

  • 5个文本框,分别用来显示用户提交的注册信息;
  • 1个按钮,文本显示为“返回”。
    在这里插入图片描述
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activityshow" >

    <TextView
        android:id="@+id/TV_show1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30px" />

    <TextView
        android:id="@+id/TV_show2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30px" />

    <TextView
        android:id="@+id/TV_show3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30px" />

    <TextView
        android:id="@+id/TV_show4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30px" />

    <TextView
        android:id="@+id/TV_show5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30px" />

    <Button

        android:id="@+id/BT_showReturn"
        android:text="返回" />

</TableLayout>

3.程序实现功能

  • 点击主界面中的“登录”按钮,若用户名和密码输入为学生姓名和学号,则跳转到InforSetActivity界面。
  • 点击InforSetActivity界面中的“保存”按钮,将用户输入的信息在activity_show界面的文本框中显示出来。
  • 点击InforSetActivity界面中的“重置”按钮,将用户输入的信息清空。
  • 点击InforSetActivity界面中的“返回”按钮,将用户输入的信息回传给MainActivity,并显示在MainActivity的第3个文本框(用户详细信息)中。
  • 点击activity_show界面中的“返回”按钮,将关闭activity_show界面,返回到InforSetActivity界面中。

1.MainActivity.java(对应activity_main.xml)

MainActivity.java】
对应activity_main.xml

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

public class MainActivity extends Activity {

	EditText ET_username,ET_password;
	Button BT_Main_Login;
	String Str_username,Str_password;
	TextView TV_show;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ET_username=(EditText) findViewById(R.id.ET_username);
		ET_password=(EditText) findViewById(R.id.ET_password);
		BT_Main_Login=(Button) findViewById(R.id.BT_login);
		TV_show=(TextView) findViewById(R.id.TV_show);

		BT_Main_Login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Str_username=ET_username.getText().toString();
				Str_password=ET_password.getText().toString();

				if(Str_username.equals("yjx")&&Str_password.equals("187427")){
					Intent intent = new Intent();
					intent.setClass(MainActivity.this, InforSetActivity.class);
					//setResult(RESULT_OK, intent);
					startActivityForResult(intent, 1);
				}
			}
		});


	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if(requestCode==1&&resultCode==RESULT_OK){
			TV_show.setText("昵称:"+data.getStringExtra("昵称")+
					"\n年龄:"+data.getStringExtra("年龄")+
					"\n性别:"+data.getStringExtra("性别")+
					"\n爱好:"+data.getStringExtra("爱好")+
					"\n职业:"+data.getStringExtra("职业"));
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

2.InforSetActivity.java(对应activity_infor_set.xml)

InforSetActivity.java】
(对应activity_infor_set.xml)

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class InforSetActivity extends Activity {

	EditText ET_nicheng,ET_age;
	RadioGroup RG_sex;
	LinearLayout LL_hobby;
	Spinner SP_pro;
	Button BT_save,BT_reset,BT_return;
	String Str_nicheng,Str_age,Str_sex,Str_hobby,Str_SP_pro;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_infor_set);

		ET_nicheng=(EditText) findViewById(R.id.ED_nicheng);
		ET_age=(EditText) findViewById(R.id.ED_age);

		RG_sex=(RadioGroup) findViewById(R.id.RG_sex);
		LL_hobby=(LinearLayout) findViewById(R.id.LinearLayout_hobby);
		SP_pro=(Spinner) findViewById(R.id.SP_pro);
		
		BT_save=(Button) findViewById(R.id.BT_save);
		BT_return=(Button) findViewById(R.id.BT_return);
		BT_reset=(Button) findViewById(R.id.BT_reset);

		BT_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Str_nicheng=ET_nicheng.getText().toString();
				Str_age=ET_age.getText().toString();
				for(int i=0;i<RG_sex.getChildCount();i++){
					RadioButton r = (RadioButton) RG_sex.getChildAt(i);
					if(r.isChecked()){
						Str_sex=r.getText().toString();
					}
				}
				Str_hobby="";
				for(int i=0;i<LL_hobby.getChildCount();i++){
					CheckBox c = (CheckBox) LL_hobby.getChildAt(i);
					if(c.isChecked()){
						Str_hobby+=c.getText().toString();
					}
				}
				Str_SP_pro=SP_pro.getSelectedItem().toString();
				Intent intent;
				intent = getIntent();
				intent.setClass(InforSetActivity.this,Activityshow.class);
				intent.putExtra("昵称", Str_nicheng);
				intent.putExtra("性别", Str_sex);
				intent.putExtra("年龄", Str_age);
				intent.putExtra("爱好", Str_hobby);
				intent.putExtra("职业", Str_SP_pro);
				setResult(RESULT_OK, intent);
				startActivity(intent);
			}
		});

		BT_return.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				finish();
			}
		});
		BT_reset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ET_nicheng.setText("");
				ET_age.setText("");
				RG_sex.clearCheck();
				for(int i=0;i<LL_hobby.getChildCount();i++){
					CheckBox c = (CheckBox) LL_hobby.getChildAt(i);
					c.setChecked(false);
				}
				SP_pro.setSelection(0);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.infor_set, menu);
		return true;
	}
}

3.Activityshow.java(对应activity_activityshow.xml)

Activityshow.java】
(对应activity_activityshow.xml)

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Activityshow extends Activity {

	TextView TV1,TV2,TV3,TV4,TV5;
	Button BT_ShowReturn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activityshow);

		TV1=(TextView) findViewById(R.id.TV_show1);
		TV2=(TextView) findViewById(R.id.TV_show2);
		TV3=(TextView) findViewById(R.id.TV_show3);
		TV4=(TextView) findViewById(R.id.TV_show4);
		TV5=(TextView) findViewById(R.id.TV_show5);

		Intent intent = getIntent();
		TV1.setText(intent.getStringExtra("昵称"));
		TV2.setText(intent.getStringExtra("年龄"));
		TV3.setText(intent.getStringExtra("性别"));
		TV4.setText(intent.getStringExtra("爱好"));
		TV5.setText(intent.getStringExtra("职业"));

		BT_ShowReturn=(Button) findViewById(R.id.BT_showReturn);
		BT_ShowReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activityshow, menu);
		return true;
	}
}
  • 6
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值