日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 仿微信demo————登录功能实现(移动端)

發布時間:2023/12/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 仿微信demo————登录功能实现(移动端) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android 仿微信demo————微信啟動界面實現

android 仿微信demo————注冊功能實現(移動端)

android 仿微信demo————注冊功能實現(服務端)

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

android 仿微信demo————登錄功能實現(服務端)

android 仿微信demo————微信主界面實現

android 仿微信demo————微信消息界面實現(移動端)

android 仿微信demo————微信消息界面實現(服務端)

android 仿微信demo————微信通訊錄界面功能實現(移動端,服務端)

android 仿微信demo————微信發現界面實現

android 仿微信demo————微信頂部操作欄界面實現

android 仿微信demo————微信頂部操作欄搜索按鈕實現(查詢通訊錄好友功能)

android 仿微信demo————微信頂部操作欄加號按鈕實現(彈出子菜單)

源碼獲取點我,記得給個start哦

移動端登錄功能實現

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

創建兩個activity,實現兩種登錄方式
微信號登錄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();@Overridepublic 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; //因為背景為淺色所以將狀態欄字體設置為黑色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(); //監聽EditView變色buttonChangeColor(); //登錄按鈕變色// 設置手機號登錄的監聽器phone_login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//跳轉到手機號登錄的activityIntent intent=new Intent(LoginUser.this,LoginPhone.class);startActivity(intent);}});//button的點擊事件button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//創建一個進度條的activity,通過AndroidMainfest.xml文件聲明為對話框,這樣activity就不會覆蓋當前的activityIntent intent = new Intent();intent.setClass(LoginUser.this, Loading.class);startActivity(intent);// 開一個線程完成網絡請求操作new Thread(new Runnable() {@Overridepublic 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() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (hasFocus) {// 此處為得到焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver1);imageView.setBackgroundResource(R.color.input_dvier_focus);} else {// 此處為失去焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver1);imageView.setBackgroundResource(R.color.input_dvier);}}});password.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (hasFocus) {// 此處為得到焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver2);imageView.setBackgroundResource(R.color.input_dvier_focus);} else {// 此處為失去焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver2);imageView.setBackgroundResource(R.color.input_dvier);}}});}public void buttonChangeColor() {//創建工具類對象 把要改變顏色的Button先傳過去WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(button);textChangeListener.addAllEditText(weixinNumber, password);//把所有要監聽的EditText都添加進去//接口回調 在這里拿到boolean變量 根據isHasContent的值決定 Button應該設置什么顏色WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() {@Overridepublic 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));}}});}// 發送請求的主要方法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函數,作用于所有的URLConnection對象。urlConnection.setInstanceFollowRedirects(true);// 是成員函數,僅作用于當前函數,設置這個連接是否可以被重定向urlConnection.setReadTimeout(3000);// 響應的超時時間urlConnection.setDoInput(true);// 設置這個連接是否可以寫入數據urlConnection.setDoOutput(true);// 設置這個連接是否可以輸出數據urlConnection.setRequestMethod("POST");// 設置請求的方式urlConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");// 設置消息的類型urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實際上它只是建立了一個與服務器的TCP連接JSONObject json = new JSONObject();// 創建json對象json.put("number", URLEncoder.encode(number, "UTF-8"));// 使用URLEncoder.encode對特殊和不可見字符進行編碼json.put("password", URLEncoder.encode(password, "UTF-8"));// 把數據put進json對象中String jsonstr = json.toString();// 把JSON對象按JSON的編碼格式轉換為字符串// ------------字符流寫入數據------------OutputStream out = urlConnection.getOutputStream();// 輸出流,用來發送請求,http請求實際上直到這個函數里面才正式發送出去BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));// 創建字符流對象并用高效緩沖流包裝它,便獲得最高的效率,發送的是字符串推薦用字符流,其它數據就用字節流bw.write(jsonstr);// 把json字符串寫入緩沖區中bw.flush();// 刷新緩沖區,把數據發送出去,這步很重要out.close();bw.close();// 使用完關閉Log.i("aa", urlConnection.getResponseCode() + "");//以下判斷是否訪問成功,如果返回的狀態碼是200則說明訪問成功if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {// 得到服務端的返回碼是否連接成功// ------------字符流讀取服務端返回的數據------------InputStream in = urlConnection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(in));String str = null;StringBuffer buffer = new StringBuffer();while ((str = br.readLine()) != null) {// BufferedReader特有功能,一次讀取一行數據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"的數據,這里服務端返回的是一個boolean類型的數據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的內容是什么,如果是1則說明登錄成功,如果是2說明登錄失敗switch (msg.what) {case 1:Log.i("aa", msg.what + "");//提示Toast.makeText(getApplicationContext(), "登錄成功",Toast.LENGTH_SHORT).show();//通過Intent跳轉到微信首頁,把微信號傳過去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(); //結束當前actitivybreak;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) {/*跳轉到微信啟動頁*/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:id="@+id/close"android:layout_width="17dp"android:layout_height="17dp"android:layout_marginLeft="20dp"android:layout_marginTop="45dp"android:onClick="login_activity_back"android:src="@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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:text="賬號"android:textColor="@color/loginText"android:textSize="16sp" /><EditTextandroid:id="@+id/log_weixin_number"android:layout_width="200dp"android:layout_height="wrap_content"android:layout_marginLeft="55dp"android:background="@null"android:hint="請填寫微信號/QQ號/郵箱"android:singleLine="true"android:textColorHint="@color/textColorHint"android:textCursorDrawable="@drawable/edit_cursor_color"android:textSize="16sp" /></LinearLayout><!--下劃線--><ImageViewandroid:id="@+id/login_diver1"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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:text="密碼"android:textColor="@color/loginText"android:textSize="16sp" /><EditTextandroid:id="@+id/log_passwd"android:layout_width="200dp"android:layout_height="wrap_content"android:layout_marginLeft="55dp"android:password="true"android:background="@null"android:hint="請填寫密碼"android:singleLine="true"android:textColorHint="@color/textColorHint"android:textCursorDrawable="@drawable/edit_cursor_color"android:textSize="16sp" /></LinearLayout><!--下劃線--><ImageViewandroid:id="@+id/login_diver2"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"><TextViewandroid:id="@+id/phone_log"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"><!--登錄按鈕--><Buttonandroid:id="@+id/log_button"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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingHorizontal="10dp"android:text="找回密碼"android:textColor="@color/massageLogin"android:textSize="14dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingHorizontal="10dp"android:text="緊急凍結"android:textColor="@color/massageLogin"android:textSize="14dp" /><TextViewandroid: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();@Overridepublic 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; //因為背景為淺色所以將狀態欄字體設置為黑色decorView.setSystemUiVisibility(option);getWindow().setStatusBarColor(Color.TRANSPARENT);}initViews(); // 初始化布局元素// 設置注冊按鈕是否可點擊if (phone.getText() + "" == "" || password.getText() + "" == "") {button.setEnabled(false);} else {button.setEnabled(true);}inputFocus(); //監聽EditView變色buttonChangeColor(); //登錄按鈕變色//設置通過微信號登錄的監聽器user_login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//跳轉到用微信號登錄的activityIntent intent = new Intent(LoginPhone.this, LoginUser.class);startActivity(intent);}});//button的點擊事件button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//創建一個進度條的activity,通過AndroidMainfest.xml文件聲明為對胡框,這樣activity就不會覆蓋當前的activityIntent intent = new Intent();intent.setClass(LoginPhone.this, Loading.class);startActivity(intent);// 開一個線程完成網絡請求操作new Thread(new Runnable() {@Overridepublic 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() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (hasFocus) {// 此處為得到焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver1);imageView.setBackgroundResource(R.color.input_dvier_focus);} else {// 此處為失去焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver1);imageView.setBackgroundResource(R.color.input_dvier);}}});password.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View v, boolean hasFocus) {if (hasFocus) {// 此處為得到焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver2);imageView.setBackgroundResource(R.color.input_dvier_focus);} else {// 此處為失去焦點時的處理內容ImageView imageView = (ImageView) findViewById(R.id.login_diver2);imageView.setBackgroundResource(R.color.input_dvier);}}});}public void buttonChangeColor() {//創建工具類對象 把要改變顏色的Button先傳過去WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(button);textChangeListener.addAllEditText(phone, password);//把所有要監聽的EditText都添加進去//接口回調 在這里拿到boolean變量 根據isHasContent的值決定 Button應該設置什么顏色WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() {@Overridepublic 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));}}});}// 發送請求的主要方法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函數,作用于所有的URLConnection對象。urlConnection.setInstanceFollowRedirects(true);// 是成員函數,僅作用于當前函數,設置這個連接是否可以被重定向urlConnection.setReadTimeout(3000);// 響應的超時時間urlConnection.setDoInput(true);// 設置這個連接是否可以寫入數據urlConnection.setDoOutput(true);// 設置這個連接是否可以輸出數據urlConnection.setRequestMethod("POST");// 設置請求的方式urlConnection.setRequestProperty("Content-Type","application/json;charset=UTF-8");// 設置消息的類型urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實際上它只是建立了一個與服務器的TCP連接JSONObject json = new JSONObject();// 創建json對象json.put("number", URLEncoder.encode(phone, "UTF-8"));// 使用URLEncoder.encode對特殊和不可見字符進行編碼json.put("password", URLEncoder.encode(password, "UTF-8"));// 把數據put進json對象中String jsonstr = json.toString();// 把JSON對象按JSON的編碼格式轉換為字符串// ------------字符流寫入數據------------OutputStream out = urlConnection.getOutputStream();// 輸出流,用來發送請求,http請求實際上直到這個函數里面才正式發送出去BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));// 創建字符流對象并用高效緩沖流包裝它,便獲得最高的效率,發送的是字符串推薦用字符流,其它數據就用字節流bw.write(jsonstr);// 把json字符串寫入緩沖區中bw.flush();// 刷新緩沖區,把數據發送出去,這步很重要out.close();bw.close();// 使用完關閉Log.i("aa", urlConnection.getResponseCode() + "");//以下判斷是否訪問成功,如果返回的狀態碼是200則說明訪問成功if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {// 得到服務端的返回碼是否連接成功// ------------字符流讀取服務端返回的數據------------InputStream in = urlConnection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(in));String str = null;StringBuffer buffer = new StringBuffer();while ((str = br.readLine()) != null) {// BufferedReader特有功能,一次讀取一行數據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"的數據,這里服務端返回的是一個boolean類型的數據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的內容是什么,如果是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) {/*跳轉到微信啟動頁*/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:id="@+id/close"android:layout_width="17dp"android:layout_height="17dp"android:layout_marginLeft="20dp"android:layout_marginTop="45dp"android:onClick="login_activity_back"android:src="@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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:text="手機號"android:textColor="@color/loginText"android:textSize="16sp" /><EditTextandroid:id="@+id/log_phone"android:layout_width="200dp"android:layout_height="wrap_content"android:layout_marginLeft="35dp"android:background="@null"android:hint="請填寫手機號"android:singleLine="true"android:textColorHint="@color/textColorHint"android:textCursorDrawable="@drawable/edit_cursor_color"android:textSize="16sp" /></LinearLayout><!--下劃線--><ImageViewandroid:id="@+id/login_diver1"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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="30dp"android:text="密碼"android:textColor="@color/loginText"android:textSize="16sp" /><EditTextandroid:id="@+id/log_passwd"android:layout_width="200dp"android:layout_height="wrap_content"android:password="true"android:layout_marginLeft="55dp"android:background="@null"android:hint="請填寫密碼"android:singleLine="true"android:textColorHint="@color/textColorHint"android:textCursorDrawable="@drawable/edit_cursor_color"android:textSize="16sp" /></LinearLayout><!--下劃線--><ImageViewandroid:id="@+id/login_diver2"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"><TextViewandroid:id="@+id/user_log"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"><!--登錄按鈕--><Buttonandroid:id="@+id/log_button"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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingHorizontal="10dp"android:text="找回密碼"android:textColor="@color/massageLogin"android:textSize="14dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingHorizontal="10dp"android:text="緊急凍結"android:textColor="@color/massageLogin"android:textSize="14dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingHorizontal="10dp"android:text="微信安全中心"android:textColor="@color/massageLogin"android:textSize="14dp" /></LinearLayout> </LinearLayout>

創建一個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都實現了一個自定義的等待框activity,當點擊登錄按鈕時,便會跳轉到這個activity,但是自定義的activity會覆蓋原有的界面。而微信點擊登錄按鈕后會彈出一個等待框且不會覆蓋原有的activity(即原有界面),所以要給自定義的等待框activity在Androidfest.xml文件配置為對話框,這樣就不會覆蓋原有activity.

創建activity Loading.java ,實現自定義等待框
Loading.java

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

創建 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"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center"android:orientation="vertical"><ProgressBarandroid:id="@+id/progressBar1"style="?android:attr/progressBarStyleLarge"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 為對話框

<activityandroid:name=".Loading"android:theme="@style/MyDialogStyle" />

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

創建樣式styles.xml文件,實現自定義主題

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文件中聲明創建的activity

測試

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

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

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

總結

以上是生活随笔為你收集整理的android 仿微信demo————登录功能实现(移动端)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。