首页 > 系统 > Android > 正文

Android模仿用户设置密码实例

2019-12-12 03:25:40
字体:
来源:转载
供稿:网友

首先有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;         }       }     });   }   /*    * 显示密码的弹窗    */   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模仿用户设置密码实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表