这里写自定义目录标题
- 实现效果
- 实现过程
- 注册mobtech并进行相关配置
- LitePal数据库
- 注册界面
- 登陆界面
- java代码
实现效果
注册界面
登陆界面
实现过程
注册mobtech并进行相关配置
,这一部分参考链接博客,其中对于相关配置,mobtech网站上有相关操作文档,请根据最新文档要求进行配置。
LitePal数据库
其中对于登陆注册账号的保存与验证均涉及到数据库操作,其中使用litepal数据库,便于理解操作,初学者可以去看《第一行代码第二版》了解相关操作。
注册界面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bg2"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center"
android:text="注册用户"
android:textSize="40sp"
android:textColor="#516D2C"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/register"
android:layout_gravity="center"
android:layout_marginBottom="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/call"
android:layout_marginRight="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="手机号:"
android:textSize="20sp"
android:gravity="center"
android:textColor="#000000"/>
<EditText
android:id="@+id/usrphone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="请输入手机号"
android:background="@drawable/editext_selector"
android:textSize="20sp"
android:textColor="#000000"
android:inputType="text"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:gravity="left|center"
android:maxLength="20"
android:paddingLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
<EditText
android:id="@+id/cord"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:hint="验证码"
/>
<Button
android:id="@+id/buttonCode"
android:background="@color/ps_color_transparent_e0db"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="获取验证码"
android:text="获取验证码" />
</LinearLayout>
<Button
android:id="@+id/registerBtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="注册"
android:gravity="center"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:background="@drawable/btn_selector"
android:layout_marginTop="15dp"/>
</LinearLayout>
</RelativeLayout>
登陆界面
<?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/bg"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="欢迎进入社区用户管理系统"
android:textColor="#516D2C"
android:textSize="26sp" />
<ImageView
android:layout_width="160dp"
android:layout_height="148dp"
android:layout_gravity="center"
android:src="@drawable/appcon" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/loginName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:hint="请输入账号(手机号)"
android:background="@drawable/editext_selector"
android:textColor="#000000"
android:inputType="text"
android:paddingLeft="10dp"
android:textSize="20sp"/>
<EditText
android:id="@+id/loginPwd"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="110dp"
android:hint="请输入密码(首次注册为验证码)"
android:background="@drawable/editext_selector"
android:textColor="#000000"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<CheckBox
android:id="@+id/check"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="记住密码"
android:textSize="20sp"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/loginBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="#FFFFFF"
android:layout_marginTop="11dp"
android:background="@drawable/btn_selector"
android:textSize="23sp"/>
<Button
android:id="@+id/registerBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
android:textColor="#FFFFFF"
android:layout_marginTop="9dp"
android:background="@drawable/btn_selector"
android:textSize="23sp"/>
</LinearLayout>
</LinearLayout>
其中相关矢量图可以到阿里巴巴矢量图标库(免费网站) https://www.iconfont.cn/search/index?searchType=icon&q=%E6%88%91%E7%9A%84 ,进行相关图标下载。
java代码
- 使用正则匹配判断号码是否符合要求
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.*;
public class Utils {
//正则匹配手机号码
public static boolean checkTel(String tel){
Pattern p = compile("^[1][3,4,5,7,8,9][0-9]{9}$");
Matcher matcher = p.matcher(tel);
return matcher.matches();
}
}
- 注册活动代码如下
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
EditText phone;
EditText cord;
Button getCord;
Button saveCord;
//new倒计时对象,总共的时间,每隔多少秒更新一次时间
final RegisterActivity.MyCountDownTimer myCountDownTimer = new RegisterActivity.MyCountDownTimer(60000,1000);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_register);
init();//绑定控件
MobSDK.submitPolicyGrantResult(true,null);//调用smssdk接口
//设置按钮的点击事件
getCord.setOnClickListener((View.OnClickListener) this);
saveCord.setOnClickListener((View.OnClickListener) this);
//返回键
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
}
public void register(){//接口回调函数
EventHandler eh=new EventHandler() {
@Override
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE)//回调完成
{
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Usr usr = new Usr();
usr.setPassword(cord.getText().toString());
if(!(phone.getText().toString().trim()).equals("你的手机号码")){
usr.updateAll("phonenum=?", phone.getText().toString());
}else{
usr.setUsrType(0);
usr.setPhonenum("你的手机号码");
usr.save();//将管理员身份存进去
}
Toast.makeText(getApplicationContext(), "你已验证成功,注册成功,请登录", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);//从注册界面转到登陆界面
startActivity(intent);
finish();
}
});
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { //如果获取到验证码
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplication(), "已发送验证码,请注意查收", Toast.LENGTH_SHORT).show();
}
});
} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
} //返回支持发送验证码的国家列表
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplication(), "操作失败,重新获取验证码", Toast.LENGTH_SHORT).show();
}
});
((Throwable) data).printStackTrace();
}
}
};
SMSSDK.registerEventHandler(eh); //注册短信回调
saveCord.setVisibility(View.VISIBLE);
myCountDownTimer.start();
}
@SuppressLint("ResourceAsColor")
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.buttonCode: //查询操作
// getCord.setBackgroundColor(R.color.ps_color_66);
register();//调用注册短信发送的的回调接口
String a=phone.getText().toString().trim();//获取输入的手机号
if(TextUtils.isEmpty(a)){
Toast.makeText(getApplicationContext(), "输入的手机号不能为空", Toast.LENGTH_SHORT).show();
}
else{
if(Utils.checkTel(a))//利用正则表达式获取检验手机号
{
List<Usr> usrs= LitePal.where("phonenum=?",a).find(Usr.class);
//将管理员单独列出否则将无法进行之后操作
if(usrs.size()==0&&!a.equals("你的手机号码")){
Toast.makeText(getApplicationContext(), "你不是该社区用户", Toast.LENGTH_SHORT).show();
}else{
SMSSDK.getVerificationCode("86", a);
}
}else{
Toast.makeText(getApplicationContext(), "请输入有效手机号", Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.registerBtn: //
String phoneNumber=phone.getText().toString().trim();//获取输入手机号
String code=cord.getText().toString().trim();//获取输入的验证码
if(TextUtils.isEmpty(phoneNumber)&&TextUtils.isEmpty(code)){
Toast.makeText(getApplicationContext(), "请检验你输入的信息", Toast.LENGTH_SHORT).show();
}else{
SMSSDK.submitVerificationCode("86",phoneNumber,code);//对比
}
break;
default:
throw new IllegalStateException("Unexpected value: " + v.getId());
}
}
private void init(){
phone=(EditText) findViewById(R.id.usrphone);
cord=(EditText) findViewById(R.id.cord);
getCord=(Button) findViewById(R.id.buttonCode);
saveCord=(Button) findViewById(R.id.registerBtn);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onPause();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
@SuppressLint("ResourceAsColor")
@Override
protected void onDestroy(){
super.onDestroy();
// getCord.setBackgroundColor(R.color.ps_color_transparent_e0db);
}
//倒计时
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//计时过程
@Override
public void onTick(long l) {
//防止计时过程中重复点击
getCord.setClickable(false);
getCord.setBackgroundColor(Color.parseColor("#bfbfbf"));
getCord.setText(l/1000+"秒");
}
//计时完毕的方法
@Override
public void onFinish() {
//重新给Button设置文字
getCord.setText("重新获取");
//设置可点击
getCord.setClickable(true);
}
}
}
这里我设置的app管理员可以进行注册登录,然后管理员添加用户手机号码之后,用户才可以进行注册。
- 登录活动界面代码
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private EditText usrId,pwd;
private Button btnLogin,btnRegister;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
CheckBox remember;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_login);
init();//加载控件
//以下用来实现记住密码
preferences= PreferenceManager.getDefaultSharedPreferences(this);
boolean isRemember=preferences.getBoolean("remember_password",false);
if(isRemember){
String account=preferences.getString("account","");
String password=preferences.getString("password","");
usrId.setText(account);
pwd.setText(password);
remember.setChecked(true);
}
btnLogin.setOnClickListener((View.OnClickListener) this);
btnRegister.setOnClickListener((View.OnClickListener) this);
}
@Override
public void onClick(View v)
{
switch(v.getId())
{
case R.id.registerBtn: //查询操作
Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
startActivity(intent);//简单跳转一下
break;
case R.id.loginBtn: //
//检测登录
String account=usrId.getText().toString().trim();
String password=pwd.getText().toString().trim();
//用来标记正在使用app的用户
List<phoneNum> temps= LitePal.findAll(phoneNum.class);
phoneNum ph=new phoneNum();
ph.setPhonenum(account);
if(temps.size()==0){
ph.save();
}else{
ph.updateAll();
}
//
List<Usr> usrs= LitePal.where("phonenum=? and password=?",account,password).find(Usr.class);
if(usrs.size()!=0){
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
editor=preferences.edit();
if(remember.isChecked()){
editor.putBoolean("remember_password",true);
editor.putString("account",account);
editor.putString("password",password);
}else{
editor.clear();
}
editor.apply();
//登陆成功跳转到主界面
Intent intent1=new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent1);
finish();
}
else{
Toast.makeText(LoginActivity.this, "账号或密码错误,请重新登录", Toast.LENGTH_SHORT).show();
}
break;
default:break;
}
}
private void init(){
usrId=findViewById(R.id.loginName);
pwd=findViewById(R.id.loginPwd);
btnLogin=findViewById(R.id.loginBtn );
btnRegister=findViewById(R.id.registerBtn);
remember=(CheckBox) findViewById(R.id.check);
}
}
登录成功之后即可进入app首页。