2.1窗口跳转和数据传递

常用属性功能
text文本内容
textsize文本字号,单位:sp
textcolor文本颜色,#ff0000=红色
hint提示信息
singleLine单行(true or false)
background背景颜色或图片
layout_height高度,单位dp(wrap_conten,match_parent)
layout_weight宽度,单位dp(wrap_conten,match_parent)

三大控件

标签控件

在这里插入图片描述

输入对话框控件

在这里插入图片描述

按钮控件

在这里插入图片描述
在这里插入图片描述
Edit text和Button的爹是Textview

创建登录窗口

  • 创建userlogin应用
    在这里插入图片描述
  • 将背景图片添加在drawable中
    在这里插入图片描述
  • 基于模块创建登录窗口loginactivity
    在这里插入图片描述
  • 勾选Launcher activity(设为主窗口)
    在这里插入图片描述

修改布局,并添加标签,控件

  • 修改布局资源文件为线性布局
    在这里插入图片描述
  • 添加标签,控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:padding="15dp"
    android:background="@drawable/background"
    tools:context=".LoginActivity">
    <TextView
        android:id="@+id/tv_user_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user_login"
        android:textSize="25sp"
        android:textColor="@color/red"
        android:layout_marginBottom="30dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <TextView
            android:id="@+id/tv_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:text="@string/username"
            />
        <EditText
            android:id="@+id/et_username"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:singleLine="true"
            android:hint="@string/input_username"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="@color/black"
            android:text="@string/password"
            />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:singleLine="true"
            android:inputType="textPassword"
            android:hint="@string/input_password"
            />
    </LinearLayout>
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="@string/login"
            android:textSize="20sp"/>
        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="@string/cancel"
            android:textSize="20sp"/>

    </LinearLayout>


</LinearLayout>

修改loginactivity

  • 声明变量
    在这里插入图片描述
  • 通过资源标识符获取控件事例
    在这里插入图片描述
  • 设置登录按钮监听器
    在这里插入图片描述
    -给取消按钮设置监听器

在这里插入图片描述

  • 创建显示意图和数据包,通过数据包封装数据
    在这里插入图片描述

利用意图启动组件

显示意图

1、方法一

1、 Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
2、 startActivity(intent);

2、方法二

1、 Intent intent = new Intent();
2、 intent.setClass(FirstActivity.this, SecondActivity.class);
3、 startActivity(intent);

3、 方法三

1、Intent intent = new Intent();
2、ComponentName component = new ComponentName(FirstActivity.this, SecondActivity.class);
3、intent.setComponent(component);
4、startActivity(intent);

利用意图传递数据

(一)传递单项数据

  • 在起始组件通过意图传递单项数据
// 通过意图传递单项数据
intent.putExtra("username", strUsername);
intent.putExtra("password", strPassword);
  • 在目标组件通过意图获取单项数据
// 通过意图获取单项数据
String username = intent.getStringExtra("username");
String password = intent.getStringExtra("password");

(二)传递数据包

1、在起始组件通过意图携带数据包

// 创建数据包,封装数据
Bundle data = new Bundle();
data.putString("username", strUsername);
data.putString("password", strPassword);
// 通过意图携带数据包
intent.putExtras(data);

2、在目标组件通过意图获取数据包

// 获取意图携带的数据包
Bundle data = intent.getExtras();
// 从数据包里按键取值获取各项数据
String username = data.getString("username");
String password = data.getString("password");

修改mainactivity

  • (loginactivity中采用的是利用数据包传递数据)通过意图获取数据包,然后从数据包里按键名取得各项数据
    在这里插入图片描述

结果

  • 输入账号密码
    在这里插入图片描述
  • 登录成功
    在这里插入图片描述

练习任务(完成用户注册功能)

创建用户注册项目

在这里插入图片描述

创建Registeractivity

  • 基于模板创建
    在这里插入图片描述
  • 修改名称,勾选Launcher Activity(设为启动窗口)
    在这里插入图片描述

修改主窗口布局资源

  • 修改成线性布局并添加id
    在这里插入图片描述

string.xml文件

<resources>
    <string name="app_name">试题三:用户注册</string>
    <string name="name">姓名:</string>
    <string name="sex">性别:</string>
    <string name="age">年龄:</string>
    <string name="phone">电话:</string>
    <string name="email">邮箱:</string>
    <string name="homepage">主页:</string>
    <string name="bei">备注:</string>
    <string name="zhuce">注册</string>
    <string name="cancle">取消</string>
</resources>

修改注册窗口

  • 添加文本框,按钮等
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".RegisterActivity"
    android:padding="40dp"
    android:orientation="vertical"
    android:background="@color/magenta">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textSize="20sp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/et_name"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/tv_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sex"
            android:textSize="20sp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/et_sex"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/age"
            android:textSize="20sp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/et_age"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/phone"
            android:textSize="20sp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/et_phone"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/tv_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/email"
            android:textSize="20sp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/et_email"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dp">
        <TextView
            android:id="@+id/tv_homepage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/homepage"
            android:textSize="20sp"
            android:textColor="@color/black"/>
        <EditText
            android:id="@+id/et_homepage"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="20sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">
        <TextView
            android:id="@+id/tv_bei"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/bei"
            android:textSize="20sp"
            android:textColor="@color/black"/>
    </LinearLayout>
    <EditText
        android:id="@+id/et_bei"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textSize="20sp"
        android:layout_marginLeft="60dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <Button
            android:id="@+id/btn_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/zhuce"
            android:layout_marginRight="20dp"
            android:background="@color/silver"/>
        <Button
            android:id="@+id/btn_cancle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancle"
            android:background="@color/silver"/>
    </LinearLayout>


</LinearLayout>

修改registeractivity

package net.ls.userregister;

import androidx.appcompat.app.AppCompatActivity;

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

public class RegisterActivity extends AppCompatActivity {
    //声明变量
    private EditText etname;
    private EditText etsex;
    private EditText etage;
    private EditText etphone;
    private EditText etemail;
    private EditText ethomepage;
    private EditText etbei;
    private Button btzhuce;
    private Button btcancle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //通过id获取值
        setContentView(R.layout.activity_register);
        etname = findViewById(R.id.et_name);
        etsex =findViewById(R.id.et_sex);
        etage = findViewById(R.id.et_age);
        etphone = findViewById(R.id.et_phone);
        etemail =findViewById(R.id.et_email);
        ethomepage = findViewById(R.id.et_homepage);
        etbei = findViewById(R.id.et_bei);
        btzhuce = findViewById(R.id.btn_register);
        btcancle = findViewById(R.id.btn_cancle);
        btzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取编辑框数据
                String strname = etname.getText().toString().trim();
                String strsex = etsex.getText().toString().trim();
                String strage = etage.getText().toString().trim();
                String strphone = etphone.getText().toString().trim();
                String stremail = etemail.getText().toString().trim();
                String strhomepage = ethomepage.getText().toString().trim();
                String strbei = etbei.getText().toString().trim();
                //吐司
                Toast.makeText(RegisterActivity.this,"恭喜,注册成功",Toast.LENGTH_LONG).show();
                //创建显示意图
                Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                //创建数据包,封装数据
                Bundle data = new Bundle();
                data.putString("name",strname);
                data.putString("sex",strsex);
                data.putString("age",strage);
                data.putString("phone",strphone);
                data.putString("email",stremail);
                data.putString("homepage",strhomepage);
                data.putString("bei",strbei);
                intent.putExtras(data);
                //按照意图启动组件
                startActivity(intent);

            }
        });


    }
}

修改主界面mainactivity

public class MainActivity extends AppCompatActivity {

    private TextView tvmessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvmessage = findViewById(R.id.tv_message);

        Intent intent = getIntent();
        if (intent != null){
            Bundle data =intent.getExtras();
            String name = data.getString("name");
            String sex = data.getString("sex");
            String age = data.getString("age");
            String phone = data.getString("phone");
            String email = data.getString("email");
            String homepage = data.getString("homepage");
            String bei = data.getString("bei");
             String message="姓名:"+name+"\n性别:"+sex+"\n年龄:"+age+"\n电话号码:"+phone+"\n邮箱:"+email+"\n主页:"+homepage+"\n备注:"+bei;
             tvmessage.setText(message);
        }
    }
}

对主界面的布局资源进行优化

  • 设置字体颜色大小等
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="30dp">

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textColor="@color/blue"
        android:layout_marginBottom="20dp"
        />

</LinearLayout>

删除文件清单中mainactivity的意图过滤器

在这里插入图片描述

最终效果

在这里插入图片描述

  • 看来页面还有待优化
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值