大二上-我的周总结03

  • 本周的学习生活依旧非常的丰富,各个学科都学到了一些新的东西,特别是安卓的,让我尝到了学习它的乐趣。
  • 本周的安卓学习了activity生命周期、事件处理及窗口的跳转还有线性布局。事件处理及窗口的跳转是真的让我对这门课程产生了极大的兴趣,因为作出来的效果都可以真真正正的在用户界面看到,所以这让我产生了一种成就感,原来我也可以做这么“厉害”的东西了,这给我继续学习安卓这门课程极大的信心。
    - activity生命周期
  • activity生命周期的话,Activity类提供六个核心回调方法:onCreate()、onStart()、onResume()、onPause()、onStop() 和 onDestroy()。当 Activity 进入新状态时,系统会调用其中每个回调方法。就是这些回调方法很重要,但是因为有一些目前来说本人用得并不多,所以有一些还没怎么太熟悉。在这里我插入一张华老师用过的图片来进行一下总结,可能这六个核心的回调方法就如下图一样,是相互对应的关系。

在这里插入图片描述

  • 为了更好地查看相应应用的调试信息,需要添加消息过滤器。下面这张图片就是创建的过程。

在这里插入图片描述
事件处理及窗口的跳转

  • 这里用一个案例来进行总结,在本案例中,对相应的按钮进行了相应的事件处理,并且,在登录成功之后实现了窗口的跳转。
  • 以下是相应的步骤
    1,创建一个新的项目
    在这里插入图片描述

在这里插入图片描述
2,编写相应的代码
activity_login的相应代码

<?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:background="@drawable/bg2"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="15dp"
    tools:context=".LoginActivity"
    >

    <TextView
        android:id="@+id/tvUserLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user_login"
        android:textColor="#ff0000"
        android:textSize="25sp"
        android:layout_marginBottom="30dp"/>

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

        <TextView
            android:id="@+id/tvUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username"
            android:textColor="#000000"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/edtUsername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_username"
            android:singleLine="true"/>

    </LinearLayout>

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

        <TextView
            android:id="@+id/tvPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password"
            android:textColor="#000000"
            android:textSize="20sp"/>

        <EditText
            android:id="@+id/edtPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_password"
            android:inputType="textPassword"
            android:singleLine="true"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/login"
            android:textSize="20sp"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:textColor="#ffffff"
            android:background="#04BE02"
            android:layout_marginRight="10sp"
            />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:textSize="20sp"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:textColor="#ffffff"
            android:background="#04BE02"
            />

    </LinearLayout>

</LinearLayout>

strings相应的代码

<resources>
    <string name="app_name">用户登录</string>
    <string name="user_login">用户登录</string>
    <string name="username">用户:</string>
    <string name="input_username">请输入用户名</string>
    <string name="password">密码:</string>
    <string name="input_password">请输入密码</string>
    <string name="login">登录</string>
    <string name="cancel">取消</string>
</resources>

LoginActivity相应的代码

package net.zll.userlogin;

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

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    private EditText edtUsername;
    private EditText edtPassword;
    private Button btnLogin;
    private Button btnCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源文件设置用户界面
        setContentView(R.layout.activity_login);
        //通过资源标识获得控件实例
        edtUsername=findViewById(R.id.edtUsername);
        edtPassword=findViewById(R.id.edtPassword);
        btnLogin=findViewById(R.id.btnLogin);
        btnCancel=findViewById(R.id.btnCancel);
        //给登录按钮注册监听器,实现监听器接口,编写事件处理方法
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //保存用户输入的数据:用户名与密码
                String strUsername=edtUsername.getText().toString().trim();
                String strPassword=edtPassword.getText().toString().trim();

                //判断用户名与密码是否正确(假定用户名与密码都是“admin”)
                if(strUsername.equals("admin")&&strPassword.equals("admin")){
                    //利用吐司提示用户登录成功
                    Toast.makeText(LoginActivity.this,"恭喜,用户名与密码正确!",Toast.LENGTH_LONG).show();
                    //创建显式意图(参数1:包上下文,参数2:目标组件)
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    //创建数据包,封装数据
                    Bundle data=new Bundle();
                    data.putString("username",strUsername);
                    data.putString("password",strPassword);
                    //通过意图携带数据包
                    intent.putExtras(data);
                    //按照意图启动目标组件
                    startActivity(intent);
                }else {
                    //利用吐司提示用户登录失败
                    Toast.makeText(LoginActivity.this,"遗憾,用户名或密码错误!",Toast.LENGTH_LONG).show();
                }
            }
        });
        //给取消按钮注册监听器,实现监听器接口,编写事件处理方法
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //关闭登录窗口
                finish();
            }
        });
        // 在活动栏上显示图标
        ActionBar actionBar=getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setLogo(R.mipmap.ic_launcher);
    }
}

activity_main相应的代码

<?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:gravity="center">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="#ff0000"/>

</LinearLayout>

MainActivity相应的代码

package net.zll.userlogin;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

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.tvMessage);
        //获取意图
        Intent intent=getIntent();
        //判断意图是否为空
        if(intent!=null){
            //获取意图携带的数据包
            Bundle data=intent.getExtras();
            //从数据包里按键取值获取各项数据
            String username=data.getString("username");
            String password=data.getString("password");
            //拼接用户信息
            String message="登录成功!\n\n用户:"+username+"\n密码:"+password+"\n\n欢迎您的到来!";
            //设置标签属性,显示用户信息
            tvMessage.setText(message);
        }
    }
}

最后修改AndroidManifest,让其首选启动项为LoginActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.zll.userlogin">

    <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=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

3,效果
在这里插入图片描述
在这里插入图片描述
总结一下,学这里的时候还是非常的快乐的,毕竟有实际的东西出来。这个事件处理和窗口的跳转的话不是很难,就是要做的东西比较的多,仔细一点的话是不会出太大的问题的。就是因为没怎么做过,所以还是有点不太熟练,但是我相信后面慢慢得就会熟练起来的。
线性布局
线性布局就是对项目中相应的东西进行布局,让它按照你想的位置安放,属于比较基础的内容,而已比较繁琐,理解相应的命令是怎么用得就行。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值