电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:125日期: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); //設置布局/* 隱藏自帶標題*/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)欄字體設置為黑色 decorView.setSystemUiVisibility(option); getWindow().setStatusBarColor(Color.TRANSPARENT);}initViews(); // 初始化布局元素/*獲取注冊activity傳過來的微信號*/Intent intent = getIntent();String number = intent.getStringExtra('weixin_number');//把傳過來的值顯示在登錄布局上weixinNumber.setText(number);// 設置注冊按鈕是否可點擊if (weixinNumber.getText() + '' == '' || password.getText() + '' == '') { button.setEnabled(false);} else { button.setEnabled(true);}inputFocus(); //監(jiān)聽EditView變色buttonChangeColor(); //登錄按鈕變色// 設置手機號登錄的監(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)絡請求操作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應該設置什么顏色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ù),設置這個連接是否可以被重定向 urlConnection.setReadTimeout(3000);// 響應的超時時間 urlConnection.setDoInput(true);// 設置這個連接是否可以寫入數(shù)據(jù) urlConnection.setDoOutput(true);// 設置這個連接是否可以輸出數(shù)據(jù) urlConnection.setRequestMethod('POST');// 設置請求的方式 urlConnection.setRequestProperty('Content-Type', 'application/json;charset=UTF-8');// 設置消息的類型 urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實際上它只是建立了一個與服務器的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();// 使用完關閉 Log.i('aa', urlConnection.getResponseCode() + ''); //以下判?嗍欠裨L??成功,如果返回的狀態(tài)碼是200則說明訪問成功 if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {// 得到服務端的返回碼是否連接成功// ------------字符流讀取服務端返回的數(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ù),這里服務端返回的是一個boolean類型的數(shù)據(jù)System.out.println('json:===' + result);//如果服務器端返回的是true,則說明登錄成功,否則登錄失敗if (result) {// 判斷結果是否正確 //在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();// 使用完關閉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(); //結束當前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(); //結束當前activity }}

微信號登錄activity對應的布局文件

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://www.hdgsjgj.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='緊急凍結' 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); //設置布局/* 隱藏自帶標題*/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)欄字體設置為黑色 decorView.setSystemUiVisibility(option); getWindow().setStatusBarColor(Color.TRANSPARENT);}initViews(); // 初始化布局元素// 設置注冊按鈕是否可點擊if (phone.getText() + '' == '' || password.getText() + '' == '') { button.setEnabled(false);} else { button.setEnabled(true);}inputFocus(); //監(jiān)聽EditView變色buttonChangeColor(); //登錄按鈕變色//設置通過微信號登錄的監(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)絡請求操作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應該設置什么顏色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ù),設置這個連接是否可以被重定向 urlConnection.setReadTimeout(3000);// 響應的超時時間 urlConnection.setDoInput(true);// 設置這個連接是否可以寫入數(shù)據(jù) urlConnection.setDoOutput(true);// 設置這個連接是否可以輸出數(shù)據(jù) urlConnection.setRequestMethod('POST');// 設置請求的方式 urlConnection.setRequestProperty('Content-Type', 'application/json;charset=UTF-8');// 設置消息的類型 urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實際上它只是建立了一個與服務器的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();// 使用完關閉 Log.i('aa', urlConnection.getResponseCode() + ''); //以下判?嗍欠裨L??成功,如果返回的狀態(tài)碼是200則說明訪問成功 if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {// 得到服務端的返回碼是否連接成功// ------------字符流讀取服務端返回的數(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ù),這里服務端返回的是一個boolean類型的數(shù)據(jù)System.out.println('json:===' + result);//如果服務器端返回的是true,則說明登錄成功,否則登錄失敗if (result) {// 判斷結果是否正確 //在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();// 使用完關閉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(); //結束當前activity }}

手機號登錄activity對應的布局文件

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://www.hdgsjgj.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='緊急凍結' 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); //設置布局//一秒后結束當前activitynew Handler().postDelayed(new Runnable() { @Override public void run() {Loading.this.finish(); }}, 1000); }}

創(chuàng)建 activity Loading.java對應的布局文件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)(移動端)

測試

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

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

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

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

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

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

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

標簽: 微信
相關文章:
主站蜘蛛池模板: 工业淬火油烟净化器,北京油烟净化器厂家,热处理油烟净化器-北京众鑫百科 | 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | 网架支座@球铰支座@钢结构支座@成品支座厂家@万向滑动支座_桥兴工程橡胶有限公司 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 精密冲床,高速冲床等冲压设备生产商-常州晋志德压力机厂 | 天助网 - 中小企业全网推广平台_生态整合营销知名服务商_天助网采购优选 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 | 冷凝水循环试验箱-冷凝水试验箱-可编程高低温试验箱厂家-上海巨为(www.juweigroup.com) | 天津市能谱科技有限公司-专业的红外光谱仪_红外测油仪_紫外测油仪_红外制样附件_傅里叶红外光谱技术生产服务厂商 | 压装机-卧式轴承轮轴数控伺服压装机厂家[铭泽机械] | 飞扬动力官网-广告公司管理软件,广告公司管理系统,喷绘写真条幅制作管理软件,广告公司ERP系统 | 企业彩铃制作_移动、联通、电信集团彩铃上传开通_彩铃定制_商务彩铃管理平台-集团彩铃网 | 应急灯_消防应急灯_应急照明灯_应急灯厂家-大成智慧官网 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 万濠影像仪(万濠投影仪)百科-苏州林泽仪器 | 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 厂厂乐-汇聚海量采购信息的B2B微营销平台-厂厂乐官网 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 道达尔润滑油-食品级润滑油-道达尔导热油-合成导热油,深圳道达尔代理商合-深圳浩方正大官网 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 福建自考_福建自学考试网| 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 汝成内控-行政事业单位内部控制管理服务商| 实验室装修_实验室设计_实验室规划设计- 上海广建净化工程公司 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 不锈钢拉手厂家|浴室门拉手厂家|江门市蓬江区金志翔五金制品有限公司 | 奶茶加盟,奶茶加盟店连锁品牌-甜啦啦官网| SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 发光字|标识设计|标牌制作|精神堡垒 - 江苏苏通广告有限公司 | 骨密度检测仪_骨密度分析仪_骨密度仪_动脉硬化检测仪专业生产厂家【品源医疗】 | 影视模板素材_原创专业影视实拍视频素材-8k像素素材网 | 沥青车辙成型机-车托式混凝土取芯机-混凝土塑料试模|鑫高仪器 | 电脑知识|软件|系统|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 | 万师讲师网-优质讲师培训师供应商,讲师认证,找讲师来万师 | 探伤仪,漆膜厚度测试仪,轮胎花纹深度尺厂家-淄博创宇电子 | 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 |