Android Walker登录记住密码页面功能实现
作者:Vivinia_Vivinia 发布时间:2023-12-16 05:45:03
标签:Android,登录,记住密码
本文实例为大家分享了Android Walker登录记住密码页面的具体代码,供大家参考,具体内容如下
目标效果:
这一次修改的不多,添加了点击用户登录的跳转,登录页面的记住密码,和程序运行一次后,不进入导航页面的功能。
1.MainActivity.java页面修改了setOnItemClickListener的点击事件,进行跳转。
MainActivity.java页面:
package com.example.login;
import java.util.ArrayList;
import java.util.List;
import com.example.walkersimulate.util.SlideMenu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private SlideMenu slideMenu;
private ImageView ivSwitchSlideMenu;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initMenuList();
slideMenu = (SlideMenu) findViewById(R.id.slideMenu);
ivSwitchSlideMenu = (ImageView) findViewById(R.id.switch_slidemenu);
ivSwitchSlideMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (slideMenu.isMainScreenShowing()) {// 判断滑动菜单是否已打开,如果未打开
slideMenu.openMenu(); // 打开滑动菜单
} else {
slideMenu.closeMenu();// 关闭滑动菜单
}
}
});
}
private void initMenuList() {
/*设置数据源(图片和文本信息)*/
int[] icons = { R.drawable.icons_menu_login,
R.drawable.icons_menu_sport, R.drawable.icons_menu_inform,
R.drawable.icons_menu_history, R.drawable.icons_menu_weather,
R.drawable.icons_menu_health, R.drawable.icons_menu_setting };
final String[] introductons = getResources().getStringArray(
R.array.menulist);
/*实例列表*/
List<Item> items = new ArrayList<Item>();
/*向列表中添加图片和对应的文本信息*/
for (int i = 0; i < icons.length; i++) {
items.add(new Item(icons[i], introductons[i]));
}
ListView lvMenuList = (ListView) findViewById(R.id.lvMenuList);
/*创建适配器*/
itemAdapter adapter = new itemAdapter(this, R.layout.menulist_item,
items);
/*配置适配器*/
lvMenuList.setAdapter(adapter);
/*列表某一项的点击事件*/
lvMenuList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
switch (position) {
case 0:
intent=new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
}
}
2.WelcomeActivity.java页面修改onAnimationEnd动画结束事件。
WelcomeActivity.java页面:
package com.example.login;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.RelativeLayout;
public class WelcomeActivity extends Activity {
private Intent intent;
private SharedPreferences pref;
private boolean isFirst=true;//用于判断是否是第一次运行,运行后变为false
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
RelativeLayout layoutWelcome=(RelativeLayout) findViewById(R.id.layoutwelcome);
AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
alphaAnimation.setDuration(3000);
layoutWelcome.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
judgeIntent();
}
private void judgeIntent() {
pref=getSharedPreferences("isFirst",MODE_PRIVATE);//创建SharedPreferences对象
isFirst=pref.getBoolean("isFirstIn",true);//如果第一次运行,无isFirstIn值,自动获取第二个参数为默认值
if(isFirst){//如果为true,进入if语句
intent=new Intent(WelcomeActivity.this,GiudeActivity.class);
Editor editor=pref.edit();
editor.putBoolean("isFirstIn",false);//保存isFirstIn值为false
editor.commit();//提交数据
}else{
intent=new Intent(WelcomeActivity.this,MainActivity.class);//如果为false,说明程序已经运行过,直接跳转到主页面
}
startActivity(intent);
finish();
}
});
}
}
3.login_top.xml页面添加多选框,用于选择记住密码。
login_top.xml页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/logintop_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_top_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/etName"
android:hint="@string/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/etaccount"
android:drawablePadding="25dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/etPass"
android:hint="@string/etPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etName"
android:layout_below="@+id/etName"
android:layout_marginTop="10dp"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/etpass"
android:drawablePadding="10dp"
android:ems="10"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/cbSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
android:text="记住密码"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etPass"
android:layout_below="@+id/etPass"
android:layout_marginTop="40dp" >
<Button
android:id="@+id/btLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btchange_bg"
android:text="@string/btLogin" />
<Button
android:id="@+id/btRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btchange_bg"
android:text="@string/btRegister"
android:layout_marginLeft="5dp" />
</LinearLayout>
</RelativeLayout>
4.LoginActivity.java页面处理数据并保存。
LoginActivity.java页面:
package com.example.login;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity implements OnClickListener{
private EditText etName,etPass;
private CheckBox cbSave;
private Button btLogin,btRegister;
private SharedPreferences pref;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
/*获取控件id*/
getId();
/*获取数据*/
getData();
/*绑定点击事件*/
bindClick();
}
/*获取控件id*/
private void getId() {
etName=(EditText) findViewById(R.id.etName);
etPass=(EditText) findViewById(R.id.etPass);
cbSave=(CheckBox) findViewById(R.id.cbSave);
btLogin=(Button) findViewById(R.id.btLogin);
btRegister=(Button) findViewById(R.id.btRegister);
}
/*获取数据*/
private void getData() {
pref=getSharedPreferences("savaLogin",MODE_PRIVATE);//创建SharedPreferences对象
editor=pref.edit();//创建SharedPreferences的编辑器
String userName=pref.getString("userName","");//获取用户名,没有则用空代替
String userPass=pref.getString("userPass","");
if(userName.equals("")){//如果为空,代表前一次为选择记住密码,则这次显示记住密码多选框不打勾
cbSave.setChecked(false);
}else{
cbSave.setChecked(true);
etName.setText(userName);//将获取到的值设置为text
etPass.setText(userPass);
}
}
/*点击事件保存数据*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btLogin:
String userName=etName.getText().toString().trim();//获取EditText中输入的值,并去掉空格
String userPass=etPass.getText().toString().trim();
if("admin".equals(userName)&&"123456".equals(userPass)){
if(cbSave.isChecked()){
editor.putString("userName",userName);
editor.putString("userPass",userPass);
editor.commit();//提交数据
}else{//若没有选择记住密码
editor.remove("userPass");//删除密码
editor.commit();
}
Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
break;
case R.id.btRegister:
break;
}
}
private void bindClick() {
btLogin.setOnClickListener(this);
btRegister.setOnClickListener(this);
}
}
5.程序运行就显示目标效果了。
源码下载:Android Walker登录记住密码页面
来源:https://blog.csdn.net/hester_hester/article/details/51318958


猜你喜欢
- 1、找准入口,使用ClassPathXmlApplicationContext的构造方法加载配置文件,用于加载classPath下的配置文件
- 一. 下载tomcat首先要到tomcat官网去下载安装包,官网下载地址如下:http://tomcat.apache.org/downlo
- 1、说明isInterrupted()可以判断当前线程是否被中断,仅仅是对interrupt()标识的一个判断,并不会影响标识发生任何改变(
- 本文实例为大家分享了SpringMVC实现上传下载文件的具体代码,供大家参考,具体内容如下一、SpringMVC专门提供了CommonsMu
- 在Android开发中,经常会遇到这样一种情况,即需要将用户偏好设置(如用户偏好的app色彩主题)、与特定登录用户相关的设置(如不同登陆用户
- 原理很简单,利用Path画一个图,然后用动画进行播放,播放时间由依赖属性输入赋值与控件内部维护的一个计时器进行控制。控件基本是玩具,无法作为
- 本文实例分析了Android中ImageView用法。分享给大家供大家参考,具体如下:猜牌游戏大家可能以前都玩过,这里我们用这个小游戏来说明
- Java平台的垃圾收集机制显著提高了开发者的效率,但是一个实现糟糕的垃圾收集器可能过多地消耗应用程序的资源。在Java虚拟机性能优化系列的第
- 一、Service简介Service是Android程序中四大基础组件之一,它和Activity一样都是Context的子类,只不过它没有U
- 为什么需要在应用程序中增加渠道信息?Android应用的发布需要面对各种各样的市场,我们称之为渠道。有的时候,我们需要知道应用是从哪个渠道下
- import java.util.ArrayList;import java.util.Collections;import java.ut
- 在学习安卓的最初过程中我们学的都是最基本的一个活动,只有一个活动的应用也太简单了吧,没错我们的最求应该更高点,不管你创建多少个活动,接下里我
- 在我们做项目的过程中,有可能会遇到跨域请求,所以需要我们自己组装支持跨域请求的JSONP数据,而在4.1版本以后的SpringMVC中,为我
- 前言:我们每天都在编写Java代码,编译,执行。很多人已经知道Java源代码文件(.java后缀)会被Java编译器编译为字节码文件(.cl
- 我们第三章分析过客户端接入的流程, 这一小节带大家剖析客户端发送数据, Server读取数据的流程:首先温馨提示, 这一小节高度耦合第三章的
- Spring Data Elasticsearch提供了ElasticsearchTemplate工具类,实现了POJO与elasticse
- 第一种,在配置文件配置在application.xml直接配置,这种方式是全局配置,所有返回给前端对象的属性为null或"&quo
- 在spring boot 项目中使用thymeleaf模板,将后台数据传递给前台界面。1、将后台数据传递给前台有很多种方式,可以将后台要传递
- 在上一篇文章中,我们之前对BarChart.lerp的定义并不是高效的,我们正在创建的Bar实例,仅作为Bar.lerp的参数给出,并且针对
- 目录小写 string 与大写 String声明与初始化 stringstring 的不可变性正则 string 与原义 stringstr