基于android校园订餐APP,简单实现登录注册功能(SharedPreferences)
欢迎浏览本篇文章
大家好! 这是小编首次在博客上写的文章,若有不足请广大网友提出批评指教。最近刚好做了一个Android的校园订餐APP,这边简单实现了登录注册界面,登录注册数据利用SharedPreferences绑定到Android程序中,本文尚未涉及到与服务器的交流。
界面效果图
界面效果视频
页面设计代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/picture_start">
<TextView
android:id="@+id/start_tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Mk订餐支持IPv6网络"
android:textSize="18sp"
android:textColor="#fff"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:textStyle="bold"/>
</RelativeLayout>
ps:这个加载图片功能有待改善,做的比较简单不建议做参考
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".load"
android:background="#F0F8FF">
<ImageView
android:layout_marginTop="30dp"
android:layout_width="280dp"
android:layout_height="150dp"
android:id="@+id/login_iv_head"
android:layout_centerHorizontal="true"
android:src="@drawable/icon_picture_bigger" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/login_ll_number"
android:layout_marginTop="30dp"
android:layout_below="@+id/login_iv_head"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<EditText
android:id="@+id/login_et_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="用户名"
android:textColor="#000"
android:textSize="18sp"
android:background="@drawable/icon_bg"
android:paddingLeft="21dp"
android:paddingRight="21dp"
android:drawablePadding="7dp"
android:drawableLeft="@drawable/icon_user"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:id="@+id/login_ll_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/login_ll_number"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<EditText
android:id="@+id/login_et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_marginTop="15dp"
android:hint="密 码"
android:inputType="textPassword"
android:textSize="18sp"
android:background="@drawable/icon_bg"
android:paddingLeft="21dp"
android:paddingRight="21dp"
android:maxLines="1"
android:drawableLeft="@drawable/icon_pwd"
android:drawablePadding="7dp"
android:layout_marginBottom="20dp"/>
</LinearLayout>
<!-- 待开发的CheckBox -->
<!--<LinearLayout-->
<!--android:id="@+id/login_ll_check"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_below="@+id/login_ll_password"-->
<!--android:layout_centerHorizontal="true"-->
<!--android:layout_marginBottom="20dp">-->
<!--<CheckBox-->
<!--android:id="@+id/login_cb_pwd"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="记住密码"-->
<!--android:layout_marginTop="10dp"-->
<!--android:layout_marginRight="50dp"/>-->
<!--<CheckBox-->
<!--android:id="@+id/login_cb_load"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="自动登录"-->
<!--android:layout_marginTop="10dp"-->
<!--android:layout_marginLeft="50dp"/>-->
<!--</LinearLayout>-->
<Button
android:layout_width="match_parent"
android:id="@+id/login_btn_load"
android:layout_height="wrap_content"
android:layout_below="@id/login_ll_password"
android:layout_centerHorizontal="true"
android:layout_margin="12dp"
android:text=" 登 录 "
android:background="@drawable/btn_load_round"
android:textSize="20sp"
android:textColor="#fff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/login_tv_xieyi"
android:layout_below="@+id/login_btn_load"
android:text=" 登录用户代表你已阅读并同意《用户服务协议》、《隐私协议》和《运营商协议》"
android:textSize="13sp"
android:layout_marginLeft="21dp"
android:layout_marginRight="20dp"/>
<LinearLayout
android:id="@+id/login_ll_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/login_tv_forget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码"
android:textSize="16sp"
android:textColor="#1E90FF"
android:layout_marginRight="30dp"/>
<!--android:drawableLeft="@drawable/icon_forget"-->
<!--android:drawablePadding="7dp"-->
<TextView
android:id="@+id/login_tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户注册"
android:textSize="16sp"
android:textColor="#1E90FF"
android:layout_marginLeft="30dp"/>
<!--android:drawableLeft="@drawable/icon_register"-->
<!--android:drawablePadding="7dp"-->
</LinearLayout>
</RelativeLayout>
- 注册页面(activity_register.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F8FF">
<ImageView
android:id="@+id/register_iv_head"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/register_head"/>
<LinearLayout
android:id="@+id/register_ll_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_iv_head"
android:layout_margin="20dp">
<EditText
android:id="@+id/register_et_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="用户名"
android:textColor="#000"
android:textSize="18sp"
android:background="@drawable/icon_bg"
android:paddingLeft="21dp"
android:paddingRight="21dp"
android:drawablePadding="7dp"
android:drawableLeft="@drawable/icon_user"
android:maxLines="1"
android:drawableRight="@drawable/icon_picture_small"/>
</LinearLayout>
<LinearLayout
android:id="@+id/register_ll_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_ll_num"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<EditText
android:id="@+id/register_et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:hint="密 码"
android:inputType="textPassword"
android:textSize="18sp"
android:background="@drawable/icon_bg"
android:paddingLeft="21dp"
android:paddingRight="21dp"
android:maxLines="1"
android:drawableLeft="@drawable/icon_pwd"
android:drawablePadding="7dp"
android:drawableRight="@drawable/icon_picture_small"/>
</LinearLayout>
<LinearLayout
android:id="@+id/register_ll_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_ll_pwd"
android:layout_margin="20dp">
<EditText
android:id="@+id/register_et_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:hint="确认密码"
android:inputType="textPassword"
android:textSize="18sp"
android:background="@drawable/icon_bg"
android:paddingLeft="21dp"
android:paddingRight="21dp"
android:maxLines="1"
android:drawableLeft="@drawable/icon_pwd"
android:drawablePadding="7dp"
android:drawableRight="@drawable/icon_picture_small"/>
</LinearLayout>
<LinearLayout
android:id="@+id/register_ll_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/register_ll_again"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp">
<Button
android:id="@+id/register_btn_return"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="返回"
android:textSize="17sp"
android:textColor="#fff"
android:background="@drawable/btn_load_round" />
<Button
android:id="@+id/register_btn_register"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="17sp"
android:textColor="#fff"
android:background="@drawable/btn_load_round"
android:layout_marginLeft="25dp"
android:onClick="onClick"/>
</LinearLayout>
</RelativeLayout>
ps:页面设计的图片就不上传了,有点乱嗷小编整理不来,读者们可以自己在*iconfont*上找一些自己喜欢的图片(这里面的图片质量比较高且尺寸可调)
源代码
- 加载页面(StartActivity.class)
package com.example.experiment.mkreservation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
public class StartActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Thread myThread=new Thread(){
@Override
public void run() {
try{
sleep(5000);
Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
startActivity(intent);
finish();
}catch (Exception e){
e.printStackTrace();
}
}
};
myThread.start();
}
}
ps:软件加载图片页面仅供参考
- 登录页面(LoginActivity.class)
package com.example.experiment.mkreservation;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
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 com.example.experiment.mkreservation.fragment.ContainerActivity;
import java.util.Map;
public class LoginActivity extends Activity implements View.OnClickListener {
private EditText login_ed_num,login_ed_pwd;
private Button login_btn_load;
private TextView login_tv_register,login_tv_xieyi,login_tv_forget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_tv_register = (TextView)findViewById(R.id.login_tv_register);
login_tv_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(LoginActivity.this, "欢迎您注册", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);
}
});
login_tv_xieyi = (TextView) findViewById(R.id.login_tv_xieyi);
login_tv_xieyi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this).
setTitle("提示窗口").
setIcon(R.drawable.icon_gy).
setMessage("《用户服务协议》、《隐私协议》和《运营商协议》正在完整修订,请客官耐心等待吼").
setPositiveButton("确定", null);
builder.create().show();
}
});
final SharedPreferences sp = super.getSharedPreferences("data", MODE_PRIVATE);
login_tv_forget = (TextView) findViewById(R.id.login_tv_forget);
login_tv_forget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = sp.getString("num", "");
String password = sp.getString("pwd", "");
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this).
setTitle("提示窗口").
setIcon(R.drawable.icon_gy).
setMessage("用户名: " + login_ed_num.getText().toString() + "\n密码: "
+ login_ed_pwd.getText().toString()).
setPositiveButton("确定", null);
builder.create().show();
}
});
initView();
Map<String,String> userInfo = SPSaveLogin.getUserInfo(this);
if(userInfo != null){
login_ed_num.setText(userInfo.get("number"));
login_ed_pwd.setText(userInfo.get("password"));
}
}
private void initView(){
login_ed_num = (EditText)findViewById(R.id.login_et_number);
login_ed_pwd = (EditText)findViewById(R.id.login_et_password);
login_btn_load = (Button)findViewById(R.id.login_btn_load);
login_btn_load.setOnClickListener(this);
}
@Override
public void onClick(View view){
String yhm = login_ed_num.getText().toString().trim();
String mm = login_ed_pwd.getText().toString().trim();
if(TextUtils.isEmpty(yhm)){
Toast.makeText(this,"请输入用户名",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(mm)){
Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
return;
}
SharedPreferences sp = super.getSharedPreferences("data", MODE_PRIVATE);
String number = sp.getString("num", null);
String password = sp.getString("pwd", null);
if ( yhm.equals(number) && mm.equals(password) ){
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,ContainerActivity.class);
intent.putExtra("yhm",login_ed_num.getText().toString().trim());
intent.putExtra("mm", login_ed_pwd.getText().toString().trim());
startActivity(intent);
}else {
Toast.makeText(this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
}
}
}
- 注册页面(RegisterActivity.class)
package com.example.experiment.mkreservation;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity implements View.OnClickListener {
private Button register_btn_return,register_btn_register;
private EditText register_et_number,register_et_password,register_et_again;
private boolean isInfoChecked = false;
private String number,password,again;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
register_btn_return = (Button)findViewById(R.id.register_btn_return);
register_btn_return.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(RegisterActivity.this, "请您登录", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(intent);
}
});
initView();
}
private void initView() {
register_et_number = (EditText) findViewById(R.id.register_et_number);
register_et_password = (EditText) findViewById(R.id.register_et_password);
register_et_again = (EditText) findViewById(R.id.register_et_again);
register_btn_register = (Button) findViewById(R.id.register_btn_register);
register_btn_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.register_btn_register:
number = register_et_number.getText().toString().trim();
password = register_et_password.getText().toString();
again = register_et_again.getText().toString().trim();
if (number.length() != 0) {
if (password.length() != 0) {
if (password.equals(again)) {
if (isInfoChecked == false) {
isInfoChecked = true;
AlertDialog.Builder builder = new AlertDialog.Builder(this).
setTitle("注册信息").
setIcon(R.drawable.register_head).
setMessage("账号: " + register_et_number.getText().toString() + "\n密码: "
+ register_et_again.getText().toString()
+ "\n请再次点击“注册”完成注册").
setPositiveButton("确定", null);
builder.create().show();
boolean isSaveSuccess = SPSaveLogin.saveUserInfo(this,number,password,again);
if (isSaveSuccess) {
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
} else {
Intent intent = getIntent();
RegisterActivity.this.setResult(0, intent);
RegisterActivity.this.finish();
}
} else {
Toast.makeText(RegisterActivity.this, "两次密码不一致!" + "\n" + "第一次:"+password + "\n" + "第二次:"+again, Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "密码格式错误!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "用户名格式错误!", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
- 用户名密码保存(SharedPreferences方法)
package com.example.experiment.mkreservation;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
import java.util.Map;
public class SPSaveLogin {
public static boolean saveUserInfo(Context context,String number,String password,String again) {
SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("num",number);
edit.putString("pwd", password);
edit.putString("again",again);
edit.commit();
return true;
}
public static Map<String,String> getUserInfo(Context context){
SharedPreferences sp = context.getSharedPreferences("data", Context.MODE_PRIVATE);
String number = sp.getString("num", null);
String password = sp.getString("pwd",null);
String again = sp.getString("again",null);
Map<String,String>userMap=new HashMap<String,String>();
userMap.put("number",number);
userMap.put("password",password);
userMap.put("again",again);
return userMap;
}
}
最后,大家不要忘记修改 AndroidManifest.xml 了哈(页面间的跳转关键就是看这里!)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.experiment.mkreservation" >
<application
android:allowBackup="true"
android:icon="@drawable/icon_picture"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".StartActivity"
android:label="Mk订餐">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity"
android:label="Mk订餐" >
</activity>
<activity android:name=".RegisterActivity"
android:label="请注册">
</activity>
</application>
</manifest>
最后,小编真心的希望每一个读者路过时能够评论一句或者点个赞,让我知道你来过,小编会很开心的!
|