0x00:前言

本篇主要介绍用Android Studio实现游戏登陆界面,自动跳转,代码和资源文件我会上传到GitHub上,需要的可以自行下载。登陆界面功能包括记住密码,注册等。

下载链接:

https://github.com/ThunderJie/Code/tree/master/Android development/LianLianKan

0x01:实现过程

一.实现开始欢迎界面自动跳转至登陆界面

  1. 实现效果:

  2. 实现原理:
    首先我们创建四个继承自Activity的类Login_Activity.class,login.class,Register_Activity.class,next.class我们在AndroidManifest.xml中将游戏的图标和名字进行基本的一些设置,再将标题框隐藏。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.from_thunder_j.lianliankan"><applicationandroid:allowBackup="true"android:icon="@drawable/icon"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".login.Login_Activity"android:theme="@android:style/Theme.NoTitleBar.Fullscreen"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".login.login"android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/><activity android:name=".login.Register_Activity"android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity><activity android:name=".login.next"android:theme="@android:style/Theme.NoTitleBar.Fullscreen"></activity></application>
</manifest>

我们通过TimerTask来实现2秒之后自动从欢迎界面跳转到登陆界面,下面是Login_Activity.class的代码。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;import java.util.Timer;
import java.util.TimerTask;public class Login_Activity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login_activity);//实现自动跳转,注意是继承自Activity,要跳转的页面不能继承此页面Timer timer=new Timer();TimerTask timerTask=new TimerTask() {@Overridepublic void run() {//通过Intent实现跳转startActivity(new Intent(Login_Activity.this,login.class));Login_Activity.this.finish();}};//延迟2秒后跳转,注意单位是毫秒timer.schedule(timerTask,2000);}
}

下面是login_activity.xml的代码,实现自动跳转的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/frist_bg" ><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/game_title" /></LinearLayout>

Tips:Timer和TimerTask的区别
1)Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。它可以计划执行一个任务一次或反复多次。
2)TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务。

二.实现登陆界面

  1. 实现效果

  2. 实现原理
    login.java文件使用SharePreferences实现登陆界面,登陆成功后我是跳转到next.class,根据需要修改为自己的游戏界面就可以了,下面是实现内容:

package com.from_thunder_j.lianliankan.login;import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;import com.from_thunder_j.lianliankan.R;public class login extends Activity {private SharedPreferences pref;private SharedPreferences.Editor editor;private EditText accountEdit,passwordEdit;private Button btnLogin;private Button btnCancel;private Button btnregister;private CheckBox rememberPwd;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login);accountEdit = (EditText)findViewById(R.id.username);passwordEdit = (EditText)findViewById(R.id.password);rememberPwd = (CheckBox)findViewById(R.id.remember_pwd);btnLogin = (Button)findViewById(R.id.login);btnCancel = (Button)findViewById(R.id.cancel);btnregister = (Button)findViewById(R.id.register);//注册btnregister.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(login.this,Register_Activity.class);startActivity(intent);}});//记住密码pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());boolean isRemember = pref.getBoolean("remember_password",false);if(isRemember){//将账号密码都设置在文本框内String account = pref.getString("用户名","");String password = pref.getString("密码","");accountEdit.setText(account);passwordEdit.setText(password);rememberPwd.setChecked(true);}//登陆btnLogin.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String account = accountEdit.getText().toString();String password = passwordEdit.getText().toString();//pre查询本地保存的账号和密码,并与用户输入的用户名和密码进行比较SharedPreferences pre = getSharedPreferences("data",MODE_PRIVATE);if(account.equals(pre.getString("name",""))&&password.equals(pre.getString("password",""))){editor = pref.edit();if(rememberPwd.isChecked()){editor.putBoolean("remember_password",true);editor.putString("用户名",account);editor.putString("密码",password);}else{editor.clear();}editor.apply();Toast.makeText(login.this,"登陆成功!",Toast.LENGTH_SHORT).show();//这里修改一下next.class跳转到自己想要跳转的地方就可以了Intent intent = new Intent(login.this,next.class);startActivity(intent);}else {Toast.makeText(login.this,"登录失败!",Toast.LENGTH_SHORT).show();}}});btnCancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}
}

login.xml文件使用线性布局,内容如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/frist_bg"android:orientation="vertical"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="50dp"android:text="@string/username"android:textSize="20sp"android:textColor="#000"/><EditTextandroid:id="@+id/username"android:layout_width="350dp"android:layout_height="wrap_content"android:hint="@string/input_hint"android:maxLines="1"android:layout_gravity="center"/><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/password"android:textSize="20sp"android:textColor="#000"/><EditTextandroid:id="@+id/password"android:layout_width="350dp"android:layout_height="wrap_content"android:hint="@string/input_hint_passwd"android:maxLines="1"android:inputType="textPassword"android:layout_gravity="center"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><CheckBoxandroid:id="@+id/remember_pwd"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/remember_pwd"android:textSize="18sp" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><Buttonandroid:id="@+id/login"android:layout_width="100dp"android:layout_height="wrap_content"android:text="@string/button_sure"/><Buttonandroid:id="@+id/cancel"android:layout_width="100dp"android:layout_height="wrap_content"android:text="@string/btn_cancel" /></LinearLayout><Buttonandroid:id="@+id/register"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_gravity="end"android:text="@string/register"/></LinearLayout>

三.实现注册界面

Register_Activity.java文件主要实现注册内容,还是用了SharePreferences方法

package com.from_thunder_j.lianliankan.login;import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import com.from_thunder_j.lianliankan.R;public class Register_Activity extends Activity {private Button button;private Button btnCancel;private EditText account;private EditText password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.register_activity);button = (Button)findViewById(R.id.btn_sure);account = (EditText)findViewById(R.id.username);password = (EditText)findViewById(R.id.password);btnCancel = (Button)findViewById(R.id.btn_cancel);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();editor.putString("name",account.getText().toString());editor.putString("password",password.getText().toString());SharedPreferences pre = getSharedPreferences("data",MODE_PRIVATE);String Account = account.getText().toString();if(Account.equals(pre.getString("name",""))){Toast.makeText(Register_Activity.this,"账户名已经被注册!",Toast.LENGTH_SHORT).show();}else {//提交数据editor.commit();Toast.makeText(Register_Activity.this,"注册成功!",Toast.LENGTH_SHORT).show();finish();}}});btnCancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(Register_Activity.this,"注册失败!",Toast.LENGTH_SHORT).show();finish();}});}
}

布局文件register_activity.xml还是线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/frist_bg"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="请输入您想要注册的用户名和密码"android:textSize="20sp"android:textColor="#000"/><TableLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:stretchColumns="1"><TableRow><TextViewandroid:layout_height="wrap_content"android:text="@string/username"android:textSize="20sp"android:textColor="#000"/><EditTextandroid:id="@+id/username"android:layout_height="wrap_content"android:hint="@string/input_hint"/></TableRow><TableRow><TextViewandroid:layout_height="wrap_content"android:text="@string/password"android:textSize="20sp"android:textColor="#000"/><EditTextandroid:id="@+id/password"android:layout_height="wrap_content"android:inputType="textPassword"android:hint="@string/input_hint_passwd"/></TableRow></TableLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_sure"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/button_sure"android:textSize="20sp"/><Buttonandroid:id="@+id/btn_cancel"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/btn_cancel"android:textSize="20sp"/></LinearLayout></LinearLayout>

引用到的字符串文件:

<resources><string name="app_name">LianLianKan</string><string name="username">用户名:</string><string name="input_hint">请输入用户名</string><string name="password">密码:</string><string name="input_hint_passwd">请输入密码</string><string name="remember_pwd">记住密码</string><string name="button_sure">确定</string><string name="btn_cancel">取消</string><string name="register">注册</string>
</resources>

0x02:总结

代码还有很多地方可以优化,还可以判断登陆时是否存在用户名等,这只是一个大概的模板,需要的自行修改,代码可能有些地方写的不专业,希望大家多多包涵。

Android实现游戏登陆界面(自动跳转)相关推荐

  1. Android学习之登陆界面设计(一)前后期准备以及相关配置

    Android学习之登陆界面设计(一)前后期准备以及相关配置 前言 成品 成品样式 成品特点 工具 系统配置 手机配置 Android Studio 3.6.3 SDK 图片来源 矢量图标库 Back ...

  2. Android学习之登陆界面设计(二)基本界面设计

    Android学习之登陆界面设计(二)基本界面设计 前提 绘图样式 - drawable bg_login_btn_submit.xml bg_login_panel_slide.xml bg_log ...

  3. 自动生成Android界面,面向Android的Web Service界面自动生成技术研究

    摘要: 据统计,开发人员在开发应用程序的过程中,接近一半的代码用于用户界面部分,大约一半的运行时间用于执行这一部分.所以,减少用户界面部分的开发代码和运行时间,能有效提高程序的运行效率.智能家居中,由 ...

  4. android 登录保存密码,android 如何实现登陆界面的记住密码功能

    今天 写了一个有关登录记住密码的列子 其实这个例子的关键使用到了AutoCompleteTextView 以及sharedPreference的两个关键知识点,大家知道 AutoCompleteTex ...

  5. Android/IOS 实现接触NFC自动跳转到App,如果未安装App,则跳转到应用市场

    我们是做共享电单车的,友商最近推出了手机碰一碰NFC自动跳转到App自动开锁的功能,这个对于用户体验是有提升的,所以研究了一下. 友商的逻辑是这样的 如果手机没有安装该App,那么触碰NFC后 And ...

  6. 【Android如何从一个页面自动跳转页面】

    1.自动跳转 这里分享两种方法: 第一个:点击按钮实现跳转 新建项目 如下图点击之后自定义一个项目名一直下一步就行 项目结构是这样的 右击src里面的com.xxx.xxx 如下图点击other 然后 ...

  7. android中页面自动跳转,【学习笔记-安卓开发】8. Android Studio如何实现页面自动跳转(安卓学习系列博客)...

    先将上上一篇博客中写在页面里的button以及相关代码删除 8.如何让页面自动跳转 在安卓开发中有一个非常重要的Handler 当我们输入Handler会出现两个提示,一个是os中的,一个是loggi ...

  8. 离开微信界面自动跳转

    页面不在微信端,将自动跳转到指定页面 let isWeiXin = () => { return navigator.userAgent.toLowerCase().indexOf('micro ...

  9. html登陆成功自动跳转,点击登录,登陆成功,自动跳转到起始页面,这要怎么做?...

    先介绍下登录过程,然后介绍代码如何做. 登录过程 下面这个页面是起始页面,因为我已经测试过了,所以这里会显示我的用户名 点击按钮跳转到登录页面,输入用户名,密码,然后点击登录(注意这里我们的用户名是: ...

最新文章

  1. MAXIMO启动中心设置
  2. HTML5和Flash——如何选择合适的工具
  3. AndroidStudio 集成环信的一个坑
  4. 操作系统:了解一下磁盘结构
  5. [core]-ARM A76学习笔记
  6. mysql5.6 pt-query-digest,分析pt-query-digest输出信息
  7. python模块的导入的两种方式区别详解
  8. 2021奥运经济蓝皮书
  9. 什么是UIScrollView
  10. JavaScript数据结构-15.二叉树
  11. 【风电功率预测】基于matlab BP神经网络风电功率预测【含Matlab源码 399期】
  12. JAVA实用教程(第2版)配套源码笔记
  13. 杨春立:基于数字孪生的智慧城市顶层设计探索与实践...
  14. 计数器集成芯片+分析时序逻辑电路
  15. Potplayer如何显示书签,书签编辑器
  16. teamviewer远程黑屏问题
  17. 快速搭建自己的conda环境---以bioconda为例
  18. 虚拟串口软件:VSPD的使用
  19. 关于Java内存可见性的探究实验遇到的意外和happens-before
  20. C++使用windows API实现https站点的访问

热门文章

  1. 从网上下载他人软件并破解需要担责吗
  2. MySQL数据库备份 DataGrip导入和导出
  3. 【自学笔记】简单java电话本管理系统v1.0源码
  4. 命令行安装 Pycharm
  5. nbtstat命令linux_NBTSTAT和Tracert_命令的原理与作用
  6. 关于经营自媒体的几点思考
  7. 如何不费吹灰之力的装机?部署PXE实现Kickstart无人值守安装 — 装机界的最强王者
  8. 1、matlab中如何求定积分和不定积分(完整代码)
  9. 常见快捷键eclipse
  10. Java项目茶叶溯源系统(java+SSM+JSP+bootstrap+layUI+mysql)