星空网 > 软件开发 > 操作系统

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 "";  }}

MD5简单实例images/loading.gif' data-original="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" />MD5简单实例
<?  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)

 




原标题:MD5简单实例

关键词:md5

md5
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

又有产品禁止入亚马逊FBA仓库!卖家尽快自查!:https://www.goluckyvip.com/news/6304.html
美国海运如何更好地节省跨境物流成本?:https://www.goluckyvip.com/news/6305.html
美国海运消息:多家船公司联合推出亚洲-东非新航线服务:https://www.goluckyvip.com/news/6306.html
内附链接 | TikTok小店新增三国站点,越南、泰国、马来西亚! :https://www.goluckyvip.com/news/6307.html
Ushop BI软件,专注精细化运营的Lazada卖家助手 :https://www.goluckyvip.com/news/6308.html
跨境物流乌云笼罩,谁来拯救跨境卖家的现金流?:https://www.goluckyvip.com/news/6309.html
深圳西乡三月三北帝庙会的千岁盆菜多少钱:https://www.vstour.cn/a/365180.html
TikTok SEO:TikTok会取代谷歌吗?:https://www.kjdsnews.com/a/1836538.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流