Android学习记录(七)_android if (namestr.equals("admin")&& passwordstr.-程序员宅基地

技术标签: android  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.用户名或者密码错误

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_46705517/article/details/112632321

智能推荐

Mysql区间查询的注意点_mysql check 区间-程序员宅基地

文章浏览阅读7.4k次。1. Description   最近在使用mysql区间查询的时候遇到的一个问题。在此简单记录以下SELECT * from table where 1 < id <100 ;这样查询会返回table表中所有的数据或者空数据,实际上的sql其实是SELECT * from table where 1; SELECT * from table where 0;2..._mysql check 区间

hwpf POI-程序员宅基地

文章浏览阅读214次。样式集StyleSheet styleSheet = hwpfDocument.getStyleSheet();字体集hwpfDocument.getFontTable().getMainFont(0)hwpfDocument.getFontTable().getAltFont(0)Times New RomanTimes New RomanSymbolSy..._poi stylesheet操作hwpf

微信小程序 多选框的使用_微信小程序多选框两列布局-程序员宅基地

文章浏览阅读8.1k次,点赞5次,收藏37次。微信小程序 多选框的使用需求需求![在这里插入图片描述](https://img-blog.csdnimg.cn/20181229092342945.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RzZngwNTE0MzVhZHNs,..._微信小程序多选框两列布局

初投稿:关于Volley请求的乱码处理-程序员宅基地

文章浏览阅读210次。在Volley请求中,我们会遇到通过Volley请求时返回的数据是乱码的,这个问题对于我来说非常地头疼,于是我到网上寻找合适的解决方案。终于发现一个比较简单的解决方法,只需一个方法就可以解决请求乱码了。现在,我就给大家分享一下关于Volley请求返回数据乱码处理,大家可以参考一下。protected Response<String> parseNetworkResponse(Netw...

TensorFlow1.2~2.1各个GPU版本CUDA和cuDNN对应版本整理_cuda对应的tensorflow-gpu2.0版本-程序员宅基地

文章浏览阅读1.2k次。要搭建TensorFlow的GPU版本,首先需要的必备条件就是一块能够支持CUDA的NVIDIA显卡,因为在搭建TensorFlow的GPU版本时,首先需要做的一件事就是安装其基础支持平台CUDA和其机器学习库cuDNN,然后在此基础上搭建TensorFlow GPU版本。其次还要了解一下不同的TensorFlow版本所需要对应安装的CUDA和cuDNN版本是多少,因为在TensorFlow的G..._cuda对应的tensorflow-gpu2.0版本

留言板v2.0(添加了一个简单登录功能php+mysql)_完成留言板首页indexc.php及登录页面login.html。-程序员宅基地

文章浏览阅读5.2k次,点赞7次,收藏20次。简述:在之前基础上添加了一个非常简单的登录功能,不涉及数据库,本地判断。第一步:建立数据库。(之前写过,在写一遍。)第二步:登录界面代码login.php 留言板登录 .center{text-align: center;} .login-page { width: 360px; padding: 8% 0 0; margin: auto; } _完成留言板首页indexc.php及登录页面login.html。

随便推点

正则判断字符串是否为数值(正数、负数、小数)_判断是否数值的正则-程序员宅基地

文章浏览阅读1w次,点赞3次,收藏6次。在用MapReduce进行处理业务时,发现HDFS中的数据有的列为汉字、有的列为字符串、有的列为正数、有的为负数、有的为小数,根据业务要求只有数字参与运算,因此首先清洗数据,用正则提取所有数值:public boolean isNumber(String str){ String reg = "^[-\\+]?([0-9]+\\.?)?[0-9]+$"; ret..._判断是否数值的正则

秦始皇陵-程序员宅基地

文章浏览阅读198次。秦始皇陵 2010年11月30日  风水传说    骊山以它特有的温泉和风景而闻名于世。西周末年的周幽王与爱妾褒姒曾在这里演出了一场兴起烽火戏诸侯的历史悲剧,从而葬送了西周王朝。相传秦始皇生前在骊山与神女相遇,游览当中欲戏神女,神女盛怒之下,朝他脸上唾了一口,秦始皇很快就长了一身的烂疮。虽然这是一个神话故事,但隐隐约约可以看出秦始皇与骊山似乎有些缘分。他的墓地也选在骊山之旁。秦始皇..._秦始皇陵背山面水左右各是什么

【vscode】vscode中文支持设置_如何让vscode支持注释中文-程序员宅基地

文章浏览阅读209次。1)打开vscode工具;2)使用快捷键组合【Ctrl+Shift+p】,在搜索框中输入“configure display language”,点击确定后;3)修改locale.json文件下的属性“locale”为“zh-CN”;4)重启vscode工具;如果重启后vscode菜单等仍然是英文显示,在商店查看已安装的插件,把中文插件重新安装一遍(如下图),然后在重启工具。在上图中商..._如何让vscode支持注释中文

STM8学习笔记---串口uart1_stm8串口发送字符串-程序员宅基地

文章浏览阅读1.4w次,点赞3次,收藏14次。使用uart1串口,需要用到stm8s_uart1.c和stm8s_uart1.h两个文件1、建立工程目录结构如下:2、编写uart.h文件如下:#ifndef __UART_H#define __UART_H#include "stm8s.h"#include "stm8s_clk.h"void USART_Configuration(void); //串口配置函数void UART_sen..._stm8串口发送字符串

X11 Wayland 及 Mir 比较_mir x11-程序员宅基地

文章浏览阅读1.1w次,点赞3次,收藏8次。MirCanonical 2013年3月宣布开发自己的显示服务器 Mir之后,引发了开源界的大量谴责,很多人指责Canonical为什么不采用被设计用来取代X11的Wayland,Wayland的开发者也表示Wayland完全能够满足Canonical的需求,指责Canonical搞分裂的行为。而 Canonical 则表示现在的 X 以及未来的 Wayland 无法满足未来横跨桌面、手机_mir x11

Linux 连接sftp 影响下载速度的因素_sftp速度受什么限制-程序员宅基地

文章浏览阅读5k次。  最近公司的一个项目需要使用sftp来下载文件到Linux服务器,然后再通过AES解密,RSA验签,解压等操作读取数据然后插入到数据库中。  公司的sftp部署到了公网上,刚开始传输小文件的时候。sftp都是正常,没有发现什么异常情况。但是当文件达到600M时候,sftp就卡着不动了。我自己电脑上跑项目下载速度还是可以的,大约10min就可以了。但是Linux中卡死了,下载不完。  首先分析..._sftp速度受什么限制