色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

android 仿微信demo——登錄功能實現(xiàn)(移動端)

瀏覽:86日期:2022-06-03 18:32:48
目錄移動端登錄功能實現(xiàn)測試移動端登錄功能實現(xiàn)

登錄功能基本和注冊一樣,唯一不同的是登錄可以實現(xiàn)兩種登錄方式(微信號和手機號),也就是布局不一樣。所以需要兩個布局,兩個activity(這個方法比較簡單粗暴,我懶。也可以通過activity動態(tài)切換布局,這樣只需要一個activity就可以了)

創(chuàng)建兩個activity,實現(xiàn)兩種登錄方式

微信號登錄activity

LoginUser.java

package com.example.wxchatdemo;import android.annotation.SuppressLint;import android.app.AlertDialog;import android.content.Intent;import android.graphics.Color;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;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.wxchatdemo.tools.IEditTextChangeListener;import com.example.wxchatdemo.tools.WorksSizeCheckUtil;import org.json.JSONObject;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;public class LoginUser extends AppCompatActivity { //聲明組件變量 private EditText weixinNumber; private EditText password; private TextView phone_login; private Button button; //自定義的一個Hander消息機制 private MyHander myhander = new MyHander(); @Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login_user); //設(shè)置布局/* 隱藏自帶標題*/ActionBar actionBar = getSupportActionBar();if (actionBar != null) { actionBar.hide();}if (Build.VERSION.SDK_INT >= 21) { View decorView = getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //全屏顯示 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; //因為背景為淺色所以將狀態(tài)欄字體設(shè)置為黑色 decorView.setSystemUiVisibility(option); getWindow().setStatusBarColor(Color.TRANSPARENT);}initViews(); // 初始化布局元素/*獲取注冊activity傳過來的微信號*/Intent intent = getIntent();String number = intent.getStringExtra('weixin_number');//把傳過來的值顯示在登錄布局上weixinNumber.setText(number);// 設(shè)置注冊按鈕是否可點擊if (weixinNumber.getText() + '' == '' || password.getText() + '' == '') { button.setEnabled(false);} else { button.setEnabled(true);}inputFocus(); //監(jiān)聽EditView變色buttonChangeColor(); //登錄按鈕變色// 設(shè)置手機號登錄的監(jiān)聽器phone_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//跳轉(zhuǎn)到手機號登錄的activityIntent intent=new Intent(LoginUser.this,LoginPhone.class);startActivity(intent); }});//button的點擊事件button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//創(chuàng)建一個進度條的activity,通過AndroidMainfest.xml文件聲明為對話框,這樣activity就不會覆蓋當前的activityIntent intent = new Intent();intent.setClass(LoginUser.this, Loading.class);startActivity(intent);// 開一個線程完成網(wǎng)絡(luò)請求操作new Thread(new Runnable() { @Override public void run() {try { Thread.sleep(1000); httpUrlConnPost(LoginUser.this.weixinNumber.getText() + '', password.getText() + '');} catch (InterruptedException e) { e.printStackTrace();} }}).start(); }}); } @SuppressLint('NewApi') public void initViews() {// 得到所有的組件weixinNumber = (EditText) this.findViewById(R.id.log_weixin_number);password = (EditText) this.findViewById(R.id.log_passwd);phone_login = (TextView) this.findViewById(R.id.phone_log);button = (Button) this.findViewById(R.id.log_button); } public void inputFocus() {weixinNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) {if (hasFocus) { // 此處為得到焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver1); imageView.setBackgroundResource(R.color.input_dvier_focus);} else { // 此處為失去焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver1); imageView.setBackgroundResource(R.color.input_dvier);} }});password.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) {if (hasFocus) { // 此處為得到焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier_focus);} else { // 此處為失去焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier);} }}); } public void buttonChangeColor() {//創(chuàng)建工具類對象 把要改變顏色的Button先傳過去WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(button);textChangeListener.addAllEditText(weixinNumber, password);//把所有要監(jiān)聽的EditText都添加進去//接口回調(diào) 在這里拿到boolean變量 根據(jù)isHasContent的值決定 Button應(yīng)該設(shè)置什么顏色WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() { @Override public void textChange(boolean isHasContent) {if (isHasContent) { button.setBackgroundResource(R.drawable.login_button_focus); button.setTextColor(getResources().getColor(R.color.loginButtonTextFouse));} else { button.setBackgroundResource(R.drawable.login_button_shape); button.setTextColor(getResources().getColor(R.color.loginButtonText));} }}); } // 發(fā)送請求的主要方法 public void httpUrlConnPost(String number, String password) {HttpURLConnection urlConnection = null;URL url;try { // 請求的URL地地址 url = new URL( 'http://100.2.178.10:8080/AndroidServer_war_exploded/Login'); urlConnection = (HttpURLConnection) url.openConnection();// 打開http連接 urlConnection.setConnectTimeout(3000);// 連接的超時時間 urlConnection.setUseCaches(false);// 不使用緩存 // urlConnection.setFollowRedirects(false);是static函數(shù),作用于所有的URLConnection對象。 urlConnection.setInstanceFollowRedirects(true);// 是成員函數(shù),僅作用于當前函數(shù),設(shè)置這個連接是否可以被重定向 urlConnection.setReadTimeout(3000);// 響應(yīng)的超時時間 urlConnection.setDoInput(true);// 設(shè)置這個連接是否可以寫入數(shù)據(jù) urlConnection.setDoOutput(true);// 設(shè)置這個連接是否可以輸出數(shù)據(jù) urlConnection.setRequestMethod('POST');// 設(shè)置請求的方式 urlConnection.setRequestProperty('Content-Type', 'application/json;charset=UTF-8');// 設(shè)置消息的類型 urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實際上它只是建立了一個與服務(wù)器的TCP連接 JSONObject json = new JSONObject();// 創(chuàng)建json對象 json.put('number', URLEncoder.encode(number, 'UTF-8'));// 使用URLEncoder.encode對特殊和不可見字符進行編碼 json.put('password', URLEncoder.encode(password, 'UTF-8'));// 把數(shù)據(jù)put進json對象中 String jsonstr = json.toString();// 把JSON對象按JSON的編碼格式轉(zhuǎn)換為字符串 // ------------字符流寫入數(shù)據(jù)------------ OutputStream out = urlConnection.getOutputStream();// 輸出流,用來發(fā)送請求,http請求實際上直到這個函數(shù)里面才正式發(fā)送出去 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));// 創(chuàng)建字符流對象并用高效緩沖流包裝它,便獲得最高的效率,發(fā)送的是字符串推薦用字符流,其它數(shù)據(jù)就用字節(jié)流 bw.write(jsonstr);// 把json字符串寫入緩沖區(qū)中 bw.flush();// 刷新緩沖區(qū),把數(shù)據(jù)發(fā)送出去,這步很重要 out.close(); bw.close();// 使用完關(guān)閉 Log.i('aa', urlConnection.getResponseCode() + ''); //以下判?嗍欠裨L??成功,如果返回的狀態(tài)碼是200則說明訪問成功 if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {// 得到服務(wù)端的返回碼是否連接成功// ------------字符流讀取服務(wù)端返回的數(shù)據(jù)------------InputStream in = urlConnection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(in));String str = null;StringBuffer buffer = new StringBuffer();while ((str = br.readLine()) != null) {// BufferedReader特有功能,一次讀取一行數(shù)據(jù) buffer.append(str);}in.close();br.close();JSONObject rjson = new JSONObject(buffer.toString());Log.i('aa', 'rjson=' + rjson);// rjson={'json':true}boolean result = rjson.getBoolean('json');// 從rjson對象中得到key值為'json'的數(shù)據(jù),這里服務(wù)端返回的是一個boolean類型的數(shù)據(jù)System.out.println('json:===' + result);//如果服務(wù)器端返回的是true,則說明登錄成功,否則登錄失敗if (result) {// 判斷結(jié)果是否正確 //在Android中http請求,必須放到線程中去作請求,但是在線程中不可以直接修改UI,只能通過hander機制來完成對UI的操作 myhander.sendEmptyMessage(1); Log.i('用戶:', '登錄成功');} else { myhander.sendEmptyMessage(2); System.out.println('222222222222222'); Log.i('用戶:', '登錄失敗');} } else {myhander.sendEmptyMessage(2); }} catch (Exception e) { e.printStackTrace(); Log.i('aa', e.toString()); System.out.println('11111111111111111'); myhander.sendEmptyMessage(2);} finally { urlConnection.disconnect();// 使用完關(guān)閉TCP連接,釋放資源} } // 在Android中不可以在線程中直接修改UI,只能借助Handler機制來完成對UI的操作 class MyHander extends Handler {@Overridepublic void handleMessage(Message msg) { super.handleMessage(msg); //判斷hander的內(nèi)容是什么,如果是1則說明登錄成功,如果是2說明登錄失敗 switch (msg.what) {case 1: Log.i('aa', msg.what + ''); //提示 Toast.makeText(getApplicationContext(), '登錄成功', Toast.LENGTH_SHORT).show(); //通過Intent跳轉(zhuǎn)到微信首頁,把微信號傳過去 Intent intent = new Intent(); intent.putExtra('weixin_number', weixinNumber.getText().toString()); intent.setClass(com.example.wxchatdemo.LoginUser.this, com.example.wxchatdemo.MainWeixin.class); startActivity(intent); com.example.wxchatdemo.LoginUser.this.finish(); //結(jié)束當前actitivy break;case 2: Log.i('aa', msg.what + ''); //對話框 new AlertDialog.Builder(com.example.wxchatdemo.LoginUser.this) .setTitle(' 登錄失敗') .setMessage(' 用戶名或密碼錯誤,請重新填寫') .setPositiveButton('確定', null) .show(); break; }} } //返回按鈕處理事件 public void login_activity_back(View v) {/*跳轉(zhuǎn)到微信啟動頁*/Intent intent = new Intent();intent.setClass(com.example.wxchatdemo.LoginUser.this, Welcome.class);startActivity(intent);com.example.wxchatdemo.LoginUser.this.finish(); //結(jié)束當前activity }}

微信號登錄activity對應(yīng)的布局文件

login_user.xml

<?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='@color/title' android:orientation='vertical'> <!--返回按鈕--> <ImageViewandroid: android:layout_width='17dp'android:layout_height='17dp'android:layout_marginLeft='20dp'android:layout_marginTop='45dp'android:onClick='login_activity_back'android:src='http://m.lshqa.cn/bcjs/@drawable/backpay' /> <!--標題--> <TextViewandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginLeft='30dp'android:layout_marginTop='45dp'android:text='微信號/QQ號/郵箱登錄'android:textColor='@color/loginText'android:textSize='25sp' /> <!--賬號輸入--> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='40dp'><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='30dp' android:text='賬號' android:textColor='@color/loginText' android:textSize='16sp' /><EditText android: android:layout_width='200dp' android:layout_height='wrap_content' android:layout_marginLeft='55dp' android:background='@null' android:hint='請?zhí)顚懳⑿盘?QQ號/郵箱' android:singleLine='true' android:textColorHint='@color/textColorHint' android:textCursorDrawable='@drawable/edit_cursor_color' android:textSize='16sp' /> </LinearLayout> <!--下劃線--> <ImageViewandroid: android:layout_width='320dp'android:layout_height='1dp'android:layout_gravity='center_horizontal'android:layout_marginTop='17dp'android:background='@color/input_dvier' /> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='40dp'><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='30dp' android:text='密碼' android:textColor='@color/loginText' android:textSize='16sp' /><EditText android: android:layout_width='200dp' android:layout_height='wrap_content' android:layout_marginLeft='55dp' android:password='true' android:background='@null' android:hint='請?zhí)顚懨艽a' android:singleLine='true' android:textColorHint='@color/textColorHint' android:textCursorDrawable='@drawable/edit_cursor_color' android:textSize='16sp' /> </LinearLayout> <!--下劃線--> <ImageViewandroid: android:layout_width='320dp'android:layout_height='1dp'android:layout_gravity='center_horizontal'android:layout_marginTop='17dp'android:background='@color/input_dvier' /> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='30dp' android:layout_marginTop='30dp' android:text='用手機號登錄' android:textColor='@color/massageLogin' android:textSize='17dp' /> </LinearLayout> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='40dp'android:gravity='center_horizontal'><!--登錄按鈕--><Button android: android:layout_width='321dp' android:layout_height='48dp' android:background='@drawable/login_button_shape' android:text='登錄' android:textColor='@color/loginButtonText' android:textSize='16sp' /> </LinearLayout> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='300dp'android:divider='@drawable/login_dvier'android:gravity='center_horizontal'android:showDividers='middle'><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingHorizontal='10dp' android:text='找回密碼' android:textColor='@color/massageLogin' android:textSize='14dp' /><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingHorizontal='10dp' android:text='緊急凍結(jié)' android:textColor='@color/massageLogin' android:textSize='14dp' /><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingHorizontal='10dp' android:text='微信安全中心' android:textColor='@color/massageLogin' android:textSize='14dp' /> </LinearLayout></LinearLayout>

手機號登錄activity

LoginPhone.java

package com.example.wxchatdemo;import android.annotation.SuppressLint;import android.app.AlertDialog;import android.content.Intent;import android.graphics.Color;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBar;import android.support.v7.app.AppCompatActivity;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.wxchatdemo.tools.IEditTextChangeListener;import com.example.wxchatdemo.tools.WorksSizeCheckUtil;import org.json.JSONObject;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;public class LoginPhone extends AppCompatActivity { //聲明組件變量 private EditText phone; private EditText password; private TextView user_login; private Button button; //自定義的一個Hander消息機制 private LoginPhone.MyHander myhander = new LoginPhone.MyHander(); @Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login_phone); //設(shè)置布局/* 隱藏自帶標題*/ActionBar actionBar = getSupportActionBar();if (actionBar != null) { actionBar.hide();}if (Build.VERSION.SDK_INT >= 21) { View decorView = getWindow().getDecorView(); int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //全屏顯示 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; //因為背景為淺色所以將狀態(tài)欄字體設(shè)置為黑色 decorView.setSystemUiVisibility(option); getWindow().setStatusBarColor(Color.TRANSPARENT);}initViews(); // 初始化布局元素// 設(shè)置注冊按鈕是否可點擊if (phone.getText() + '' == '' || password.getText() + '' == '') { button.setEnabled(false);} else { button.setEnabled(true);}inputFocus(); //監(jiān)聽EditView變色buttonChangeColor(); //登錄按鈕變色//設(shè)置通過微信號登錄的監(jiān)聽器user_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//跳轉(zhuǎn)到用微信號登錄的activityIntent intent = new Intent(LoginPhone.this, LoginUser.class);startActivity(intent); }});//button的點擊事件button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//創(chuàng)建一個進度條的activity,通過AndroidMainfest.xml文件聲明為對胡框,這樣activity就不會覆蓋當前的activityIntent intent = new Intent();intent.setClass(LoginPhone.this, Loading.class);startActivity(intent);// 開一個線程完成網(wǎng)絡(luò)請求操作new Thread(new Runnable() { @Override public void run() {try { Thread.sleep(1000); httpUrlConnPost(LoginPhone.this.phone.getText() + '', password.getText() + '');} catch (InterruptedException e) { e.printStackTrace();} }}).start(); }}); } @SuppressLint('NewApi') public void initViews() {// 得到所有的組件phone = (EditText) this.findViewById(R.id.log_phone);password = (EditText) this.findViewById(R.id.log_passwd);user_login = (TextView) this.findViewById(R.id.user_log);button = (Button) this.findViewById(R.id.log_button); } public void inputFocus() {phone.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) {if (hasFocus) { // 此處為得到焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver1); imageView.setBackgroundResource(R.color.input_dvier_focus);} else { // 此處為失去焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver1); imageView.setBackgroundResource(R.color.input_dvier);} }});password.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) {if (hasFocus) { // 此處為得到焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier_focus);} else { // 此處為失去焦點時的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier);} }}); } public void buttonChangeColor() {//創(chuàng)建工具類對象 把要改變顏色的Button先傳過去WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(button);textChangeListener.addAllEditText(phone, password);//把所有要監(jiān)聽的EditText都添加進去//接口回調(diào) 在這里拿到boolean變量 根據(jù)isHasContent的值決定 Button應(yīng)該設(shè)置什么顏色WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() { @Override public void textChange(boolean isHasContent) {if (isHasContent) { button.setBackgroundResource(R.drawable.login_button_focus); button.setTextColor(getResources().getColor(R.color.loginButtonTextFouse));} else { button.setBackgroundResource(R.drawable.login_button_shape); button.setTextColor(getResources().getColor(R.color.loginButtonText));} }}); } // 發(fā)送請求的主要方法 public void httpUrlConnPost(String phone, String password) {HttpURLConnection urlConnection = null;URL url;try { // 請求的URL地地址 url = new URL( 'http://100.2.178.10:8080/AndroidServer_war_exploded/Login'); urlConnection = (HttpURLConnection) url.openConnection();// 打開http連接 urlConnection.setConnectTimeout(3000);// 連接的超時時間 urlConnection.setUseCaches(false);// 不使用緩存 // urlConnection.setFollowRedirects(false);是static函數(shù),作用于所有的URLConnection對象。 urlConnection.setInstanceFollowRedirects(true);// 是成員函數(shù),僅作用于當前函數(shù),設(shè)置這個連接是否可以被重定向 urlConnection.setReadTimeout(3000);// 響應(yīng)的超時時間 urlConnection.setDoInput(true);// 設(shè)置這個連接是否可以寫入數(shù)據(jù) urlConnection.setDoOutput(true);// 設(shè)置這個連接是否可以輸出數(shù)據(jù) urlConnection.setRequestMethod('POST');// 設(shè)置請求的方式 urlConnection.setRequestProperty('Content-Type', 'application/json;charset=UTF-8');// 設(shè)置消息的類型 urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實際上它只是建立了一個與服務(wù)器的TCP連接 JSONObject json = new JSONObject();// 創(chuàng)建json對象 json.put('number', URLEncoder.encode(phone, 'UTF-8'));// 使用URLEncoder.encode對特殊和不可見字符進行編碼 json.put('password', URLEncoder.encode(password, 'UTF-8'));// 把數(shù)據(jù)put進json對象中 String jsonstr = json.toString();// 把JSON對象按JSON的編碼格式轉(zhuǎn)換為字符串 // ------------字符流寫入數(shù)據(jù)------------ OutputStream out = urlConnection.getOutputStream();// 輸出流,用來發(fā)送請求,http請求實際上直到這個函數(shù)里面才正式發(fā)送出去 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));// 創(chuàng)建字符流對象并用高效緩沖流包裝它,便獲得最高的效率,發(fā)送的是字符串推薦用字符流,其它數(shù)據(jù)就用字節(jié)流 bw.write(jsonstr);// 把json字符串寫入緩沖區(qū)中 bw.flush();// 刷新緩沖區(qū),把數(shù)據(jù)發(fā)送出去,這步很重要 out.close(); bw.close();// 使用完關(guān)閉 Log.i('aa', urlConnection.getResponseCode() + ''); //以下判?嗍欠裨L??成功,如果返回的狀態(tài)碼是200則說明訪問成功 if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {// 得到服務(wù)端的返回碼是否連接成功// ------------字符流讀取服務(wù)端返回的數(shù)據(jù)------------InputStream in = urlConnection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(in));String str = null;StringBuffer buffer = new StringBuffer();while ((str = br.readLine()) != null) {// BufferedReader特有功能,一次讀取一行數(shù)據(jù) buffer.append(str);}in.close();br.close();JSONObject rjson = new JSONObject(buffer.toString());Log.i('aa', 'rjson=' + rjson);// rjson={'json':true}boolean result = rjson.getBoolean('json');// 從rjson對象中得到key值為'json'的數(shù)據(jù),這里服務(wù)端返回的是一個boolean類型的數(shù)據(jù)System.out.println('json:===' + result);//如果服務(wù)器端返回的是true,則說明登錄成功,否則登錄失敗if (result) {// 判斷結(jié)果是否正確 //在Android中http請求,必須放到線程中去作請求,但是在線程中不可以直接修改UI,只能通過hander機制來完成對UI的操作 myhander.sendEmptyMessage(1); Log.i('用戶:', '登錄成功');} else { myhander.sendEmptyMessage(2); System.out.println('222222222222222'); Log.i('用戶:', '登錄失敗');} } else {myhander.sendEmptyMessage(2); }} catch (Exception e) { e.printStackTrace(); Log.i('aa', e.toString()); System.out.println('11111111111111111'); myhander.sendEmptyMessage(2);} finally { urlConnection.disconnect();// 使用完關(guān)閉TCP連接,釋放資源} } // 在Android中不可以在線程中直接修改UI,只能借助Handler機制來完成對UI的操作 class MyHander extends Handler {@Overridepublic void handleMessage(Message msg) { super.handleMessage(msg); //判斷hander的內(nèi)容是什么,如果是1則說明登錄成功,如果是2說明登錄失敗 switch (msg.what) {case 1: Log.i('aa', msg.what + ''); Toast.makeText(getApplicationContext(), '登錄成功', Toast.LENGTH_SHORT).show(); Intent intent = new Intent (com.example.wxchatdemo.LoginPhone.this, com.example.wxchatdemo.MainWeixin.class); startActivity(intent); com.example.wxchatdemo.LoginPhone.this.finish(); break;case 2: Log.i('aa', msg.what + ''); new AlertDialog.Builder(com.example.wxchatdemo.LoginPhone.this) .setTitle(' 登錄失敗') .setMessage(' 用戶名或密碼錯誤,請重新填寫') .setPositiveButton('確定', null) .show(); }} } //返回按鈕處理事件 public void login_activity_back(View v) {/*跳轉(zhuǎn)到微信啟動頁*/Intent intent = new Intent();intent.setClass(com.example.wxchatdemo.LoginPhone.this, Welcome.class);startActivity(intent);com.example.wxchatdemo.LoginPhone.this.finish(); //結(jié)束當前activity }}

手機號登錄activity對應(yīng)的布局文件

login_phone.xml

<?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='@color/title' android:orientation='vertical'> <!--返回按鈕--> <ImageViewandroid: android:layout_width='17dp'android:layout_height='17dp'android:layout_marginLeft='20dp'android:layout_marginTop='45dp'android:onClick='login_activity_back'android:src='http://m.lshqa.cn/bcjs/@drawable/backpay' /> <!--標題--> <TextViewandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginLeft='30dp'android:layout_marginTop='45dp'android:text='手機號登錄'android:textColor='@color/loginText'android:textSize='25sp' /> <!--賬號輸入--> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='40dp'><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='30dp' android:text='手機號' android:textColor='@color/loginText' android:textSize='16sp' /><EditText android: android:layout_width='200dp' android:layout_height='wrap_content' android:layout_marginLeft='35dp' android:background='@null' android:hint='請?zhí)顚懯謾C號' android:singleLine='true' android:textColorHint='@color/textColorHint' android:textCursorDrawable='@drawable/edit_cursor_color' android:textSize='16sp' /> </LinearLayout> <!--下劃線--> <ImageViewandroid: android:layout_width='320dp'android:layout_height='1dp'android:layout_gravity='center_horizontal'android:layout_marginTop='17dp'android:background='@color/input_dvier' /> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='40dp'><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='30dp' android:text='密碼' android:textColor='@color/loginText' android:textSize='16sp' /><EditText android: android:layout_width='200dp' android:layout_height='wrap_content' android:password='true' android:layout_marginLeft='55dp' android:background='@null' android:hint='請?zhí)顚懨艽a' android:singleLine='true' android:textColorHint='@color/textColorHint' android:textCursorDrawable='@drawable/edit_cursor_color' android:textSize='16sp' /> </LinearLayout> <!--下劃線--> <ImageViewandroid: android:layout_width='320dp'android:layout_height='1dp'android:layout_gravity='center_horizontal'android:layout_marginTop='17dp'android:background='@color/input_dvier' /> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='30dp' android:layout_marginTop='30dp' android:text='用微信號/QQ號/郵箱登錄' android:textColor='@color/massageLogin' android:textSize='17dp' /> </LinearLayout> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='40dp'android:gravity='center_horizontal'><!--登錄按鈕--><Button android: android:layout_width='321dp' android:layout_height='48dp' android:background='@drawable/login_button_shape' android:text='登錄' android:textColor='@color/loginButtonText' android:textSize='16sp' /> </LinearLayout> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='150dp'android:divider='@drawable/login_dvier'android:gravity='center_horizontal'android:showDividers='middle'><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingHorizontal='10dp' android:text='找回密碼' android:textColor='@color/massageLogin' android:textSize='14dp' /><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingHorizontal='10dp' android:text='緊急凍結(jié)' android:textColor='@color/massageLogin' android:textSize='14dp' /><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingHorizontal='10dp' android:text='微信安全中心' android:textColor='@color/massageLogin' android:textSize='14dp' /> </LinearLayout></LinearLayout>

創(chuàng)建一個shapre文件login_dvier.xml,自定義豎直分割線

login_dvier.xml

<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android' > <solid android:color='@color/login_dvier' /> <size android:height='1dp'></size> <size android:width='1dp'></size></shape>

上面兩個登錄activity都實現(xiàn)了一個自定義的等待框activity,當點擊登錄按鈕時,便會跳轉(zhuǎn)到這個activity,但是自定義的activity會覆蓋原有的界面。而微信點擊登錄按鈕后會彈出一個等待框且不會覆蓋原有的activity(即原有界面),所以要給自定義的等待框activity在Androidfest.xml文件配置為對話框,這樣就不會覆蓋原有activity.

創(chuàng)建activity Loading.java ,實現(xiàn)自定義等待框

Loading.java

package com.example.wxchatdemo;import android.app.Activity;import android.os.Bundle;import android.os.Handler;public class Loading extends Activity { @Override public void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.loading); //設(shè)置布局//一秒后結(jié)束當前activitynew Handler().postDelayed(new Runnable() { @Override public void run() {Loading.this.finish(); }}, 1000); }}

創(chuàng)建 activity Loading.java對應(yīng)的布局文件loading.xml

loading.xml

<RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent'> <RelativeLayoutandroid:layout_width='180dp'android:layout_height='180dp'android:layout_centerInParent='true'android:background='@drawable/loading_bg'><LinearLayout android:layout_width='fill_parent' android:layout_height='fill_parent' android:gravity='center' android:orientation='vertical'> <ProgressBarandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_gravity='center_horizontal' /> <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_marginTop='10dp'android:text='正在登錄'android:textColor='#fff'android:textSize='20sp' /></LinearLayout> </RelativeLayout></RelativeLayout>

在AndroidMainfest.xml文件中配置自定義等待框activity Loading.java 為對話框

<activity android:name='.Loading' android:theme='@style/MyDialogStyle' />

android 仿微信demo——登錄功能實現(xiàn)(移動端)

上面用到的主題theme是自定義的主題,把activity轉(zhuǎn)化為對話框,這樣就不會覆蓋原有的activity,下面會給出如何定義自定義主題

創(chuàng)建樣式styles.xml文件,實現(xiàn)自定義主題

android 仿微信demo——登錄功能實現(xiàn)(移動端)

android 仿微信demo——登錄功能實現(xiàn)(移動端)

styles.xml

<?xml version='1.0' encoding='utf-8'?><resources> <style name='MyDialogStyle'><item name='android:windowBackground'>@android:color/transparent</item><item name='android:windowFrame'>@null</item><item name='android:windowNoTitle'>true</item><item name='android:windowIsFloating'>true</item><item name='android:windowIsTranslucent'>true</item><item name='android:windowContentOverlay'>@null</item><item name='android:windowAnimationStyle'>@android:style/Animation.Dialog</item><item name='android:backgroundDimEnabled'>true</item> </style></resources>

在colors.xml聲明用到的顏色

colors.xml

<color name='massageLogin'>#5A6A8B</color> <color name='login_dvier'>#BEBEBE</color>

在AndroidMainfest.xml文件中聲明創(chuàng)建的activity

android 仿微信demo——登錄功能實現(xiàn)(移動端)

測試

雖然服務(wù)端登錄表單處理功能還沒寫,但是還是可以測試上面的效果

把以往文章中點擊登陸按鈕注釋代碼取消注釋

android 仿微信demo——登錄功能實現(xiàn)(移動端)

把兩個activity登錄成功后跳轉(zhuǎn)activity那段代碼段注釋掉,啟動項目測試

android 仿微信demo——登錄功能實現(xiàn)(移動端)

android 仿微信demo——登錄功能實現(xiàn)(移動端)

到此這篇關(guān)于android 仿微信demo——登錄功能實現(xiàn)(移動端)的文章就介紹到這了,更多相關(guān)android仿微信登錄內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: 微信
相關(guān)文章:
主站蜘蛛池模板: 97视频免费观看 | 亚洲性视频在线 | 国内精品久久久久久网站 | 国产成人精品视频午夜 | 欧美亚洲中日韩中文字幕在线 | 国产一级小视频 | 久久黄色影院 | 9999毛片免费看 | 一级毛片免费观看不卡的 | av片免费大全在线观看不卡 | 国产aⅴ精品一区二区三区久久 | 99re思思 | 成人软件网18免费视频 | 国产2区| wwwav视频| 99热久久国产这里是精品 | 成人毛片在线观看 | 欧美一级毛片久久精品 | 国产精品久久久久久久y | 欧美特欧美特级一片 | 男人扒开双腿女人爽视频免费 | 国产一区二区三区不卡免费观看 | 久久精品国产精品青草不卡 | 亚洲国产一区二区三区四区 | 日韩毛片大全免费高清 | 一级特色黄大片 | 狠狠色丁香婷婷综合小时婷婷 | 免费鲁丝片一级观看 | 精品国产综合成人亚洲区 | 美女视频网站色 | 日本一区二区三区在线 视频观看免费 | 久久国产精品成人免费 | 99久久综合 | 四川一级片 | 国产毛片一级 | 日本在线观看免费视频网址 | 国产在线一区二区三区四区 | 一区二区精品在线观看 | 欧美在线观看成人高清视频 | 天堂亚洲网 | 一区欧美 |