Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> MD5簡單實例,md5實例

MD5簡單實例,md5實例

編輯:關於android開發

MD5簡單實例,md5實例


如圖當點擊按鈕時,會先判斷是否第一次登陸,如果是第一次登陸登陸則會彈出設置密碼的彈窗,若果登陸過則彈出登陸彈窗

其中輸入的密碼會用MD5加密下

package com.org.demo.wangfeng.demo;

import com.org.wangfeng.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HomeActivity extends Activity {
    private Button bbButton;
    private SharedPreferences mPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homeactivity);
        mPref = getSharedPreferences("config", MODE_PRIVATE);
        bbButton = (Button) findViewById(R.id.bb_home);
        bbButton.setOnClickListener(new OnclickItem());
    }

    private class OnclickItem implements View.OnClickListener {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String savedPassword = mPref.getString("password", null);
            if(!TextUtils.isEmpty(savedPassword)){
                //輸入密碼彈窗
                showPasswordInputDialog();
            }else{
                //如果沒有設置過,彈出設置密碼的彈窗
                showPasswordSetDialog();
            }
        }

    }

    /**
     * 設置密碼的彈窗
     */
    private void showPasswordSetDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_set_password, null);
        dialog.setView(view, 0, 0, 0, 0);// 設置邊距為0,保證在2.x的版本上運行沒問題

        final EditText edPassWord = (EditText) view
                .findViewById(R.id.et_password);
        final EditText edPassWordConfirm = (EditText) view
                .findViewById(R.id.et_password_confirm);

        Button btnOK = (Button) view.findViewById(R.id.btn_ok);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);

        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String password = edPassWord.getText().toString();
                String passwordConfirm = edPassWordConfirm.getText().toString();
                if (!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()) {
                    // 當輸入的2個內容相同
                    if (password.equals(passwordConfirm)) {
                        Toast.makeText(HomeActivity.this, "登陸成功",
                                Toast.LENGTH_SHORT).show();
                        // 將密碼保存起來
                        mPref.edit()
                                .putString("password",
                                        MD5Utils.encode(password)).commit();
                        dialog.dismiss();
                    } else {
                        Toast.makeText(HomeActivity.this, "2此輸入密碼不一致",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HomeActivity.this, "輸入內容不能為空",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 輸入密碼彈窗
     */
    private void showPasswordInputDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_input_password, null);
        dialog.setView(view, 0, 0, 0, 0);
        final EditText etPassword = (EditText) view
                .findViewById(R.id.et_password);

        Button btnOk = (Button) view.findViewById(R.id.btn_ok);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
        btnOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String password = etPassword.getText().toString();
                if (!TextUtils.isEmpty(password)) {
                    String savedPassword = mPref.getString("password", null);
                    if (MD5Utils.encode(password).equals(savedPassword)) {
                        Toast.makeText(HomeActivity.this, "登陸成功",
                                Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    } else {
                        Toast.makeText(HomeActivity.this, "密碼錯誤",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HomeActivity.this, "輸入內容不能為空",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
package com.org.demo.wangfeng.demo;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    /**
     * md5加密
     * 
     * @param password
     * @return
     */
    public static String encode(String password) {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");// 獲取MD5算法對象
            byte[] digest = instance.digest(password.getBytes());// 對字符串加密,返回字節數組

            StringBuffer sb = new StringBuffer();
            for (byte b : digest) {
                int i = b & 0xff;// 獲取字節的低八位有效值
                String hexString = Integer.toHexString(i);// 將整數轉為16進制

                if (hexString.length() < 2) {
                    hexString = "0" + hexString;// 如果是1位的話,補0
                }

                sb.append(hexString);
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            // 沒有該算法時,拋出異常, 不會走到這裡
        }

        return "";
    }
}
<?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="#fff" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#66ff6600" android:gravity="center" android:padding="10dp" android:text="設置密碼" android:textColor="@android:color/black" android:textSize="20sp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請輸入密碼" android:inputType="textPassword" > </EditText> <EditText android:id="@+id/et_password_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請再次輸入密碼" android:inputType="textPassword" > </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="確定" /> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout> View Code

 

其實在android裡 if(text ==null || text.length()==0)是有封裝的。
在android.text.TextUtils裡

public static boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
            return true;
        else
            return false;
    }
所以我們可以使用
TextUtils.isEmpty(text)

代替

if(text == null || text.length() == 0)

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved