基础登录界面+第三方登录代码片

来源:互联网 发布:京津冀生态环境数据 编辑:程序博客网 时间:2024/06/11 22:34
package com.example.jd.Activitys;import android.app.Activity;import android.content.DialogInterface;import android.content.Intent;import android.content.SharedPreferences;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import com.example.jd.Beans.UserBean;import com.example.jd.Interface.OnNetView;import com.example.jd.Interface.OnPostNetView;import com.example.jd.Presenters.OkHttpPresenter;import com.example.jd.R;import com.tencent.connect.UserInfo;import com.tencent.connect.auth.QQToken;import com.tencent.connect.common.Constants;import com.tencent.tauth.IUiListener;import com.tencent.tauth.Tencent;import com.tencent.tauth.UiError;import org.json.JSONException;import org.json.JSONObject;import java.util.HashMap;import java.util.regex.Pattern;public class LoginActivity extends AppCompatActivity implements View.OnClickListener,OnPostNetView,OnNetView {    private EditText name;    private EditText psd;    private Button login;    private OkHttpPresenter presenter;    private TextView zhu_phone;    private ImageView qqLogin,jiantou;    //第三方QQ    private static final String TAG = "MainActivity";    private static final String APP_ID = "1105602574";//官方获取的APPID    private Tencent mTencent;    private BaseUiListener mIUiListener;    private UserInfo mUserInfo;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        //第三方QQ登录        //传入参数APPID和全局Context上下文        mTencent = Tencent.createInstance(APP_ID,LoginActivity.this.getApplicationContext());        initview();        login.setOnClickListener(this);        zhu_phone.setOnClickListener(this);        qqLogin.setOnClickListener(this);        jiantou.setOnClickListener(this);    }    //初始化组件    private void initview() {        name = (EditText) findViewById(R.id.login_name);        psd = (EditText) findViewById(R.id.login_password);        login = (Button) findViewById(R.id.login_login);        qqLogin = (ImageView) findViewById(R.id.Login_QQ);        jiantou =(ImageView) findViewById(R.id.login_jian);        zhu_phone = (TextView) findViewById(R.id.Zhuce_phone);        presenter = new OkHttpPresenter(this, this);    }    //点击事件    @Override    public void onClick(View view) {        switch (view.getId()){            //登陆的按钮            case R.id.login_login:                String username = name.getText().toString().trim();                String userpsd = psd.getText().toString().trim();                //添加请求数据                HashMap<String, String> map = new HashMap<>();                map.put("mobile",username);                map.put("password",userpsd);                if(panduannull(username,userpsd)){                    if(isPhone(username)){                        presenter.post("http://120.27.23.105/user/login",map,"login",UserBean.class);                    }else{                        Toast.makeText(LoginActivity.this,"手机格式不正确!",Toast.LENGTH_SHORT).show();                    }                }                break;            //注册            case R.id.Zhuce_phone:                Intent intent = new Intent(LoginActivity.this, ZhuCeActivity.class);                startActivity(intent);                finish();                break;            //QQ登录            case R.id.Login_QQ:                mIUiListener = new BaseUiListener();                //all表示获取所有权限                mTencent.login(LoginActivity.this, "all", mIUiListener);                break;            case R.id.login_jian:                finish();                break;        }    }    //登陆非空判断    public Boolean panduannull(String username,String userpsd){        if(TextUtils.isEmpty(username)){            Toast.makeText(LoginActivity.this,"账户名为空!",Toast.LENGTH_SHORT).show();            return  false;        }        if(TextUtils.isEmpty(userpsd)){            Toast.makeText(LoginActivity.this,"密码为空!",Toast.LENGTH_SHORT).show();            return false;        }        return  true;    }    //手机号判断    public Boolean isPhone(String username){        //正则表达式        String regex="^1[3|4|5|7|8]\\d{9}";        return Pattern.matches(regex,username);    }    //登陆的post请求    @Override    public void onNetPostSuccess(Object o, String tag) {        if(tag =="login"){            UserBean userBean = (UserBean) o;            String msg = userBean.getMsg();            if(msg.equals("登录成功")){                Toast.makeText(LoginActivity.this,"登陆成功!",Toast.LENGTH_SHORT).show();                //登录成功保存ID                SharedPreferences sharedPreferences = getSharedPreferences("User", Activity.MODE_PRIVATE);                SharedPreferences.Editor edit = sharedPreferences.edit();                edit.putString("UserID",userBean.getData().getUid()+"");                edit.putBoolean("UserShow",true);                edit.commit();                Intent intent = new Intent(LoginActivity.this, MainActivity.class);                startActivity(intent);                finish();            }else if(msg.equals("天呢!密码错误")){                Toast.makeText(LoginActivity.this,"天呢!密码错误",Toast.LENGTH_SHORT).show();            }else if(msg.equals("天呢!用户不存在")){                AlertDialog.Builder builder = new AlertDialog.Builder(this);                builder.setTitle("京东");                builder.setMessage("是否去注册?");                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialogInterface, int i) {                        Intent intent = new Intent(LoginActivity.this, ZhuCeActivity.class);                        startActivity(intent);                    }                });                builder.setNegativeButton("否", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialogInterface, int i) {                    }                });                builder.show();                Toast.makeText(LoginActivity.this,"天呢!用户不存在",Toast.LENGTH_SHORT).show();            }        }    }    @Override    public void onNetPostError(String e) {    }    private class BaseUiListener implements IUiListener {        @Override        public void onComplete(Object response) {            Toast.makeText(LoginActivity.this, "授权成功", Toast.LENGTH_SHORT).show();            Log.e(TAG, "response:" + response);            JSONObject obj = (JSONObject) response;            try {                String openID = obj.getString("openid");                String accessToken = obj.getString("access_token");                String expires = obj.getString("expires_in");                mTencent.setOpenId(openID);                mTencent.setAccessToken(accessToken,expires);                QQToken qqToken = mTencent.getQQToken();                mUserInfo = new UserInfo(getApplicationContext(),qqToken);                mUserInfo.getUserInfo(new IUiListener() {                    @Override                    public void onComplete(Object response) {                        Log.e(TAG,"登录成功"+response.toString());                    }                    @Override                    public void onError(UiError uiError) {                        Log.e(TAG,"登录失败"+uiError.toString());                    }                    @Override                    public void onCancel() {                        Log.e(TAG,"登录取消");                    }                });            } catch (JSONException e) {                e.printStackTrace();            }        }        @Override        public void onError(UiError uiError) {            Toast.makeText(LoginActivity.this, "授权失败", Toast.LENGTH_SHORT).show();        }        @Override        public void onCancel() {            Toast.makeText(LoginActivity.this, "授权取消", Toast.LENGTH_SHORT).show();        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if(requestCode == Constants.REQUEST_LOGIN){            Tencent.onActivityResultData(requestCode,resultCode,data,mIUiListener);        }        super.onActivityResult(requestCode, resultCode, data);    }    //get请求    @Override    public void onNetSuccess(Object data, String tag) {    }    @Override    public void onNetError(String e) {    }    //防止内存溢出    @Override    protected void onDestroy() {        super.onDestroy();        presenter.guanOkHttpPresenter();    }

}

//布局文件

<?xml version="1.0" encoding="utf-8"?><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:orientation="vertical"    tools:context="com.example.jd.Activitys.LoginActivity">    <ImageView        android:layout_width="wrap_content"        android:layout_height="20dp"        android:src="@mipmap/icon_leftjian"        android:id="@+id/login_jian"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="30dp"        >        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="登录界面"            android:textSize="20dp"            android:textColor="#0e0e0e"            android:gravity="center_horizontal"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:orientation="vertical"        android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="130dp"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="账号"            android:layout_marginLeft="15dp"            android:textSize="15dp"            />        <EditText            android:layout_width="match_parent"            android:layout_height="wrap_content"            style="?android:attr/textViewStyle"            android:layout_marginLeft="15dp"            android:background="@null"            android:id="@+id/login_name"            />    </LinearLayout>    <View        android:layout_width="match_parent"        android:layout_height="2dp"        android:background="#ae9f9a"        android:layout_marginTop="5dp"        ></View>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="密码"                android:layout_marginLeft="15dp"                android:textSize="15dp"                />            <EditText                android:layout_width="match_parent"                android:layout_height="wrap_content"                style="?android:attr/textViewStyle"                android:layout_marginLeft="15dp"                android:background="@null"                android:id="@+id/login_password"                />        </LinearLayout>        <View            android:layout_width="match_parent"            android:layout_height="2dp"            android:background="#ae9f9a"            android:layout_marginTop="5dp"            ></View>        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"           android:layout_margin="15dp"            android:text="登录"            android:id="@+id/login_login"            />        <LinearLayout            android:layout_width="match_parent"            android:orientation="horizontal"            android:layout_height="wrap_content">            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="手机号快速注册"                android:layout_marginLeft="15dp"                android:layout_weight="1"                android:id="@+id/Zhuce_phone"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="忘记密码"                android:layout_marginRight="15dp"                />        </LinearLayout>    </LinearLayout>    <LinearLayout        android:layout_alignParentBottom="true"        android:layout_width="match_parent"        android:orientation="horizontal"        android:id="@+id/Login_linear"        android:layout_height="wrap_content">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="登录代表你已经同意"            android:layout_marginRight="0dp"            android:layout_weight="1"          android:gravity="right"            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="杨栓隐私政策"            android:layout_weight="1"            android:textColor="#ff3600"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:orientation="vertical"        android:layout_above="@+id/Login_linear"        android:layout_marginBottom="30dp"        android:layout_height="wrap_content">        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@mipmap/qq"            android:layout_gravity="center_horizontal"            android:id="@+id/Login_QQ"            />/        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="第三方登录"            android:textSize="15dp"            android:textColor="#00aeff"            android:layout_gravity="center_horizontal"            />    </LinearLayout></RelativeLayout>


原创粉丝点击