首先有2個對話框,沒有設(shè)置過密碼,需要設(shè)置dialog_set_password.xml,用戶設(shè)置過密碼,不需要設(shè)置,直接輸入密碼dialog_input_password.xml,
創(chuàng)新互聯(lián)公司十年專注成都高端網(wǎng)站建設(shè)按需定制開發(fā)服務(wù),為客戶提供專業(yè)的成都網(wǎng)站制作,成都網(wǎng)頁設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)服務(wù);創(chuàng)新互聯(lián)公司服務(wù)內(nèi)容包含成都網(wǎng)站建設(shè),成都小程序開發(fā),軟件開發(fā),網(wǎng)絡(luò)營銷推廣,網(wǎng)絡(luò)運(yùn)營服務(wù)及企業(yè)形象設(shè)計(jì);創(chuàng)新互聯(lián)公司擁有眾多專業(yè)的高端網(wǎng)站制作開發(fā)團(tuán)隊(duì),資深的高端網(wǎng)頁設(shè)計(jì)團(tuán)隊(duì)及經(jīng)驗(yàn)豐富的架構(gòu)師高端網(wǎng)站策劃團(tuán)隊(duì);我們始終堅(jiān)持從客戶的角度出發(fā),為客戶量身訂造網(wǎng)絡(luò)營銷方案,解決網(wǎng)絡(luò)營銷疑問。
設(shè)置對話框dialog_set_password.xml
<?xml version="1.0" encoding="utf-8"?>
輸入對話框dialog_input_password.xml,
<?xml version="1.0" encoding="utf-8"?>
業(yè)務(wù)邏輯并實(shí)現(xiàn)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[] {"手機(jī)防盜", "通訊衛(wèi)士", "軟件管理", "進(jìn)程管理", "流量統(tǒng)計(jì)", "手機(jī)殺毒", "緩存清理", "高級工具", "設(shè)置中心"}; 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()); //監(jiān)聽gridview點(diǎn)擊事件 gv_home.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch(position){ case 0: //手機(jī)防盜 showPasswordDialog(); break; case 8: //設(shè)置中心 startActivity(new Intent(HomeActivity.this, SettingActivity.class)); break; default: break; } } }); } /* * 顯示密碼的彈窗 */ protected void showPasswordDialog(){ //判斷是否設(shè)置密碼 String savePassword = mPref.getString("password", null); if(!TextUtils.isEmpty(savePassword)){ //輸入密碼的彈窗 showPasswordInputDialog(); }else{ //如果沒有設(shè)置過,彈出密碼設(shè)置的彈窗 showPasswordSetDialog(); } } /* * 輸入密碼框,設(shè)置過密碼只需要登陸 */ 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設(shè)置到dialog dialog.setView(view, 0, 0, 0, 0);//設(shè)置邊距為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); //監(jiān)聽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(); //跳轉(zhuǎn)到手機(jī)防盜界面 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(); } } }); //監(jiān)聽取消按鍵 btnCancle.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } /* * 設(shè)置密碼的彈窗,以前沒有設(shè)置密碼 */ 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設(shè)置到dialog dialog.setView(view, 0, 0, 0, 0);//設(shè)置邊距為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); //監(jiān)聽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(); //跳轉(zhuǎn)到手機(jī)防盜界面 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(); } } }); //監(jiān)聽取消按鍵 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模仿用戶設(shè)置密碼實(shí)例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!