欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android模仿用户设置密码实例

程序员文章站 2023-11-27 08:55:58
首先有2个对话框,没有设置过密码,需要设置dialog_set_password.xml,用户设置过密码,不需要设置,直接输入密码dialog_input_password...

首先有2个对话框,没有设置过密码,需要设置dialog_set_password.xml,用户设置过密码,不需要设置,直接输入密码dialog_input_password.xml,

设置对话框dialog_set_password.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:orientation="vertical" > 
  <textview  
    android:id="@+id/textview1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:textcolor="@color/black" 
    android:textsize="20sp" 
    android:background="#66ff00" 
    android:text="设置密码" 
    android:padding="10dp" 
    /> 
  <edittext  
    android:id="@+id/et_password" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputtype="textpassword" 
    android:hint="请输入密码" 
    /> 
  <edittext  
    android:id="@+id/et_password_confirm" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputtype="textpassword" 
    android:hint="请再次输入密码" 
    /> 
  <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_cancle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="取消" /> 
  </linearlayout> 
</linearlayout> 

输入对话框dialog_input_password.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:orientation="vertical" > 
  <textview  
    android:id="@+id/textview1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:textcolor="@color/black" 
    android:textsize="20sp" 
    android:background="#66ff00" 
    android:text="输入密码" 
    android:padding="10dp" 
    /> 
  <edittext  
    android:id="@+id/et_password" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputtype="textpassword" 
    android:hint="请输入密码" 
    /> 
  <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_cancle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="取消" /> 
  </linearlayout> 
</linearlayout> 

业务逻辑并实现md5加密

package com.ldw.safe.activity; 
import android.app.activity; 
import android.app.alertdialog; 
import android.content.intent; 
import android.content.sharedpreferences; 
import android.os.bundle; 
import android.text.textutils; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.view.viewgroup; 
import android.widget.adapterview; 
import android.widget.adapterview.onitemclicklistener; 
import android.widget.baseadapter; 
import android.widget.button; 
import android.widget.edittext; 
import android.widget.gridview; 
import android.widget.imageview; 
import android.widget.textview; 
import android.widget.toast; 
import com.ldw.safe.r; 
import com.ldw.safe.utils.md5utils; 
public class homeactivity extends activity{ 
  private gridview gv_home; 
  private string[] mitems = new string[] {"手机防盗", "通讯卫士", "软件管理", "进程管理", "流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心"}; 
  private int[] mpics = new int[] {r.drawable.safe, r.drawable.callmsgsafe, r.drawable.app,  
      r.drawable.taskmanager, r.drawable.netmanager, r.drawable.sysoptimize,  
      r.drawable.*, r.drawable.atools, r.drawable.settings}; 
  private sharedpreferences mpref; 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_home); 
    mpref = getsharedpreferences("config", mode_private); 
    gv_home = (gridview) findviewbyid(r.id.gv_home); 
    gv_home.setadapter(new homeadapter()); 
    //监听gridview点击事件 
    gv_home.setonitemclicklistener(new onitemclicklistener(){ 
      @override 
      public void onitemclick(adapterview<?> parent, view view, 
          int position, long id) { 
        switch(position){ 
        case 0: 
          //手机防盗 
          showpassworddialog(); 
          break; 
        case 8: 
          //设置中心 
          startactivity(new intent(homeactivity.this, settingactivity.class)); 
          break; 
        default: 
          break; 
        } 
      } 
    }); 
  } 
  /* 
   * 显示密码的弹窗 
   */ 
  protected void showpassworddialog(){ 
    //判断是否设置密码 
    string savepassword = mpref.getstring("password", null); 
    if(!textutils.isempty(savepassword)){ 
      //输入密码的弹窗 
      showpasswordinputdialog(); 
    }else{ 
      //如果没有设置过,弹出密码设置的弹窗 
      showpasswordsetdialog(); 
    } 
  } 
  /* 
   * 输入密码框,设置过密码只需要登陆 
   */ 
  protected 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);//自定义的view设置到dialog 
    dialog.setview(view, 0, 0, 0, 0);//设置边距为0,兼容android2.3 
    final edittext etpassword = (edittext) view.findviewbyid(r.id.et_password); 
    //获取2个按键 
    button btnok = (button)view.findviewbyid(r.id.btn_ok); 
    button btncancle = (button)view.findviewbyid(r.id.btn_cancle); 
    //监听ok按键 
    btnok.setonclicklistener(new onclicklistener(){ 
      @override 
      public void onclick(view v) { 
        string password = etpassword.gettext().tostring(); 
        if(!textutils.isempty(password)){ 
          string savepassword = mpref.getstring("password", null); 
          if(md5utils.encode(password).equals(savepassword)){ 
            toast.maketext(homeactivity.this, "登陆成功", toast.length_short).show(); 
            //对话框消失 
            dialog.dismiss(); 
            //跳转到手机防盗界面 
            startactivity(new intent(homeactivity.this, lostandfind.class)); 
          }else{ 
            toast.maketext(homeactivity.this, "登陆失败", toast.length_short).show(); 
          } 
        }else{ 
          toast.maketext(homeactivity.this, "输入框不能是空的", toast.length_short).show(); 
        } 
      } 
    }); 
    //监听取消按键 
    btncancle.setonclicklistener(new onclicklistener(){ 
      @override 
      public void onclick(view v) { 
        dialog.dismiss(); 
      } 
    }); 
    dialog.show(); 
  } 
  /* 
   * 设置密码的弹窗,以前没有设置密码 
   */ 
  protected 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);//自定义的view设置到dialog 
    dialog.setview(view, 0, 0, 0, 0);//设置边距为0,兼容android2.3 
    final edittext etpassword = (edittext) view.findviewbyid(r.id.et_password); 
    final edittext etpasswordconfirm = (edittext) view.findviewbyid(r.id.et_password_confirm); 
    //获取2个按键 
    button btnok = (button)view.findviewbyid(r.id.btn_ok); 
    button btncancle = (button)view.findviewbyid(r.id.btn_cancle); 
    //监听ok按键 
    btnok.setonclicklistener(new onclicklistener(){ 
      @override 
      public void onclick(view v) { 
        string password = etpassword.gettext().tostring(); 
        string passwordconfirm = etpasswordconfirm.gettext().tostring(); 
        //textutils.isempty(password)判断password是不是空,可以避免都是空格 
        if(!textutils.isempty(password) && !passwordconfirm.isempty()){ 
          if(password.equals(passwordconfirm)){ 
            toast.maketext(homeactivity.this, "登陆成功", toast.length_short).show(); 
            //保存密码到mpref 
            mpref.edit().putstring("password", md5utils.encode(password)).commit(); 
            //对话框消失 
            dialog.dismiss(); 
            //跳转到手机防盗界面 
            startactivity(new intent(homeactivity.this, lostandfind.class)); 
          }else{ 
            toast.maketext(homeactivity.this, "两次密码不一致", toast.length_short).show(); 
          } 
        }else{ 
          toast.maketext(homeactivity.this, "输入框不能是空的", toast.length_short).show(); 
        } 
      } 
    }); 
    //监听取消按键 
    btncancle.setonclicklistener(new onclicklistener(){ 
      @override 
      public void onclick(view v) { 
        dialog.dismiss(); 
      } 
    }); 
    dialog.show(); 
  } 
  class homeadapter extends baseadapter{ 
    @override 
    public int getcount() { 
      // todo auto-generated method stub 
      return mitems.length; 
    } 
    @override 
    public object getitem(int position) { 
      // todo auto-generated method stub 
      return mitems[position]; 
    } 
    @override 
    public long getitemid(int position) { 
      // todo auto-generated method stub 
      return position; 
    } 
    @override 
    public view getview(int position, view convertview, viewgroup parent) { 
      view view = view.inflate(homeactivity.this, r.layout.home_list_item, null); 
      imageview iv_item = (imageview) view.findviewbyid(r.id.iv_item); 
      textview tv_item = (textview) view.findviewbyid(r.id.tv_item); 
      iv_item.setimageresource(mpics[position]); 
      tv_item.settext(mitems[position]); 
      return view; 
    } 
  } 
} 

以上所述是小编给大家介绍的android模仿用户设置密码实例,希望对大家有所帮助