十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
首先有2个对话框,没有设置过密码,需要设置dialog_set_password.xml,用户设置过密码,不需要设置,直接输入密码dialog_input_password.xml,
创新互联公司十年专注成都高端网站建设按需定制开发服务,为客户提供专业的成都网站制作,成都网页设计,成都网站设计服务;创新互联公司服务内容包含成都网站建设,成都小程序开发,软件开发,网络营销推广,网络运营服务及企业形象设计;创新互联公司拥有众多专业的高端网站制作开发团队,资深的高端网页设计团队及经验丰富的架构师高端网站策划团队;我们始终坚持从客户的角度出发,为客户量身订造网络营销方案,解决网络营销疑问。
设置对话框dialog_set_password.xml
<?xml version="1.0" encoding="utf-8"?>
输入对话框dialog_input_password.xml,
<?xml version="1.0" encoding="utf-8"?>
业务逻辑并实现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模仿用户设置密码实例,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!