概述
LoginActivity.java 包 com.hyx.example.mymap; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; 公共类 LoginActivity扩展 Activity { private EditText username = null ; private EditText password = null ; private Button login = null ; private SharedPreferences sharedPreferences ; //初始化控件 公共无效的init(){ 用户名 =(的EditText)findViewById(R.id。 username_edit); 密码 =(的EditText)findViewById(R.id。 userpassword_edit); login =(Button)findViewById( R.id.login_button); } @Override protected void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); 的setContentView(R.layout 登录); //显示自己的UI视图 // 初始化控件 init(); //调用验证是否第一次登录方法 userUpdate(); //调用登录验证方法 loginVerify(); } //判断是否第一次登录初始化用户名密码 public void userUpdate(){ //读取文件内容 sharedPreferences = getSharedPreferences( “myfile”, MODE_PRIVATE); //读取数据 String susername = sharedPreferences .getString( “username”, “”); String spwd = sharedPreferences .getString(“pwd”,“”); //第一次进入程序读取用户名密码为空,则写入初始化数据并提示用户 if(susername.equals(“”)&& spwd.equals(“”)){ //写入数据 SharedPreferences.Editor editor = sharedPreferences .edit(); editor.putString(“username”,“admin”); editor.putString(“pwd”,“123456”); //提交数据 editor.commit(); 吐司。makeText(LoginActivity。这个,“初始化用户名管理员密码123456” , 吐司。LENGTH_LONG)。show(); } } //登录验证 public void loginVerify(){ //点击登录按钮所发生的事件。(1,验证合法2,验证不合法) login .setOnClickListener( new OnClickListener(){ @Override public void onClick(查看arg0){ //定义变量 String strUsername,strPassword; //得到输入框内容 strUsername = username .getText()。toString(); strPassword = password .getText()。toString(); //读取文件内容得到用户名密码 sharedPreferences = getSharedPreferences( “myfile”, MODE_PRIVATE); //读取数据 String susername = sharedPreferences .getString( “username”, “”); String spwd = sharedPreferences .getString(“pwd”,“”); //登录校验 if(strUsername.equals(susername)&& strPassword.equals(spwd)){ //登录成功后提示 Toast。makeText(LoginActivity。这个, “登陆成功!” , 吐司。LENGTH_SHORT).show(); //登陆成功后切换到MainActivity.class 意图意图= 新意图(LoginActivity。此, 主要活动。课程); //启动 startActivity(intent); //登录成功后销毁登录界面 finish(); } else { 吐司。makeText(LoginActivity。这个,“登陆失败!” , 吐司。LENGTH_SHORT).show(); } } }); } // @Override // public boolean onCreateOptionsMenu(菜单菜单){ // getMenuInflater()。inflate(R.menu.main,menu); //返回true; //} } login.xml
< RelativeLayout xmlns:android = “http://schemas.android.com/apk/res/android” xmlns:custom = “http://schemas.android.com/apk/res-auto” android :layout_width = “match_parent “ android :layout_height = ”match_parent“ android :fitsSystemWindows = ”true“ > < RelativeLayout android :id = “@ + id / login_layout” android :layout_width = “match_parent” android :layout_height = “wrap_content” android :layout_marginLeft = “20dp” android :layout_marginRight = “20dp” android :gravity = “center” > < FrameLayout android :id = “@ + id / username_layout” android :layout_width = “fill_parent” android :layout_height = “wrap_content” android :layout_marginTop = “55dp” android :gravity = “center” > < EditText android :id = “@ + id / username_edit” android :layout_width = “match_parent” android :layout_height = “55dp” android :layout_marginTop = “5dp” android :hint = “账号/手机号/邮箱” android :paddingRight = “60dp” android :maxLength = “20” android :paddingLeft = “55dp” > </ EditText > < ImageView android :layout_width = “22dp” android :layout_height = “21dp” android :layout_marginStart = “8dp” android :layout_gravity = “left | center_vertical” android :background = “@ drawable / zhanghao” android :visibility = “visible” / > < TextView android :id = “@ + id / contry_sn” android :layout_width = “40dp” android :layout_height = “50dp” android :layout_gravity = “left | center_vertical” android :layout_marginTop = “4dp” android :gravity = “center” android :text = “+ 62” android :textColor = “@ android :color / black” android :textSize = “18sp” android :visibility = “invisible” /> </ FrameLayout > < FrameLayout android :id = “@ + id / usercode_layout” android :layout_width = “fill_parent” android :layout_height = “wrap_content” android :layout_below = “@ id / username_layout” android :layout_marginTop = “6dp” android :gravity = “center “ > < EditText android :id = “@ + id / userpassword_edit” android :layout_width = “match_parent” android :layout_height = “55dp” android :hint = “密码” android :inputType = “textPassword” android :paddingRight = “60dp” android : maxLength = “20” android :paddingLeft = “55dp” > </ EditText > < ImageView android :layout_width = “24dp” android :layout_height = “22dp” android :layout_marginStart = “7dp” android :layout_gravity = “left | center_vertical” android :background = “@ drawable / mima” /> < 按钮 android :id = “@ + id / passwordeye” android :layout_width = “23dp” android :layout_height = “23dp” android :background = “@ drawable / kejian” android :layout_gravity = “right | center_vertical” android :layout_marginRight = “10dp” /> </ FrameLayout > < Button android :id = “@ + id / login_button” android :layout_width = “wrap_content” android :layout_height = “45dp” android :layout_below = “@ + id / usercode_layout” android :layout_centerHorizontal = “true” android :layout_marginTop = “ 34dp“ android :background = ”@ drawable / bluebutton“ android :textSize = ”18sp“ android :text = ” 登录 “ android :textColor = ”#fffffff“ /> < Button android :id = “@ + id / login_error” android :layout_width = “wrap_content” android :layout_height = “wrap_content” android :layout_alignRight = “@ id / login_button” android :layout_below = “@ id / login_button” android :text = “忘记密码” android :background = “#00000000” android :textSize = “16sp” /> < Button android :id = “@ + id / register” android :layout_width = “wrap_content” android :layout_height = “wrap_content” android :layout_alignLeft = “@ id / login_button” android :layout_below = “@ id / login_button” android :background = “#00000000” android :gravity = “left | center_vertical” android :text = “注册” android :textSize = “16sp” android :visibility = “visible” /> </ RelativeLayout > < RelativeLayout android :id = “@ + id / remember_layout000” android :layout_width = “wrap_content” android :layout_height = “wrap_content” android :layout_alignParentBottom = “true” android :layout_marginBottom = “1dp” android :layout_marginLeft = “20dp” android : layout_marginRight = “20dp” > </ RelativeLayout > </ RelativeLayout >
注意:一定要在AndroidManifest.xml里加activity啊!!!
到此这篇关于Android Studio实现登录功能案例讲解的文章就介绍到这了,更多相关Android Studio实现登录内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是淡定红酒为你收集整理的Android Studio实现登录功能案例讲解的全部内容,希望文章能够帮你解决Android Studio实现登录功能案例讲解所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复