The original web site: www.oschina.net/code/snippe…

package com.amaker.wlo;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.widget.Button;

import android.widget.EditText;

import com.amaker.util.HttpUtil;

import com.amaker.util.LoginEncryption;

public class LoginActivity extends Activity {

Private Button loginBtn; private Button loginBtn; Private EditText userEditText,pwdEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EndAllActivity.getInstance().addActivity(this); RequestWindowFeature (window.feature_no_title); // Set the current Activity layout setContentView(r.layout.activity_login); LoginBtn = (Button)findViewById(R.id.login); UserEditText = (EditText)findViewById(r.id.user EditText); // Instantiate the component with the findViewById method pwdEditText = (EditText)findViewById(R.id.wdeditText); LoginBtn. SetOnClickListener (new an OnClickListener () {@ Override public void onClick (View v) {/ / login if (the validate ()) { if(login()){ Intent intent = new Intent(LoginActivity.this,MainMenuActivity.class); startActivity(intent); finish(); } else{showDialog(" User name or password error, please re-enter!" ); }}}}); } private Boolean login(){String username = userEditText.gettext ().toString(); / / pin / / MD5 encrypted compared with database String. PWD = LoginEncryption string2MD5 (pwdEditText. The getText (), toString ()); String result=query(username, PWD); if(result! =null&&result.equals("0")){ return false; }else{ saveUserMsg(result); return true; Private void saveUserMsg(String MSG){// User id String ID = ""; // User name String name = ""; [] MSGS = msg.split(";") ); int idx = msgs[0].indexOf("="); id = msgs[0].substring(idx+1); idx = msgs[1].indexOf("="); name = msgs[1].substring(idx+1); SharedPreferences pre = getSharedPreferences("user_msg", MODE_WORLD_WRITEABLE); SharedPreferences.Editor editor = pre.edit(); editor.putString("id", id); editor.putString("name", name); editor.commit(); } private Boolean validate(){String username = userEditText.gettext ().toString(); If (username. Equals ("")){showDialog(" Username is required!" ); return false; } String pwd = pwdEditText.getText().toString(); If (pwd.equals("")){showDialog(" User password is required! ); return false; } return true; } private void showDialog(String msg){ AlertDialog.Builder builder = new AlertDialog.Builder(this); Builder.setmessage (MSG).setCancelable(false).setpositiveButton (" confirm ", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }); AlertDialog alert = builder.create(); alert.show(); Private String query(String username,String password){// Query parameter String queryString = "username="+username+"&password="+password; // url String url = HttpUtil.BASE_URL+"servlet/LoginServlet?" +queryString; / / query returns the results return HttpUtil. QueryStringForPost (url); }Copy the code

}

If this article was helpful to you, please click “like” on the left. Thanks for your support.