Android学习记录(七)


在进行系统学习几天后,导师给了我们几个新的任务,需要我们在规定时间内完成。今天我的学习内容就是完成第一个任务-Activity页面跳转。

一.Activity

活动代表了一个具有用户界面的单一屏幕,如 Java 的窗口或者帧。Android 的活动是 ContextThemeWrapper 类的子类。
Activity所定义的回调如下表所示

回调 描述
onCreate() 这是第一个回调,在活动第一次创建时调用
onStart() 这个回调在活动为用户可见时被调用
onResume() 这个回调在应用程序与用户开始可交互的时候调用
onPause() 被暂停的活动无法接受用户输入,不能执行任何代码。当前活动将要被暂停,上一个活动将要被恢复时调用
onStop() 当活动不在可见时调用
onDestroy() 当活动被系统销毁之前调用
onRestart() 当活动被停止以后重新打开时调用

二.案例-实现用户登录界面

本次任务要求我们满足如下要求:
(1)在登录界面类里声明按钮控件变量btnLogin, btnCancel
(2)通过findViewById方法得到两个按钮的实例 (instance)
(3)利用setOnClickListener方法给两个按钮注册单击事件监听器
(4)实现单击事件监听器接口,采用匿名实现方式
(5)在接口的抽象方法里编写事件处理代码

实现步骤

(一)创建新项目–UserLogin

在这里插入图片描述
在这里插入图片描述

(二)添加背景图

向新建项目中添加你所喜欢的背景图
在这里插入图片描述

(三)创建登录窗口–LoginActivity

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

(四) 编写文件–activity_login.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center"
    android:padding="15dp"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvUserLogin"
        android:layout_marginBottom="30dp"
        android:text="@string/user_login"
        android:textColor="#ff00ff"
        android:textSize="25sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvUsername"
            android:text="@string/username"
            android:textColor="#000000"
            android:textSize="20sp"/>
        <EditText
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:id="@+id/edtUsername"
            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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvPassword"
            android:text="@string/password"
            android:textColor="#000000"
            android:textSize="20sp"/>

        <EditText
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_password"
            android:id="@+id/edtPassword"
            android:inputType="textPassword"
            android:singleLine="true"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnLogin"
            android:paddingRight="30dp"
            android:paddingLeft="30dp"
            android:text="@string/login"
            android:textSize="20sp"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnCancel"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="@string/cancel"
            android:textSize="20sp"/>
    </LinearLayout>

</LinearLayout>

(五) 编写文件–activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvMessage"
        android:textSize="25dp"
        android:textColor="#0000ff"/>

</LinearLayout>

(六)修改项目清单文件

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.nell.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/Theme.UserLogin">
        <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">
            <!--<intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

(七)修改字符串资源文件

在这里插入图片描述

<resources>
    <string name="app_name">UserLogin</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.nell.userlogin;

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

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);
        btnCancel = findViewById(R.id.btnCancel);
        btnLogin = findViewById(R.id.btnLogin);

        btnLogin.setOnClickListener(new View.OnClickListener(){
    
    

            @Override
            public void onClick(View v) {
    
    
                String strUsername = edtUsername.getText().toString().trim();
                String strPassword = edtPassword.getText().toString().trim();

                if (strUsername.equals("admin") && strPassword.equals("admin")){
    
    
                    Toast.makeText(LoginActivity.this,"恭喜,用户名与密码正确!",Toast.LENGTH_SHORT).show();;
                }else {
    
    
                    Toast.makeText(LoginActivity.this,"遗憾,用户名或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                finish();
            }
        });
    }
}

(九) 启动项目,查看结果

1.密码及用户名输入正确

在这里插入图片描述

2.用户名或密码不正确

在这里插入图片描述

三.利用意图启动组件

1.使用显示意图

此种方法的讲解,都需要假设拥有两个窗口,分别为:FirstActivity以及SecondActivity

方法一:

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

方法二:

Intent intent = new Intent();
intent.setClass(FirstActivity.this,SecondActivity.class);
startActivity(intent);

方法三:

Intent intent = new Intent();
ComponentName componen = new ComponentName(FirstActivity.this,SecondActivity.class);
intent.setComponent(component);
startActivity(intent);

2.使用隐式意图

方法一:在java代码中创建

Intent intent = new Intent();
intent.setAction("net.nell.ACTION_NEXT");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);

方法二:在项目清单文件中设置意图过滤器

<activity android:name="net.nell.SecondActivity">
	<intent-filter>
		<action android:name="net.nell.ACTION_NEXT"/>
		<category android:name="android.intent.category.DEFAULT"/>
	</intent-filter>
</activity>

四.修改用户登录程序

修改用户登录程序,在正确输入密码以及用户名后,跳转到主窗口,在主窗口显示用户名以及密码

实现方法

1.修改LoginActivity

在原有基础上创建上文中所提到的显示意图,利用意图进行数据的携带,并按照意图进行目标组件的启动
在这里插入图片描述

2.修改MainActivity

package net.nell.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){
    
    
            //获取意图携带数据
            String username = intent.getStringExtra("username");
            String password = intent.getStringExtra("password");
            //拼接用户信息
            String message = "登录成功!\n用户:"+ username +"\n密码"+password;
            //设置标签属性,显示用户信息
            tvMessage.setText(message);
        }
        }
    }
}

3.运行程序,查看结果

1.用户名以及密码正确

在这里插入图片描述

2.用户名或者密码错误

在这里插入图片描述
今天的学习任务大概就是以上内容,之所以在一天之内可以完成这么多内容,是由于在前几次的学习内容中我已经进行了一次该种项目的训练。明天的学习,相信也会让我的即能更进一步。

猜你喜欢

转载自blog.csdn.net/weixin_46705517/article/details/112632321