大家在使用app的时候一般都需要一个登录界面。
今天我就来实现一下登录界面的功能
1.界面布局1.新建一个空白界面Empity Activity
设置几个组件如图
看一下配置xml文件的code
这部分没有什么难点,基本的框架有了,然后通过代码把功能加上去。
这里说一下这个底图的实现,就是添加一个默认的ImageView组件,然后在 res>layout里粘贴上想放的图片,然后在对应的xml文件里找到组件的code然后添加代码Android:background@mipmap/文件名
2.界面跳转+3.信息传递Activity代码如下
package com.example.app01;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.app01.enroll.Enrollpage;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//(1)create two list to save the data of enroll name and password
public List userNameList = new ArrayList<>();
public List userPassList = new ArrayList();
//(2)initialize the list and put the default value of name and password
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add listener on button"login in "
//1.get the button by "findviewByid" method
//2.transfer the type into Button
Button button = (Button) this.findViewById(R.id.buttonlogin);
Button buttonEnroll = (Button) this.findViewById(R.id.buttonEnroll);
Button buttonEnter = (Button) this.findViewById(R.id.buttonenter);
// 3.create a clicklistener and put it on the object button
// 4. method "inner class"在实际开发中,我们常常遇到这样的情况:
// 一个接口/类的方法的某个实现方式在程序中只会执行一次,但为了使用它,
// 我们需要创建它的实现类/子类去实现/重写。此时可以使用匿名内部类的方式,
// 可以无需创建新的类,减少代码冗余。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//5.initial the object name and password
//notion: there "this" is not represent the class"MainActivity"
//in order to use the main class,we use"MainActivity.this" instead of "this"
//also , we need to transfer the object type.
EditText userName = (EditText) MainActivity.this.findViewById(R.id.editTextTextPersonName);
EditText userPassword = (EditText) MainActivity.this.findViewById(R.id.editTextTextPassword2);
//6.assign the String with the message we get
String name = userName.getText().toString();
String password = userPassword.getText().toString();
// we need to check if we get the correct message we need
//7. print the name and password
Log.i("MainActivity 1st check out line ","name ="+name+"nPassword= "+password);
int flag=0;
// userNameList.add("Timon");
// userNameList.add("123");
//
for (int i = 0; i
我这个代码有一些为了实现注册功能的冗余代码
主要是几个步骤
1.初始化按钮对象,通过静态内部类将监听器绑定在按钮上。
获取两个输入框的内容name和password。然后通过if的判断函数,判断登录成功与否。
这里积累一个用法。显示文字消息弹出提示。
Toast toast=Toast.makeText(MainActivity.this, "密码错误或账户不存在!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
这里可以修改参数设置信息显示的时间长短,也可以修改显示的位置。
然后如果密码正确,则要跳转的主页。
这里指的把用户名传到homepage然后显示XX,欢迎回来
if("Timon".equals(name)&&"123".equals(password)){
Intent intent1 = new Intent(MainActivity.this,Homepage1.class);
intent1.putExtra("name",name);
MainActivity.this.startActivity(intent1);
}else{
Toast toast=Toast.makeText(MainActivity.this, "密码错误或账户不存在!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
//Toast.makeText(MainActivity.this,"密码错误或账户不存在!",Toast.LENGTH_LONG).show();
}
使用Intent类创建对象,调用方法putExtra选好对应的类型,然后传递信息,信息就被传到目的界面了。
//get the userName from login UI and show it
Intent intent = getIntent();
//把传送进来的String类型的Message的值赋给新的变量message
String message = intent.getStringExtra("name");
//把布局文件中的文本框和textview链接起来
TextView textView = (TextView) findViewById(R.id.RecMes);
//在textview中显示出来message
textView.setText(message);
在目标界面用类似于Graphics的方法,获取intent对象
然后显示在textView对象上即可。
4.实现注册功能——界面的生命周期浅浅的了解一下
界面的功能除了登录就是注册了
1.0版本思路
通过intent传递信息。进入注册界面之后,吧注册的信息传回主界面。
实现以后程序崩溃,查看报错信息。intent对象报空指针。然后就发现,我的方法是卸载oncreate下的。等于进入登录界面也就是intent中还没有信息的时候就需求信息和对象,因此报空指针崩溃。
这个第一个思路是将这部分返回代码写在别的生命周期里。也借此了解了下Acitity的生命周期。
I.何为Activity?
是Android的四大组件中最直观的一个,可以狭义地理解为Activity是与用户交互的界面,可以处理与用户交互的事件。简单说就是看到的界面。
常见的 Activity 类型有 ListActivity,FragmentActivitiy,TabAcitivty 等。当然开发中可根据自己的功能设计自己的公共基类: BaseActivity。
II.Activity的生命历程——概览
官方文档中对Activity的生命周期有个比较经典图,如下图一所示:
参考博主「sarsscofy」的原创文章,原文链接:https://blog.csdn.net/sarsscofy/article/details/82621579
Activity有如下特点:
A、在OnCreate创建,在OnDestroy销毁;
B、在onStart()之后可见,onStop之后不可见;
C、在OnResume之后获得焦点,在OnPause之后失去焦点。
D、在不可见时,如果系统资源紧张,会自动将Activity Destroy,再次拉起Activity时,需要重建则走OnCreate,否则走OnRestart ->OnStart。
E、因此,在开发中可以将一些必要资源释放放在onDestory中进行,而只需要在重建时更新设置的处理放在OnCreate中进行,需要每次切换页面显示时更新的处理放在OnResume,当然要注意他们之间的配合,不然会出错哦。
我尝试输出打印各个生命周期的状态发现无论如何都会在切换界面的时候进行一个完整的周期,因此注册功能目前尚无法通过intent实现。
当前2.0版本思路
通过调用IO,读写文件的方法来实现。实现以后会来补充。



