Android模仿用戶設(shè)置密碼實(shí)例
首先有2個(gè)對(duì)話框,沒(méi)有設(shè)置過(guò)密碼,需要設(shè)置dialog_set_password.xml,用戶設(shè)置過(guò)密碼,不需要設(shè)置,直接輸入密碼dialog_input_password.xml,
設(shè)置對(duì)話框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="設(shè)置密碼"
android:padding="10dp"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請(qǐng)輸入密碼"
/>
<EditText
android:id="@+id/et_password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請(qǐng)?jiān)俅屋斎朊艽a"
/>
<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>
輸入對(duì)話框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="請(qǐng)輸入密碼"
/>
<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>
業(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ī)殺毒", "緩存清理", "高級(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)聽(tīng)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{
//如果沒(méi)有設(shè)置過(guò),彈出密碼設(shè)置的彈窗
showPasswordSetDialog();
}
}
/*
* 輸入密碼框,設(shè)置過(guò)密碼只需要登陸
*/
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個(gè)按鍵
Button btnOk = (Button)view.findViewById(R.id.btn_ok);
Button btnCancle = (Button)view.findViewById(R.id.btn_cancle);
//監(jiān)聽(tīng)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();
//對(duì)話框消失
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)聽(tīng)取消按鍵
btnCancle.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
/*
* 設(shè)置密碼的彈窗,以前沒(méi)有設(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個(gè)按鍵
Button btnOk = (Button)view.findViewById(R.id.btn_ok);
Button btnCancle = (Button)view.findViewById(R.id.btn_cancle);
//監(jiān)聽(tīng)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();
//對(duì)話框消失
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)聽(tīng)取消按鍵
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í)例,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)詳解
最近因?yàn)楣拘枨螅谧鯣PS定位,并且將獲得的坐標(biāo)顯示在高德地圖上,但是實(shí)際效果跟我們期望的是有偏差的。通過(guò)查閱資料,才知道有地球坐標(biāo)、火星坐標(biāo)之說(shuō)。下面這篇文章就詳細(xì)介紹了Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)的方法,需要的朋友可以參考下。2017-01-01
Android 基于Socket的聊天應(yīng)用實(shí)例(二)
本篇文章主要介紹了Android 基于Socket的聊天應(yīng)用實(shí)例,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
在Android環(huán)境下WebView中攔截所有請(qǐng)求并替換URL示例詳解
這篇文章主要介紹了在Android環(huán)境下WebView中攔截所有請(qǐng)求并替換URL示例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
android實(shí)現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫(kù)代碼
這篇文章主要介紹了android實(shí)現(xiàn)raw文件夾導(dǎo)入數(shù)據(jù)庫(kù)代碼,有需要的朋友可以參考一下2013-12-12
Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突
這篇文章主要為大家介紹了Android進(jìn)階事件分發(fā)機(jī)制解決事件沖突過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
簡(jiǎn)單說(shuō)說(shuō)Android中如何使用攝像頭和相冊(cè)
本篇文章主要介紹了簡(jiǎn)單說(shuō)說(shuō)Android中如何使用攝像頭和相冊(cè),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁(yè)面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

