首先有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.trojan,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; } } }); } /* * 显示密码的d窗 */ protected voID showPasswordDialog(){ //判断是否设置密码 String savePassword = mPref.getString("password",null); if(!TextUtils.isEmpty(savePassword)){ //输入密码的d窗 showPasswordinputDialog(); }else{ //如果没有设置过,d出密码设置的d窗 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,兼容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(); } /* * 设置密码的d窗,以前没有设置密码 */ 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,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,"两次密码不一致",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模仿用户设置密码实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android模仿用户设置密码实例全部内容,希望文章能够帮你解决Android模仿用户设置密码实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)