700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 基于eclipse的android项目实战—博学谷(三)登录界面

基于eclipse的android项目实战—博学谷(三)登录界面

时间:2020-11-14 05:19:46

相关推荐

基于eclipse的android项目实战—博学谷(三)登录界面

BoXueGu源码资源下载链接:

/download/hyh/19477319

BoXueGu图片资源下载(免费):

/iWos0pyc4rc

登录界面主要用于输入登录信息,当点击“登录”按钮时需要在 Shared Preferences中查询输入的用户名是否有对应的密码,如果有则用此密码与当前输入的密码(需MD5加密)进行比对,如果信息一致,则登录成功,并把登录成功的状态和用户名保存到Sharedpreferences中,便于后续判断登录状态和获取用户名。如果登录失败,则有两种情况:一种是输入的用户名和密码不匹配:另一种是此用户名不存在。

效果图:

1.登录界面布局文件activity_login.xml

在该布局文件中,通过标签将main_title_bar.xml(标题栏)引入。

代码如下:activity_login.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/register_bg"android:orientation="vertical" ><include layout="@layout/main_title_bar"/><ImageView android:id="@+id/iv_head"android:layout_marginTop="25dp"android:layout_gravity="center_horizontal"android:contentDescription="@null"android:src="@drawable/default_icon"android:layout_width="70dp"android:layout_height="70dp"/><EditText android:singleLine="true"android:id="@+id/et_user_name"android:layout_width="fill_parent"android:layout_height="48dp"android:background="@drawable/register_psw"android:layout_marginTop="35dp"android:layout_marginLeft="35dp"android:layout_marginRight="35dp"android:drawableLeft="@drawable/user_name_icon"android:paddingLeft="8dp"android:drawablePadding="10dp"android:hint="@string/name"android:gravity="center_vertical"android:textColorHint="#a3a3a3"android:textColor="#000000"android:textSize="14sp"/><EditText android:singleLine="true"android:id="@+id/et_pwd"android:layout_width="fill_parent"android:layout_height="48dp"android:background="@drawable/register_psw"android:layout_marginTop="10dp"android:layout_marginLeft="35dp"android:layout_marginRight="35dp"android:drawableLeft="@drawable/psw_icon"android:paddingLeft="8dp"android:drawablePadding="10dp"android:inputType="textPassword"android:hint="@string/pwd"android:gravity="center_vertical"android:textColorHint="#a3a3a3"android:textColor="#000000"android:textSize="14sp"/><Buttonandroid:text="@string/login"android:id="@+id/btn_login"android:layout_gravity="center_horizontal"android:layout_marginTop="15dp"android:layout_marginLeft="35dp"android:layout_marginRight="35dp"android:textColor="@android:color/white"android:textSize="20sp"android:textStyle="bold"android:layout_width="fill_parent"android:layout_height="50dp"android:background="@drawable/register_selector"/><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_marginTop="8dp"android:layout_marginLeft="35dp"android:layout_marginRight="35dp"android:gravity="center_horizontal"android:orientation="horizontal"><TextView android:id="@+id/tv_register"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center_horizontal"android:padding="8dp"android:text="@string/tv_register"android:textColor="@android:color/white"android:textSize="14sp"/><!--layout_weight="1" layout_width="0dp"实现均分效果--><TextView android:id="@+id/tv_find_pwd"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center_horizontal"android:padding="8dp"android:text="@string/find_pwd"android:textColor="@android:color/white"android:textSize="14sp"/></LinearLayout></LinearLayout>

文本信息在values文件夹下的string.xml文件里面:

<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">BoXueGu</string><string name="hello_world">Hello world!</string><string name="boxuegu">博学谷</string><string name="name">请输入用户名</string><string name="pwd">请输入密码</string><string name="pwd_again">请再次输入密码</string><string name="btn_register">注册</string><string name="login">登录</string><string name="tv_register">立即注册</string><string name="find_pwd">找回密码?</string></resources>

2.登录界面逻辑代码

当点击“登录”按钮时,需要先判断用户名和密码是否为空,若为空则提示请输入用户名和密码;若不为空则获取用户输入的用户名,由于博学谷项目用的是本地数据,因此根据用户名在 Sharedpreferences中查询是否有对应的密码,如果有对应的密码并且与用户输入的密码(需MD5加密)比对一致,则登录成功。

新建类LoginActivity:在包china.ynyx.heyunhui.activity中,右击并选择“New”→“class”,新建LoginActivity.java文件

代码如下:LoginActivity.java

package china.ynyx.heyunhui.activity;import android.support.v7.app.AppCompatActivity;import android.content.Intent;import android.content.SharedPreferences;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import china.ynyx.heyunhui.MainActivity;import china.ynyx.heyunhui.R;import china.ynyx.heyunhui.utils.MD5Utils;import china.ynyx.heyunhui.activity.LoginActivity;public class LoginActivity extends AppCompatActivity {private TextView tv_main_title;//标题private TextView tv_back;//返回按钮private TextView tv_register,tv_find_pwd;//立即注册、找回密码的控件private Button btn_login;//登录按钮private EditText et_user_name,et_pwd;//用户名、密码的控件private String username,pwd,spPwd;//用户名、密码的控件的获取值@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_login);//设置此界面为竖屏setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);init();}private void init() {// TODO Auto-generated method stubtv_main_title=(TextView)findViewById(R.id.tv_main_title);tv_main_title.setText("登录");tv_back = ((TextView) findViewById(R.id.tv_back));tv_register = (TextView) findViewById(R.id.tv_register);tv_find_pwd = (TextView) findViewById(R.id.tv_find_pwd);btn_login = (Button) findViewById(R.id.btn_login);et_user_name = (EditText) findViewById(R.id.et_user_name);et_pwd = (EditText) findViewById(R.id.et_pwd);//返回按钮的点击事件tv_back.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubLoginActivity.this.finish();}});//立即注册控件的点击事件tv_register.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent=new Intent(LoginActivity.this,RegisterActivity.class);startActivityForResult(intent, 1);}});//找回密码点击事件tv_find_pwd.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub//跳转到找回密码界面(此界面暂时未创建)}});//登录按钮点击事件btn_login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubusername=et_user_name.getText().toString().trim();pwd=et_pwd.getText().toString().trim();String md5Pwd=MD5Utils.MD5(pwd);spPwd=readPwd(username);if(TextUtils.isEmpty(username)){Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();return;}else if (TextUtils.isEmpty(pwd)){Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();return;}else if(md5Pwd.equals(spPwd)){Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();//把登录状态和登录的用户名保存到SharedPreferences里面saveLoginStatus(true,username);//登录成功后通过Intent把登录成功的状态传递到MainActivity.java中Intent data=new Intent();data.putExtra("isLogin",true);setResult(RESULT_OK,data);//setResult为OK,关闭当前页面LoginActivity.this.finish();//在登录的时候,如果用户还没有注册则注册。注册成功后把注册成功后的用户名返回给前一个页面startActivity(new Intent(LoginActivity.this, MainActivity.class));return;}else if((!TextUtils.isEmpty(spPwd)&&!md5Pwd.equals(spPwd))){Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();return;}else {Toast.makeText(LoginActivity.this, "此用户不存在", Toast.LENGTH_SHORT).show();}}});}//从SharedPreferences中根据用户名读取密码private String readPwd(String username){SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);return sp.getString(username,"");}//保存登录状态和登录用户名到SharedPrefarences中private void saveLoginStatus(boolean status,String username){//loginInfo表示文件名SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);SharedPreferences.Editor editor=sp.edit();//获取编辑器editor.putBoolean("isLogin", status);editor.putString("loginUserName", username);//存入登录时的用户名mit();//提交修改}@Overrideprotected void onActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode, resultCode, data);if(data!=null){//从注册界面传递过来的用户名String username=data.getStringExtra("username");if(!TextUtils.isEmpty(username)){et_user_name.setText(username);//设置光标的位置上et_user_name.setSelection(username.length());}}}}

3.清单文件AndroidManifest.xml中注册

<activity android:name="china.ynyx.heyunhui.activity.LoginActivity"></activity>

4.完善功能

(1)欢迎界面SplashActivity.java

Intent intent=new Intent(SplashActivity.this,RegisterActivity.class);

改为:

Intent intent=new Intent(SplashActivity.this,LoginActivity.class);

实现欢迎界面3秒过后跳转到登录界面

(2)登录界面LoginActivity.java

上图中添加如下代码:(已经有了就不要添加了)实现登录成功后跳转到主页面

startActivity(new Intent(LoginActivity.this, MainActivity.class));

参考资料:《android项目实战——博学谷》(黑马程序员著)

基于eclipse的android项目实战—博学谷(一)欢迎界面

基于eclipse的android项目实战—博学谷(二)注册界面

基于eclipse的android项目实战—博学谷(三)登录界面

基于eclipse的android项目实战—博学谷(四)底部导航栏

基于eclipse的android项目实战—博学谷(五)“我”的模块

基于eclipse的android项目实战—博学谷(六)设置界面

基于eclipse的android项目实战—博学谷(七)修改密码

基于eclipse的android项目实战—博学谷(八)设置密保和找回密码

基于eclipse的android项目实战—博学谷(九)个人资料界面

基于eclipse的android项目实战—博学谷(十)个人资料修改

基于eclipse的android项目实战—博学谷(十一)习题界面

基于eclipse的android项目实战—博学谷(十二)习题详情界面

基于eclipse的android项目实战—博学谷(十三)水平滑动广告栏界面

基于eclipse的android项目实战—博学谷(十四)课程界面

基于eclipse的android项目实战—博学谷(十五)课程详情界面

基于eclipse的android项目实战—博学谷(十六)视频播放界面

基于eclipse的android项目实战—博学谷(十七)播放记录界面

基于eclipse的android项目实战—博学谷(十八)播放不同视频(网络视频)

基于eclipse的android项目实战—博学谷(十九)播放不同视频(本地视频)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。