话不多说先上图:
首先是具体的布局设计:
https://blog.csdn.net/qq_43377749/article/details/84260348
效果图:
现在附上实现具体功能的代码如下:
点击跳转 https://github.com/FishInWater-1999/SignInSystem
这里是登录界面
public class StartActivity extends AppCompatActivity implements View.OnClickListener{
TextView tv_regist;
EditText et_login_user, et_login_password;
Button bt_login;
TextView tv_forget;
String user_num;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载主题
final ColorTheme newTheme = new ColorTheme(this);
newTheme.loadTheme();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_start);
Bmob.initialize(this, "40de716427e259476d34aa6e0a7e4555");
tv_regist = (TextView) findViewById(R.id.tv_regist);
bt_login = (Button) findViewById(R.id.login);
tv_forget = (TextView) findViewById(R.id.tv_forget);
et_login_user = (EditText) findViewById(R.id.et_login_user);
et_login_password = (EditText) findViewById(R.id.et_login_password);
tv_regist.setOnClickListener(StartActivity.this);
bt_login.setOnClickListener(StartActivity.this);
tv_forget.setOnClickListener(StartActivity.this);
//改变控件颜色位默认
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
GradientDrawable myGrad = (GradientDrawable)bt_login.getBackground();
myGrad.setColor(typedValue.data);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.tv_regist:
Intent intent_regist = new Intent(StartActivity.this, RegistActivity.class);
startActivity(intent_regist);
break;
case R.id.tv_forget:
Intent intent_forget = new Intent(StartActivity.this, MobileLoad.class);
startActivity(intent_forget);
break;
case R.id.login:
user_num = et_login_user.getText().toString();
String user_password = et_login_password.getText().toString().trim();
// 非空验证
if (user_num.isEmpty() || user_password.isEmpty()) {
Toast.makeText(StartActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
User bu2 = new User();
bu2.setUsername(user_num);
bu2.setPassword(user_password);
// 使用BmobSDK提供的登录功能
bu2.login(new SaveListener<BmobUser>() {
@Override
public void done(BmobUser bmobUser, BmobException e) {
if(e==null){
Toast.makeText(StartActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
Intent intent_main = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent_main);
login p2 = new login();
p2.setName(user_num);
p2.update("o0HXjjjl", new UpdateListener() {
@Override
public void done(BmobException e) {
if(e==null){
}else{
}
}
});
//通过BmobUser user = BmobUser.getCurrentUser()获取登录成功后的本地用户信息
//如果是自定义用户对象MyUser,可通过MyUser user = BmobUser.getCurrentUser(MyUser.class)获取自定义用户信息
}else{
Toast.makeText(StartActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
//loge(e);
}
}
});
break;
}
}
接着就是注册界面啦:
public class RegistActivity extends AppCompatActivity implements View.OnClickListener {
dler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if((Integer)msg.obj==0){
imageView01.setImageBitmap(bitmap);
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载主题
final ColorTheme newTheme = new ColorTheme(this);
newTheme.loadTheme();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_regist);
Bmob.initialize(this, "\n" + "40de716427e259476d34aa6e0a7e4555");
chnage();
imageView01 = (ImageView) findViewById(R.id.head_portrait);
et_regist_user= (EditText)findViewById(R.id.et_regist_user);
et_regist_password= (EditText) findViewById(R.id.et_regist_password);
et_regist_fullname = (EditText) findViewById(R.id.et_regist_fullname);
et_regist_group= (EditText) findViewById(R.id.et_regist_group);
et_regist_telephone = (EditText) findViewById(R.id.et_regist_telephone);
et_regist_againpassword = (EditText) findViewById(R.id.et_regist_againpassword) ;
bt_regist_save= (Button) findViewById(R.id.bt_regist_save);
bt_regist_cancel= (Button) findViewById(R.id.bt_regist_cancel);
bt_regist_save.setOnClickListener(this);
bt_regist_cancel.setOnClickListener(this);
imageView01.setOnClickListener(this);
//改变控件颜色位主题色
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
GradientDrawable myGrad = (GradientDrawable)bt_regist_save.getBackground();
myGrad.setColor(typedValue.data);
myGrad = (GradientDrawable)bt_regist_cancel.getBackground();
myGrad.setColor(typedValue.data);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_regist_save:
File file = new File(PathGetter.getPath(this, uri));
BmobFile bmobFile = new BmobFile(file);
final String user_num=et_regist_user.getText().toString();
final String user_password=et_regist_password.getText().toString().trim();
final String user_fullname = et_regist_fullname.getText().toString().trim();
final String user_group = et_regist_group.getText().toString().trim();
final String user_telephone = et_regist_telephone.getText().toString().trim();
final String user_againpassword = et_regist_againpassword.getText().toString().trim();
// 非空验证
if (user_num.isEmpty() || user_password.isEmpty() || user_fullname.isEmpty() || user_group.isEmpty() || user_telephone.isEmpty() ||user_againpassword.equals(user_password)==false) {
Toast.makeText(RegistActivity.this, "有未填项", Toast.LENGTH_SHORT).show();
return;
}
// 使用BmobSDK提供的注册功能
final User myUser=new User();
myUser.setImg(bmobFile);
//用uploadblock将图片上传至服务器
myUser.getImg().uploadblock(new UploadFileListener() {
@Override
public void done(BmobException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), "上传成功!!", Toast.LENGTH_SHORT).show();
//将用户输入的信息存入p2,准备存入数据库
myUser.setUsername(user_num);
myUser.setPassword(user_password);
myUser.setFullname(user_fullname);
myUser.setGroup(user_group);
myUser.setTelephone(user_telephone);
myUser.setAgainpassword(user_againpassword);
//将p2整个对象存入数据库
myUser.signUp(new SaveListener<String>() {
@Override
public void done(String objectId,BmobException e) {
if(e==null){
Toast.makeText(getApplicationContext(),"添加数据成功,返回objectId为:"+objectId,Toast.LENGTH_SHORT).show();
}else{
// Toast.makeText(getApplicationContext(),"创建数据失败:" + e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
}else {
Toast.makeText(getApplicationContext(), "上传失败--", Toast.LENGTH_SHORT).show();
}
}
});
//注意:不能用save方法进行注册
finish();
break;
case R.id.head_portrait:
if(ContextCompat.checkSelfPermission(RegistActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(RegistActivity.this,new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE
},1);
}
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMAGE_REQUEST_CODE);
break;
case R.id.bt_regist_cancel:
Toast.makeText(this,"hhh",Toast.LENGTH_SHORT).show();
finish();
break;
default:
break;
}
}
/**
* 从相册获得图片
*/
/*
定义一个Handler,定义延时执行的行为
*/
public void chnage(){
new Thread(){
@Override
public void run() {
while ( bitmap == null ){
bitmap = BitmapFactory.decodeFile(path);
}
Message message = handler.obtainMessage();
message.obj = 0;
handler.sendMessage(message);
}
}.start();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//在相册里面选择好相片之后调回到现在的这个activity中
switch (requestCode) {
case IMAGE_REQUEST_CODE://这里的requestCode是我自己设置的,就是确定返回到那个Activity的标志
if (resultCode == RESULT_OK) {//resultcode是setResult里面设置的code值
try {
Uri selectedImage = data.getData(); //获取系统返回的照片的Uri
uri = selectedImage;
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);//从系统表中查询指定Uri对应的照片
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
path = cursor.getString(columnIndex); //获取照片路径
int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imgpath = cursor.getString(index);
cursor.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
bitmap = BitmapFactory.decodeFile(path,options);
imageView01.setImageBitmap(bitmap);
chnage();
Toast.makeText(RegistActivity.this,path,Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
break;
}
}
}
这里是登陆界面的几个数据成员:
//用户头像
ImageView imageView01 ;
//昵称
EditText et_regist_user;
//密码
EditText et_regist_password;
//再次输入密码
EditText et_regist_againpassword;
//真实姓名
EditText et_regist_fullname;
//组别
EditText et_regist_group;
//手机号
EditText et_regist_telephone;
//提交注册信息与取消
Button bt_regist_save,bt_regist_cancel;
//记录数组长度
int arr_num = 0;
//定义一个数组
String[] arr1 = new String[arr_num];
//从相册获得图片
Bitmap bitmap;
//判断返回到的Activity
private static final int IMAGE_REQUEST_CODE = 0;
//图片路径
private String path ;
//图片路径
Uri uri ;
//图片上传路径
String imgpath;
//用于延时处理
private Han