您好,登錄后才能下訂單哦!
首先有2個對話框,沒有設置過密碼,需要設置dialog_set_password.xml,用戶設置過密碼,不需要設置,直接輸入密碼dialog_input_password.xml,
設置對話框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="設置密碼" android:padding="10dp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="請輸入密碼" /> <EditText android:id="@+id/et_password_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="請再次輸入密碼" /> <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>
輸入對話框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="請輸入密碼" /> <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>
業務邏輯并實現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模仿用戶設置密碼實例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。