1. 程式人生 > 其它 >android原生開發專案登入頁面程式碼

android原生開發專案登入頁面程式碼

 

LoginActivity

package com.example.wms;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class LoginActivity extends AppCompatActivity {

    private String str_username;
    private String str_password;
    private EditText mEditText_username;
    private EditText mEditText_password;
    private Button mButton_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //相容性
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            //繼承後自動建立對應的佈局介面
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

            //退出
            Button btn_exit = findViewById(R.id.btn_exit);
            btn_exit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            //設定為全域性變數繫結控制元件
            mEditText_username = findViewById(R.id.edit_username);
            mEditText_password = findViewById(R.id.edit_password);
            mButton_login = findViewById(R.id.btn_login);

            //登入按鈕點選事件
            mButton_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //獲取輸入框中的值 返回該物件的字串表示
                    str_username=mEditText_username.getText().toString();
                    str_password=mEditText_password.getText().toString();

                    //呼叫login方法並傳入賬戶和密碼
                    /*login(str_username,str_password);*/
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                }
                //自定義登入方法 裡面需要傳入幾個需要的引數
                public String login(String login_name, String password) {

                    //WebServices地址
                    String WebServicesURL="http://192.168.2.113/CatlServer.asmx";
                    //名稱空間
                    String NameSpace="http://tempuri.org/";
                    //介面方法名
                    String MethodName="Login";
                    //SOAP協議定義訊息請求的地址
                    String soapAction="http://tempuri.org/Login";

                    //例項化SoapObject 物件
                    SoapObject request = new SoapObject(NameSpace, MethodName);
                    //呼叫WebService介面傳入兩個引數
                    request.addProperty("loginName",login_name);
                    request.addProperty("password",password);

                    //生成呼叫WebService方法的SOAP請求資訊,並指定SOAP的版本
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
                    // 設定是否呼叫的是dotNet開發的WebService,後續註釋掉或許能解決ksoap2引數傳輸不或者失敗
                    envelope.bodyOut=request;
                    //網路請求
                    HttpTransportSE httpTransportSE = new HttpTransportSE(WebServicesURL);
                    try {
                        httpTransportSE.call(soapAction,envelope);//這裡需要傳入一下兩個引數
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    //獲取返回的資料
                    SoapObject object= (SoapObject) envelope.bodyIn;
                    //獲取返回的結果,後續根據返回值做判斷
                    String result=object.getProperty(0).toString();

                    //邏輯判斷 狀態碼為1則成功跳轉到主介面
                    if(mEditText_username.length()<1){
                        Toast.makeText(LoginActivity.this,"請輸入使用者名稱",Toast.LENGTH_LONG).show();
                    }else if (mEditText_password.length()<1){
                        Toast.makeText(LoginActivity.this,"請輸入密碼",Toast.LENGTH_LONG).show();
                    }else if (result.equals("0")){
                        Log.e("tag","密碼錯誤");
                        Toast.makeText(LoginActivity.this,"密碼錯誤",Toast.LENGTH_LONG).show();
                    }else if (result.equals("1")){
                        Log.e("tag", "成功");
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        Toast.makeText(LoginActivity.this,"登入成功,歡迎您\t"+str_username,Toast.LENGTH_LONG).show();
                        startActivity(intent);
                    }else if (result.equals("2")){
                        Log.e("tag", "使用者名稱錯誤");
                        Toast.makeText(LoginActivity.this,"使用者名稱錯誤",Toast.LENGTH_LONG).show();
                    }else if (result.equals("-1")){
                        Log.e("tag","未找到使用者資訊");
                        Toast.makeText(LoginActivity.this,"未找到使用者資訊",Toast.LENGTH_LONG).show();
                    }
                    return result;
                }
            });



        }

   }
}