码迷,mamicode.com
首页 > 移动开发 > 详细

android 练习之路 (三)

时间:2017-03-23 23:24:47      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:com   version   class   extern   tree   access   lan   call   maven仓库   

项目的github地址:https://github.com/Qunter/SearchAndCall

------------------------------------------------------------------------

今天开始实现登录和注册的功能

先贴最终效果吧

技术分享

PS:其实最后那一下,按照代码逻辑是不会返回到登录页面的,此处会返回应该是我没有删除之前的程序,所以在测试的时候就没有覆盖掉这些,所以导致这个finish代码没有执行

 

下面开始操作:

首先按照官网给的提示,在项目中加入如下代码以集成bmob的sdk

1,在 Project 的 build.gradle 文件中添加 Bmob的maven仓库地址

allprojects {

 repositories {
     jcenter()
     //Bmob的maven仓库地址--必填
     maven { url "https://raw.github.com/bmob/bmob-android-sdk/master" }
 }
}

2,在appbuild.gradle文件中添加compile依赖文件(我只添加了sdk以及兼容6.0的http-legacy1.0包)

dependencies {
    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
//bmob-sdk:Bmob的android sdk包,包含了Bmob的数据存储、文件等服务,以下是最新的bmob-sdk:3.5.3
    compile ‘cn.bmob.android:bmob-sdk:3.5.3‘

    //bmob-push:Bmob的推送包
    compile ‘cn.bmob.android:bmob-push:0.8‘

    //bmob-im:Bmob的即时通讯包,注意每个版本的im依赖特定版本的bmob-sdk
    compile ‘cn.bmob.android:bmob-im:2.0.5@aar‘
    compile ‘cn.bmob.android:bmob-sdk:3.4.7-aar‘

    //bmob-sms :Bmob单独为短信服务提供的包
    compile ‘cn.bmob.android:bmob-sms:1.0.1‘

    //如果你想应用能够兼容Android6.0,请添加此依赖(org.apache.http.legacy.jar)
    compile ‘cn.bmob.android:http-legacy:1.0‘
}

然后sync一下,bmob的sdk就算是导入了

但是光导入sdk还是用不了,因为没有给他权限,所以manifests里加个权限

  <!--允许联网 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--获取wifi网络状态的信息 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!--获取sd卡写的权限,用于文件上传和下载-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--允许读取手机状态 用于创建BmobInstallation-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

再然后改一下之前的登录页面,之前有些命名要统一一下,然后没有加id的要加个id方便事件处理

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:src="@drawable/backgroundlogo"
        android:layout_marginTop="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="170dp"
        android:background="@drawable/background_white"
        android:orientation="vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp">

        <EditText
            android:id="@+id/login_usernameEt"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:singleLine="true"
            android:hint="登录帐号"
            android:drawableRight="@drawable/id"
            android:drawablePadding="5dp"/>

        <EditText
            android:id="@+id/login_passwordEt"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:singleLine="true"
            android:drawableRight="@drawable/password"
            android:drawablePadding="5dp"
            android:inputType="textPassword"
            android:hint="登录密码"/>

        <Button
            android:id="@+id/login_loginBtn"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/ripple_button"
            android:textColor="#ffffff"
            android:text="登录"/>
        <Button
            android:id="@+id/login_registerBtn"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/ripple_button"
            android:textColor="#ffffff"
            android:text="注册"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:background="@null"
            android:textColor="#0099ff"
            android:text="忘记密码"/>

    </LinearLayout>

</RelativeLayout>

然后既然今天还要做注册,那么注册页也得弄一个

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">
    <ImageView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:src="@drawable/backgroundlogo"
        android:layout_marginTop="50dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="170dp"
        android:background="@drawable/background_white"
        android:orientation="vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp">

        <EditText
            android:id="@+id/register_usernameEt"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:singleLine="true"
            android:hint="请输入账号"
            android:drawablePadding="5dp"/>

        <EditText
            android:id="@+id/register_passwordEt"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:singleLine="true"
            android:drawablePadding="5dp"
            android:inputType="textPassword"
            android:hint="请输入密码"/>

        <EditText
            android:id="@+id/register_userPhoneEt"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:singleLine="true"
            android:drawablePadding="5dp"
            android:hint="请输入手机号"/>
        <EditText
            android:id="@+id/register_userNicknameEt"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:singleLine="true"
            android:drawablePadding="5dp"
            android:hint="请输入昵称"/>
        <Button
            android:id="@+id/register_registerBtn"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/ripple_button"
            android:textColor="#ffffff"
            android:text="注册"/>

        <Button
            android:id="@+id/register_loginBtn"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="16dp"
            android:background="@null"
            android:textColor="#0099ff"
            android:text="已有账号  立即登录"/>

    </LinearLayout>

</RelativeLayout>

再然后得建一个注册的Activity

RegisterActivity.java

public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private EditText usernameEt,passwordEt,userPhoneEt,userNicknameEt;
    private Button registerBtn,loginBtn;
    private UserIfmt userInformation;
    @Override
    protected void initVariablesAndService() {
        Bmob.initialize(this,"8da888d03200ff2f6b403d064b805d60");
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        usernameEt = (EditText) findViewById(R.id.register_usernameEt);
        passwordEt = (EditText) findViewById(R.id.register_passwordEt);
        userPhoneEt = (EditText) findViewById(R.id.register_userPhoneEt);
        userNicknameEt = (EditText) findViewById(R.id.register_userNicknameEt);
        registerBtn = (Button) findViewById(R.id.register_registerBtn);
        registerBtn.setOnClickListener(this);
        loginBtn = (Button) findViewById(R.id.register_loginBtn);
        loginBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_registerBtn:
                register();
                break;
            case R.id.register_loginBtn:
                startActivity(LoginActivity.class);
                this.finish();
                break;
        }
    }
    /**
     * 注册账号至bmob
     */
    private void register(){
        userInformation = new UserIfmt();
        userInformation.setUsername(usernameEt.getText().toString().trim());
        userInformation.setPassword(passwordEt.getText().toString().trim());
        userInformation.setUserPhone(userPhoneEt.getText().toString().trim());
        userInformation.setUserNickname(userNicknameEt.getText().toString().trim());
        userInformation.signUp(new SaveListener<UserIfmt>() {
            @Override
            public void done(UserIfmt s, BmobException e) {
                if(e==null){
                    Toast.makeText(RegisterActivity.this,"注册成功", Toast.LENGTH_SHORT).show();
                    userInformation.login(new SaveListener<UserIfmt>() {
                        @Override
                        public void done(UserIfmt userIfmt, BmobException e) {
                            if(e==null){
                                Toast.makeText(RegisterActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
                                startActivity(MainActivity.class);
                                RegisterActivity.this.finish();
                            }
                        }
                    });
                }else{
                    Log.e("失败代码",e.toString());
                }

            }
        });
    }
}

然后转头一看,之前的登录activity还没加事件处理呢,改一下

LoginActivity.java

public class LoginActivity extends BaseActivity implements View.OnClickListener{
    private EditText usernameEt,passwordEt;
    private Button loginBtn,registerBtn;
    @Override
    protected void initVariablesAndService() {
        Bmob.initialize(this,"8da888d03200ff2f6b403d064b805d60");
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        usernameEt = (EditText) findViewById(R.id.login_usernameEt);
        passwordEt = (EditText) findViewById(R.id.login_passwordEt);
        loginBtn = (Button) findViewById(R.id.login_loginBtn);
        loginBtn.setOnClickListener(this);
        registerBtn = (Button) findViewById(R.id.login_registerBtn);
        registerBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_loginBtn:
                login();
                break;
            case R.id.login_registerBtn:
                startActivity(RegisterActivity.class);
                this.finish();
                break;
        }
    }
    /**
     * 在bmob数据库上验证账号并登录
     */
    private void login(){
        UserIfmt userInformation = new UserIfmt();
        userInformation.setUsername(usernameEt.getText().toString());
        userInformation.setPassword(passwordEt.getText().toString());
        userInformation.login(new SaveListener<UserIfmt>() {
            @Override
            public void done(UserIfmt userIfmt, BmobException e) {
                if(e==null){
                    Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
                    startActivity(MainActivity.class);
                    LoginActivity.this.finish();
                }else{
                    Toast.makeText(LoginActivity.this,"账号或密码错误  请重新输入", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

这里要说一下,暂时先不用缓存登录(自动登录),因为退出登录还没有做,所以这里如果用的话对后面账号数据会有影响

那么这里面用了个MainActivity,之所以有这个类,是因为方便测试今天的登录结果,所以今天暂时建一下,只要很简单的显示个文字,让我知道成功跳转页面了就行

MainActivity.java

public class MainActivity extends BaseActivity {
    @Override
    protected void initVariablesAndService() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="主页"
        android:textSize="24sp"
        android:layout_centerInParent="true"/>
</RelativeLayout>

这个时候肯定是运行不起来的,因为还没有到manifests里注册页面

<activity android:name=".activity.RegisterActivity"/>
<activity android:name=".activity.MainActivity"/>

完事,最终的结果就出来了

今天就到这了

android 练习之路 (三)

标签:com   version   class   extern   tree   access   lan   call   maven仓库   

原文地址:http://www.cnblogs.com/Qunter/p/6606145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!