android131 360 03 输入密码

时间:2022-10-17 13:19:05

android131 360 03  输入密码

package com.itheima52.mobilesafe.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.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.utils.MD5Utils; /**
* 主页面
*/
public class HomeActivity extends Activity { private GridView gvHome; private String[] mItems = new String[] { "手机防盗", "通讯卫士", "软件管理", "进程管理",
"流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心" }; private int[] mPics = new int[] { R.drawable.home_safe,
R.drawable.home_callmsgsafe, R.drawable.home_apps,
R.drawable.home_taskmanager, R.drawable.home_netmanager,
R.drawable.home_*, R.drawable.home_sysoptimize,
R.drawable.home_tools, R.drawable.home_settings }; private SharedPreferences mPref; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mPref = getSharedPreferences("config", MODE_PRIVATE);
gvHome = (GridView) findViewById(R.id.gv_home);
gvHome.setAdapter(new HomeAdapter());
// 设置监听
gvHome.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() {
//判断是否设置密码,密码保存在SharedPreferences中,是否自动更新也保存在SharedPreferences中。SharedPreferences是以xml格式保存。
String savedPassword = mPref.getString("password", null);//默认值是null
if (!TextUtils.isEmpty(savedPassword)) {
// 输入密码弹窗
showPasswordInputDialog();
} else {
// 如果没有设置过, 弹出设置密码的弹窗
showPasswordSetDailog();
}
} /**
* 输入密码弹窗
*/
private void showPasswordInputDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dailog_input_password, null);
// dialog.setView(view);// 将自定义的布局文件设置给dialog
dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题 final EditText etPassword = (EditText) view.findViewById(R.id.et_password); Button btnOK = (Button) view.findViewById(R.id.btn_ok);
Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnOK.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
String password = etPassword.getText().toString(); if (!TextUtils.isEmpty(password)) {
String savedPassword = mPref.getString("password", null); if (MD5Utils.encode(password).equals(savedPassword)) {
// Toast.makeText(HomeActivity.this, "登录成功!",
// Toast.LENGTH_SHORT).show();
dialog.dismiss(); // 跳转到手机防盗页
startActivity(new Intent(HomeActivity.this,
LostFindActivity.class));
} else {
Toast.makeText(HomeActivity.this, "密码错误!",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(HomeActivity.this, "输入框内容不能为空!",
Toast.LENGTH_SHORT).show();
}
}
}); btnCancel.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
dialog.dismiss();// 隐藏dialog
}
}); dialog.show();
} /**
* 设置密码的弹窗
*/
private void showPasswordSetDailog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create(); View view = View.inflate(this, R.layout.dailog_set_password, null);
// dialog.setView(view);// 将自定义的布局文件设置给dialog
dialog.setView(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题 final EditText etPassword = (EditText) view
.findViewById(R.id.et_password);
final EditText etPasswordConfirm = (EditText) view
.findViewById(R.id.et_password_confirm); Button btnOK = (Button) view.findViewById(R.id.btn_ok);
Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnOK.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
String password = etPassword.getText().toString();
String passwordConfirm = etPasswordConfirm.getText().toString();
// password!=null && !password.equals("")
if (!TextUtils.isEmpty(password) && !passwordConfirm.isEmpty()) {
if (password.equals(passwordConfirm)) {
// Toast.makeText(HomeActivity.this, "登录成功!",
// Toast.LENGTH_SHORT).show(); // 将密码保存起来
mPref.edit()
.putString("password",
MD5Utils.encode(password)).commit(); dialog.dismiss(); // 跳转到手机防盗页
startActivity(new Intent(HomeActivity.this,
LostFindActivity.class));
} else {
Toast.makeText(HomeActivity.this, "两次密码不一致!",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(HomeActivity.this, "输入框内容不能为空!",
Toast.LENGTH_SHORT).show();
}
}
}); btnCancel.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
dialog.dismiss();// 隐藏dialog
}
}); dialog.show();
} class HomeAdapter extends BaseAdapter { @Override
public int getCount() {
return mItems.length;
} @Override
public Object getItem(int position) {
return mItems[position];
} @Override
public long getItemId(int position) {
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 ivItem = (ImageView) view.findViewById(R.id.iv_item);
TextView tvItem = (TextView) view.findViewById(R.id.tv_item); tvItem.setText(mItems[position]);
ivItem.setImageResource(mPics[position]);
return view;
} }
}

dailog_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:background="#fff"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#66ff6600"
android:gravity="center"
android:padding="10dp"
android:text="设置密码"
android:textColor="@color/black"
android:textSize="20sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" >
</EditText>
<EditText
android:id="@+id/et_password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请再次输入密码"
android:inputType="textPassword" >
</EditText>
<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_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
</LinearLayout>
</LinearLayout>

dailog_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:background="#fff"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#66ff6600"
android:gravity="center"
android:padding="10dp"
android:text="输入密码"
android:textColor="@color/black"
android:textSize="20sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" >
</EditText>
<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_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
</LinearLayout>
</LinearLayout>

MD5加密:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; public class Main { public static void main(String[] args) {
String password = "123456";
//MD5只是一个文件或者字符串的特征码。
try {
MessageDigest instance = MessageDigest.getInstance("MD5");// 获取MD5算法对象
byte[] digest = instance.digest(password.getBytes());// 对字符串加密,返回加密后的字节数组:[-31, 10, -36, 57, 73, -70, 89, -85, -66, 86, -32, 87, -14, 15, -120, 62] StringBuffer sb = new StringBuffer();
for (byte b : digest) {//-31,
int i = b & 0xff;// 获取字节的低八位有效值,0xff = 0000000011111111
String hexString = Integer.toHexString(i);// 将整数转为16进制
// System.out.println(hexString); if (hexString.length() < 2) {// 如果是1位的话,补0
hexString = "0" + hexString;
} sb.append(hexString);
}
System.out.println("md5:" + sb.toString());
System.out.println("md5 length:" + sb.toString().length());//Md5都是32位 } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
// 没有该算法时,抛出异常, 不会走到这里
}
}
}