您的位置:首页 > 其它

6、手机防盗--密码设置

2013-12-25 18:03 99 查看
在主界面点击第一个功能“手机防盗”进入手机防盗页面,如果是第一次进入则弹出密码设置框让用户设置密码,否则弹出密码输入框让用户输入密码进入。

界面效果图:





新建“手机防盗”页面,Activity为LostProtectedActivity,layout为layout\activity_lostprotected.xml,界面布局下一节详细设计,目前为一个空白页面。

添加MainActivity对Item点击的事件处理代码,当点击第一个item的时候进入LostProtectedActivity:

package com.example.mobilesafe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

/**
* Created by sing on 13-12-24.
* desc:
*/
public class MainActivity extends Activity {
//activity_main中的gridview控件
private GridView gv_main;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gv_main = (GridView)findViewById(R.id.gv_main);
//为gv_main对象设置适配器,该适配器为每个item填充对应的数据
gv_main.setAdapter(new MainAdapter(this));

//为gv_main设置点击item的处理事件
gv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
/**
* item点击事件
* @param adapterView
* @param view
* @param i
* @param l
*/
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = null;
switch (i) {
case 0: //手机防盗
intent = new Intent(MainActivity.this, LostProtectedActivity.class);
startActivity(intent);
break;
case 8: //设置中心
intent = new Intent(MainActivity.this, SettingCenterActivity.class);
startActivity(intent);
break;
}
}
});
}
}


LostProtectedActivity的onCreate里判断是否是第一次进入“手机防盗”,判断的依据是配置文件中是否有设置过密码。如果是第一次进入该页面则弹出密码设置对话框,如图一,否则弹出密码输入对话框,如图二。

package com.example.mobilesafe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.utils.util;

/**
* Created by sing on 13-12-25.
* desc:
*/
public class LostProtectedActivity extends Activity implements View.OnClickListener {

//保存配置
private SharedPreferences sp;

private AlertDialog dialog;

//第一次进入手机防盗弹出设置密码对话框的控件
private EditText et_first_dlg_pswd;
private EditText et_first_dlg_pswd_confirm;
private Button bt_first_dlg_ok;
private Button bt_first_dlg_cancel;

//非第一次进入手机防盗弹出设置密码对话框的控件
private EditText et_normal_dlg_pswd;
private Button bt_normal_dlg_ok;
private Button bt_normal_dlg_cancel;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lostprotected);

sp = getSharedPreferences("config", MODE_PRIVATE);

//如果是第一次进入手机防盗则弹出设置密码对话框,否则弹出输入密码对话框
if (isFirstEntry()) {
showFirstEntryDialog();
}else {
showNormalEntryDialog();
}
}

/**
* 判断是否是第一次进入手机防盗页面,判断方法是检测有没有设置过密码
* @return
*/
private boolean isFirstEntry() {
String password = sp.getString("password", "");
return password.isEmpty();
}

/**
*第一次进入手机防盗弹出设置密码对话框
*/
private void showFirstEntryDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = View.inflate(this, R.layout.first_entry_dialog, null);
et_first_dlg_pswd = (EditText)view.findViewById(R.id.et_first_dlg_pswd);
et_first_dlg_pswd_confirm = (EditText)view.findViewById(R.id.et_first_dlg_pswd_confirm);
bt_first_dlg_ok = (Button)view.findViewById(R.id.bt_first_dlg_ok);
bt_first_dlg_cancel = (Button)view.findViewById(R.id.bt_first_dlg_cancel);

//xml中已经设置android:onClick="onClick",这里无需再设置
//bt_first_dlg_ok.setOnClickListener(this);
//bt_first_dlg_cancel.setOnClickListener(this);

builder.setView(view);
dialog = builder.create();
dialog.show();
}

/**
*非第一次进入手机防盗弹出输入密码对话框
*/
private void showNormalEntryDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = View.inflate(this, R.layout.normal_entry_dialog, null);
et_normal_dlg_pswd = (EditText)view.findViewById(R.id.et_normal_dlg_pswd);
bt_normal_dlg_ok = (Button)view.findViewById(R.id.bt_normal_dlg_ok);
bt_normal_dlg_cancel = (Button)view.findViewById(R.id.bt_normal_dlg_cancel);

//xml中已经设置android:onClick="onClick",这里无需再设置
//bt_normal_dlg_ok.setOnClickListener(this);
//bt_normal_dlg_cancel.setOnClickListener(this);

builder.setView(view);
dialog = builder.create();
dialog.show();
}

@Override
public void onClick(android.view.View view) {
switch (view.getId()) {
case R.id.bt_first_dlg_cancel:
dialog.cancel();
finish();
break;
case R.id.bt_first_dlg_ok:
String pswd = et_first_dlg_pswd.getText().toString().trim();
String pswdconfirm = et_first_dlg_pswd_confirm.getText().toString().trim();

//密码不能为空
if (pswd.isEmpty() || pswdconfirm.isEmpty()) {
Toast.makeText(this, "密码不能为空", 1).show();
return;
}

//判断两次密码输入是否相等
if (pswd.equals(pswdconfirm)) {
//保存密码
SharedPreferences.Editor editor = sp.edit();
editor.putString("password", util.md5String(pswd));
editor.commit();
dialog.dismiss();
//设置完毕后进入主界面,要求用户重新进入
finish();
}else {
Toast.makeText(this, "两次密码不相同", 1).show();
return;
}
break;
case R.id.bt_normal_dlg_cancel:
finish();
break;
case R.id.bt_normal_dlg_ok:
String inputpswd = et_normal_dlg_pswd.getText().toString().trim();
if (inputpswd.isEmpty()) {
Toast.makeText(this, "密码不能为空", 1).show();
return;
}

String savedpswd = sp.getString("password", "");
if (util.md5String(inputpswd).equals(savedpswd)) {
Toast.makeText(this, "密码正确进入界面", 1).show();
dialog.dismiss();
return;
}else {
Toast.makeText(this, "密码不正确", 1).show();
return;
}
}
}

}

isFirstEntry通过读取配置文件中password是否有被设置过来判断是否是第一次进入“手机防盗”,配置文件保存在:

/data/data/com.example.mobilesafe/shared_prefs/config.xml

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="autoupdate" value="false" />
<string name="password">202cb962ac59075b964b07152d234b70</string>
</map>


第一次进入手机防盗弹出设置密码对话框的showFirstEntryDialog和非第一次进入手机防盗弹出输入密码对话框的showNormalEntryDialog,使用的对话框布局分别为:first_entry_dialog.xml、normal_entry_dialog.xml。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="设置密码"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textStyle="bold"
android:gravity="center_horizontal" />
<View
android:layout_height="1dip"
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="@drawable/devide_line" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_first_dlg_pswd"
android:hint="请输入密码" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_first_dlg_pswd_confirm"
android:layout_gravity="center_horizontal"
android:hint="请再次输入密码" />
<View
android:layout_height="1dip"
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="@drawable/listview_devider" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="确定"
android:id="@+id/bt_first_dlg_ok"
android:onClick="onClick"
android:layout_weight="1" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="取消"
android:id="@+id/bt_first_dlg_cancel"
android:onClick="onClick"
android:layout_weight="1" />
</LinearLayout>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="输入密码"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:textStyle="bold"
android:gravity="center_horizontal" />
<View
android:layout_height="1dip"
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="@drawable/devide_line" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et_normal_dlg_pswd"
android:hint="请输入密码" />
<View
android:layout_height="1dip"
android:layout_width="fill_parent"
android:layout_marginTop="5dip"
android:background="@drawable/listview_devider" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="确定"
android:id="@+id/bt_normal_dlg_ok"
android:onClick="onClick"
android:layout_weight="1" />

<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="取消"
android:id="@+id/bt_normal_dlg_cancel"
android:onClick="onClick"
android:layout_weight="1" />

</LinearLayout>
</LinearLayout>


两个按钮由于是平分所在LinearLayout容器的宽度,所以不设置它们的宽度属性,而设置权重属性:android:layout_weight="1",因为权重都设置为1,那么它们所占父容器的宽度比例就是1:1,也就是各占一半。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: