android 仿微信demo——登錄功能實(shí)現(xiàn)(移動(dòng)端)
移動(dòng)端登錄功能實(shí)現(xiàn)
登錄功能基本和注冊(cè)一樣,唯一不同的是登錄可以實(shí)現(xiàn)兩種登錄方式(微信號(hào)和手機(jī)號(hào)),也就是布局不一樣。所以需要兩個(gè)布局,兩個(gè)activity(這個(gè)方法比較簡(jiǎn)單粗暴,我懶。也可以通過(guò)activity動(dòng)態(tài)切換布局,這樣只需要一個(gè)activity就可以了)
創(chuàng)建兩個(gè)activity,實(shí)現(xiàn)兩種登錄方式
微信號(hào)登錄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; //自定義的一個(gè)Hander消息機(jī)制 private MyHander myhander = new MyHander(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login_user); //設(shè)置布局 /* 隱藏自帶標(biāo)題*/ 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; //因?yàn)楸尘盀闇\色所以將狀態(tài)欄字體設(shè)置為黑色 decorView.setSystemUiVisibility(option); getWindow().setStatusBarColor(Color.TRANSPARENT); } initViews(); // 初始化布局元素 /*獲取注冊(cè)activity傳過(guò)來(lái)的微信號(hào)*/ Intent intent = getIntent(); String number = intent.getStringExtra("weixin_number"); //把傳過(guò)來(lái)的值顯示在登錄布局上 weixinNumber.setText(number); // 設(shè)置注冊(cè)按鈕是否可點(diǎn)擊 if (weixinNumber.getText() + "" == "" || password.getText() + "" == "") { button.setEnabled(false); } else { button.setEnabled(true); } inputFocus(); //監(jiān)聽(tīng)EditView變色 buttonChangeColor(); //登錄按鈕變色 // 設(shè)置手機(jī)號(hào)登錄的監(jiān)聽(tīng)器 phone_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳轉(zhuǎn)到手機(jī)號(hào)登錄的activity Intent intent=new Intent(LoginUser.this,LoginPhone.class); startActivity(intent); } }); //button的點(diǎn)擊事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創(chuàng)建一個(gè)進(jìn)度條的activity,通過(guò)AndroidMainfest.xml文件聲明為對(duì)話框,這樣activity就不會(huì)覆蓋當(dāng)前的activity Intent intent = new Intent(); intent.setClass(LoginUser.this, Loading.class); startActivity(intent); // 開(kāi)一個(gè)線程完成網(wǎng)絡(luò)請(qǐ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) { // 此處為得到焦點(diǎn)時(shí)的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver1); imageView.setBackgroundResource(R.color.input_dvier_focus); } else { // 此處為失去焦點(diǎn)時(shí)的處理內(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) { // 此處為得到焦點(diǎn)時(shí)的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier_focus); } else { // 此處為失去焦點(diǎn)時(shí)的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier); } } }); } public void buttonChangeColor() { //創(chuàng)建工具類(lèi)對(duì)象 把要改變顏色的Button先傳過(guò)去 WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(button); textChangeListener.addAllEditText(weixinNumber, password);//把所有要監(jiān)聽(tīng)的EditText都添加進(jìn)去 //接口回調(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ā)送請(qǐng)求的主要方法 public void httpUrlConnPost(String number, String password) { HttpURLConnection urlConnection = null; URL url; try { // 請(qǐng)求的URL地地址 url = new URL( "http://100.2.178.10:8080/AndroidServer_war_exploded/Login"); urlConnection = (HttpURLConnection) url.openConnection();// 打開(kāi)http連接 urlConnection.setConnectTimeout(3000);// 連接的超時(shí)時(shí)間 urlConnection.setUseCaches(false);// 不使用緩存 // urlConnection.setFollowRedirects(false);是static函數(shù),作用于所有的URLConnection對(duì)象。 urlConnection.setInstanceFollowRedirects(true);// 是成員函數(shù),僅作用于當(dāng)前函數(shù),設(shè)置這個(gè)連接是否可以被重定向 urlConnection.setReadTimeout(3000);// 響應(yīng)的超時(shí)時(shí)間 urlConnection.setDoInput(true);// 設(shè)置這個(gè)連接是否可以寫(xiě)入數(shù)據(jù) urlConnection.setDoOutput(true);// 設(shè)置這個(gè)連接是否可以輸出數(shù)據(jù) urlConnection.setRequestMethod("POST");// 設(shè)置請(qǐng)求的方式 urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");// 設(shè)置消息的類(lèi)型 urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實(shí)際上它只是建立了一個(gè)與服務(wù)器的TCP連接 JSONObject json = new JSONObject();// 創(chuàng)建json對(duì)象 json.put("number", URLEncoder.encode(number, "UTF-8"));// 使用URLEncoder.encode對(duì)特殊和不可見(jiàn)字符進(jìn)行編碼 json.put("password", URLEncoder.encode(password, "UTF-8"));// 把數(shù)據(jù)put進(jìn)json對(duì)象中 String jsonstr = json.toString();// 把JSON對(duì)象按JSON的編碼格式轉(zhuǎn)換為字符串 // ------------字符流寫(xiě)入數(shù)據(jù)------------ OutputStream out = urlConnection.getOutputStream();// 輸出流,用來(lái)發(fā)送請(qǐng)求,http請(qǐng)求實(shí)際上直到這個(gè)函數(shù)里面才正式發(fā)送出去 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));// 創(chuàng)建字符流對(duì)象并用高效緩沖流包裝它,便獲得最高的效率,發(fā)送的是字符串推薦用字符流,其它數(shù)據(jù)就用字節(jié)流 bw.write(jsonstr);// 把json字符串寫(xiě)入緩沖區(qū)中 bw.flush();// 刷新緩沖區(qū),把數(shù)據(jù)發(fā)送出去,這步很重要 out.close(); bw.close();// 使用完關(guān)閉 Log.i("aa", urlConnection.getResponseCode() + ""); //以下判斷是否訪問(wèn)成功,如果返回的狀態(tài)碼是200則說(shuō)明訪問(wèn)成功 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對(duì)象中得到key值為"json"的數(shù)據(jù),這里服務(wù)端返回的是一個(gè)boolean類(lèi)型的數(shù)據(jù) System.out.println("json:===" + result); //如果服務(wù)器端返回的是true,則說(shuō)明登錄成功,否則登錄失敗 if (result) {// 判斷結(jié)果是否正確 //在Android中http請(qǐng)求,必須放到線程中去作請(qǐng)求,但是在線程中不可以直接修改UI,只能通過(guò)hander機(jī)制來(lái)完成對(duì)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機(jī)制來(lái)完成對(duì)UI的操作 class MyHander extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); //判斷hander的內(nèi)容是什么,如果是1則說(shuō)明登錄成功,如果是2說(shuō)明登錄失敗 switch (msg.what) { case 1: Log.i("aa", msg.what + ""); //提示 Toast.makeText(getApplicationContext(), "登錄成功", Toast.LENGTH_SHORT).show(); //通過(guò)Intent跳轉(zhuǎn)到微信首頁(yè),把微信號(hào)傳過(guò)去 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é)束當(dāng)前actitivy break; case 2: Log.i("aa", msg.what + ""); //對(duì)話框 new AlertDialog.Builder(com.example.wxchatdemo.LoginUser.this) .setTitle(" 登錄失敗") .setMessage(" 用戶名或密碼錯(cuò)誤,請(qǐng)重新填寫(xiě)") .setPositiveButton("確定", null) .show(); break; } } } //返回按鈕處理事件 public void login_activity_back(View v) { /*跳轉(zhuǎn)到微信啟動(dòng)頁(yè)*/ Intent intent = new Intent(); intent.setClass(com.example.wxchatdemo.LoginUser.this, Welcome.class); startActivity(intent); com.example.wxchatdemo.LoginUser.this.finish(); //結(jié)束當(dāng)前activity } }
微信號(hào)登錄activity對(duì)應(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"> <!--返回按鈕--> <ImageView android: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" /> <!--標(biāo)題--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="45dp" android:text="微信號(hào)/QQ號(hào)/郵箱登錄" android:textColor="@color/loginText" android:textSize="25sp" /> <!--賬號(hào)輸入--> <LinearLayout android: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="賬號(hào)" android:textColor="@color/loginText" android:textSize="16sp" /> <EditText android:id="@+id/log_weixin_number" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginLeft="55dp" android:background="@null" android:hint="請(qǐng)?zhí)顚?xiě)微信號(hào)/QQ號(hào)/郵箱" android:singleLine="true" android:textColorHint="@color/textColorHint" android:textCursorDrawable="@drawable/edit_cursor_color" android:textSize="16sp" /> </LinearLayout> <!--下劃線--> <ImageView android: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" /> <LinearLayout android: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: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="請(qǐng)?zhí)顚?xiě)密碼" android:singleLine="true" android:textColorHint="@color/textColorHint" android:textCursorDrawable="@drawable/edit_cursor_color" android:textSize="16sp" /> </LinearLayout> <!--下劃線--> <ImageView android: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" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/phone_log" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="30dp" android:text="用手機(jī)號(hào)登錄" android:textColor="@color/massageLogin" android:textSize="17dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:gravity="center_horizontal"> <!--登錄按鈕--> <Button android: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> <LinearLayout android: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>
手機(jī)號(hào)登錄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; //自定義的一個(gè)Hander消息機(jī)制 private LoginPhone.MyHander myhander = new LoginPhone.MyHander(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login_phone); //設(shè)置布局 /* 隱藏自帶標(biāo)題*/ 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; //因?yàn)楸尘盀闇\色所以將狀態(tài)欄字體設(shè)置為黑色 decorView.setSystemUiVisibility(option); getWindow().setStatusBarColor(Color.TRANSPARENT); } initViews(); // 初始化布局元素 // 設(shè)置注冊(cè)按鈕是否可點(diǎn)擊 if (phone.getText() + "" == "" || password.getText() + "" == "") { button.setEnabled(false); } else { button.setEnabled(true); } inputFocus(); //監(jiān)聽(tīng)EditView變色 buttonChangeColor(); //登錄按鈕變色 //設(shè)置通過(guò)微信號(hào)登錄的監(jiān)聽(tīng)器 user_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //跳轉(zhuǎn)到用微信號(hào)登錄的activity Intent intent = new Intent(LoginPhone.this, LoginUser.class); startActivity(intent); } }); //button的點(diǎn)擊事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //創(chuàng)建一個(gè)進(jìn)度條的activity,通過(guò)AndroidMainfest.xml文件聲明為對(duì)胡框,這樣activity就不會(huì)覆蓋當(dāng)前的activity Intent intent = new Intent(); intent.setClass(LoginPhone.this, Loading.class); startActivity(intent); // 開(kāi)一個(gè)線程完成網(wǎng)絡(luò)請(qǐ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) { // 此處為得到焦點(diǎn)時(shí)的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver1); imageView.setBackgroundResource(R.color.input_dvier_focus); } else { // 此處為失去焦點(diǎn)時(shí)的處理內(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) { // 此處為得到焦點(diǎn)時(shí)的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier_focus); } else { // 此處為失去焦點(diǎn)時(shí)的處理內(nèi)容 ImageView imageView = (ImageView) findViewById(R.id.login_diver2); imageView.setBackgroundResource(R.color.input_dvier); } } }); } public void buttonChangeColor() { //創(chuàng)建工具類(lèi)對(duì)象 把要改變顏色的Button先傳過(guò)去 WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(button); textChangeListener.addAllEditText(phone, password);//把所有要監(jiān)聽(tīng)的EditText都添加進(jìn)去 //接口回調(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ā)送請(qǐng)求的主要方法 public void httpUrlConnPost(String phone, String password) { HttpURLConnection urlConnection = null; URL url; try { // 請(qǐng)求的URL地地址 url = new URL( "http://100.2.178.10:8080/AndroidServer_war_exploded/Login"); urlConnection = (HttpURLConnection) url.openConnection();// 打開(kāi)http連接 urlConnection.setConnectTimeout(3000);// 連接的超時(shí)時(shí)間 urlConnection.setUseCaches(false);// 不使用緩存 // urlConnection.setFollowRedirects(false);是static函數(shù),作用于所有的URLConnection對(duì)象。 urlConnection.setInstanceFollowRedirects(true);// 是成員函數(shù),僅作用于當(dāng)前函數(shù),設(shè)置這個(gè)連接是否可以被重定向 urlConnection.setReadTimeout(3000);// 響應(yīng)的超時(shí)時(shí)間 urlConnection.setDoInput(true);// 設(shè)置這個(gè)連接是否可以寫(xiě)入數(shù)據(jù) urlConnection.setDoOutput(true);// 設(shè)置這個(gè)連接是否可以輸出數(shù)據(jù) urlConnection.setRequestMethod("POST");// 設(shè)置請(qǐng)求的方式 urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");// 設(shè)置消息的類(lèi)型 urlConnection.connect();// 連接,從上述至此的配置必須要在connect之前完成,實(shí)際上它只是建立了一個(gè)與服務(wù)器的TCP連接 JSONObject json = new JSONObject();// 創(chuàng)建json對(duì)象 json.put("number", URLEncoder.encode(phone, "UTF-8"));// 使用URLEncoder.encode對(duì)特殊和不可見(jiàn)字符進(jìn)行編碼 json.put("password", URLEncoder.encode(password, "UTF-8"));// 把數(shù)據(jù)put進(jìn)json對(duì)象中 String jsonstr = json.toString();// 把JSON對(duì)象按JSON的編碼格式轉(zhuǎn)換為字符串 // ------------字符流寫(xiě)入數(shù)據(jù)------------ OutputStream out = urlConnection.getOutputStream();// 輸出流,用來(lái)發(fā)送請(qǐng)求,http請(qǐng)求實(shí)際上直到這個(gè)函數(shù)里面才正式發(fā)送出去 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));// 創(chuàng)建字符流對(duì)象并用高效緩沖流包裝它,便獲得最高的效率,發(fā)送的是字符串推薦用字符流,其它數(shù)據(jù)就用字節(jié)流 bw.write(jsonstr);// 把json字符串寫(xiě)入緩沖區(qū)中 bw.flush();// 刷新緩沖區(qū),把數(shù)據(jù)發(fā)送出去,這步很重要 out.close(); bw.close();// 使用完關(guān)閉 Log.i("aa", urlConnection.getResponseCode() + ""); //以下判斷是否訪問(wèn)成功,如果返回的狀態(tài)碼是200則說(shuō)明訪問(wèn)成功 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對(duì)象中得到key值為"json"的數(shù)據(jù),這里服務(wù)端返回的是一個(gè)boolean類(lèi)型的數(shù)據(jù) System.out.println("json:===" + result); //如果服務(wù)器端返回的是true,則說(shuō)明登錄成功,否則登錄失敗 if (result) {// 判斷結(jié)果是否正確 //在Android中http請(qǐng)求,必須放到線程中去作請(qǐng)求,但是在線程中不可以直接修改UI,只能通過(guò)hander機(jī)制來(lái)完成對(duì)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機(jī)制來(lái)完成對(duì)UI的操作 class MyHander extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); //判斷hander的內(nèi)容是什么,如果是1則說(shuō)明登錄成功,如果是2說(shuō)明登錄失敗 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(" 用戶名或密碼錯(cuò)誤,請(qǐng)重新填寫(xiě)") .setPositiveButton("確定", null) .show(); } } } //返回按鈕處理事件 public void login_activity_back(View v) { /*跳轉(zhuǎn)到微信啟動(dòng)頁(yè)*/ Intent intent = new Intent(); intent.setClass(com.example.wxchatdemo.LoginPhone.this, Welcome.class); startActivity(intent); com.example.wxchatdemo.LoginPhone.this.finish(); //結(jié)束當(dāng)前activity } }
手機(jī)號(hào)登錄activity對(duì)應(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"> <!--返回按鈕--> <ImageView android: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" /> <!--標(biāo)題--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="45dp" android:text="手機(jī)號(hào)登錄" android:textColor="@color/loginText" android:textSize="25sp" /> <!--賬號(hào)輸入--> <LinearLayout android: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="手機(jī)號(hào)" android:textColor="@color/loginText" android:textSize="16sp" /> <EditText android:id="@+id/log_phone" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginLeft="35dp" android:background="@null" android:hint="請(qǐng)?zhí)顚?xiě)手機(jī)號(hào)" android:singleLine="true" android:textColorHint="@color/textColorHint" android:textCursorDrawable="@drawable/edit_cursor_color" android:textSize="16sp" /> </LinearLayout> <!--下劃線--> <ImageView android: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" /> <LinearLayout android: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: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="請(qǐng)?zhí)顚?xiě)密碼" android:singleLine="true" android:textColorHint="@color/textColorHint" android:textCursorDrawable="@drawable/edit_cursor_color" android:textSize="16sp" /> </LinearLayout> <!--下劃線--> <ImageView android: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" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/user_log" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="30dp" android:text="用微信號(hào)/QQ號(hào)/郵箱登錄" android:textColor="@color/massageLogin" android:textSize="17dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:gravity="center_horizontal"> <!--登錄按鈕--> <Button android: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> <LinearLayout android: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)建一個(gè)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>
上面兩個(gè)登錄activity都實(shí)現(xiàn)了一個(gè)自定義的等待框activity,當(dāng)點(diǎn)擊登錄按鈕時(shí),便會(huì)跳轉(zhuǎn)到這個(gè)activity,但是自定義的activity會(huì)覆蓋原有的界面。而微信點(diǎn)擊登錄按鈕后會(huì)彈出一個(gè)等待框且不會(huì)覆蓋原有的activity(即原有界面),所以要給自定義的等待框activity在Androidfest.xml文件配置為對(duì)話框,這樣就不會(huì)覆蓋原有activity.
創(chuàng)建activity Loading.java ,實(shí)現(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 stub super.onCreate(savedInstanceState); setContentView(R.layout.loading); //設(shè)置布局 //一秒后結(jié)束當(dāng)前activity new Handler().postDelayed(new Runnable() { @Override public void run() { Loading.this.finish(); } }, 1000); } }
創(chuàng)建 activity Loading.java對(duì)應(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"> <RelativeLayout android: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"> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> <TextView android: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 為對(duì)話框
<activity android:name=".Loading" android:theme="@style/MyDialogStyle" />
上面用到的主題theme是自定義的主題,把a(bǔ)ctivity轉(zhuǎn)化為對(duì)話框,這樣就不會(huì)覆蓋原有的activity,下面會(huì)給出如何定義自定義主題
創(chuàng)建樣式styles.xml文件,實(shí)現(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
測(cè)試
雖然服務(wù)端登錄表單處理功能還沒(méi)寫(xiě),但是還是可以測(cè)試上面的效果
把以往文章中點(diǎn)擊登陸按鈕注釋代碼取消注釋
把兩個(gè)activity登錄成功后跳轉(zhuǎn)activity那段代碼段注釋掉,啟動(dòng)項(xiàng)目測(cè)試
到此這篇關(guān)于android 仿微信demo——登錄功能實(shí)現(xiàn)(移動(dòng)端)的文章就介紹到這了,更多相關(guān)android仿微信登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android 中使用TableLayout實(shí)現(xiàn)表單布局效果示例
本篇文章主要介紹了android 中使用TableLayout實(shí)現(xiàn)表單布局效果示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06android studio 3.6.0 綁定視圖新特性的方法
這篇文章主要介紹了android studio 3.6.0 綁定視圖新特性的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼
這篇文章主要介紹了Android直播軟件搭建之實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的詳細(xì)代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Android開(kāi)發(fā)筆記之:Splash的實(shí)現(xiàn)詳解
本篇文章是對(duì)Android中Splash的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android后臺(tái)線程和UI線程通訊實(shí)例
這篇文章主要介紹了Android后臺(tái)線程和UI線程通訊實(shí)例,每一步的要點(diǎn)和步驟都有提及,并配有代碼例子,需要的朋友可以參考下2014-06-06android 幀動(dòng)畫(huà),補(bǔ)間動(dòng)畫(huà),屬性動(dòng)畫(huà)的簡(jiǎn)單總結(jié)
本文主要對(duì)android 幀動(dòng)畫(huà),補(bǔ)間動(dòng)畫(huà),屬性動(dòng)畫(huà)進(jìn)行了簡(jiǎn)單總結(jié),具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01Android SharedPreference存儲(chǔ)文件三步走
SharedPreferences是安卓平臺(tái)上一個(gè)輕量級(jí)的存儲(chǔ)類(lèi),用來(lái)保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時(shí),將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時(shí),再?gòu)腟haredPreferences中將值取出2023-01-01android使用SkinManager實(shí)現(xiàn)換膚功能的示例
本篇文章主要介紹了android使用SkinManager實(shí)現(xiàn)換膚功能的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼
這篇文章主要介紹了Flutter banner_view 輪播圖的使用及實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07