Android模仿用戶設(shè)置密碼實(shí)例

首先有2個(gè)對(duì)話框,沒(méi)有設(shè)置過(guò)密碼,需要設(shè)置dialog_set_password.xml,用戶設(shè)置過(guò)密碼,不需要設(shè)置,直接輸入密碼dialog_input_password.xml,

創(chuàng)新互聯(lián)公司十年專注成都高端網(wǎng)站建設(shè)按需定制開(kāi)發(fā)服務(wù),為客戶提供專業(yè)的成都網(wǎng)站制作,成都網(wǎng)頁(yè)設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)服務(wù);創(chuàng)新互聯(lián)公司服務(wù)內(nèi)容包含成都網(wǎng)站建設(shè),成都小程序開(kāi)發(fā),軟件開(kāi)發(fā),網(wǎng)絡(luò)營(yíng)銷推廣,網(wǎng)絡(luò)運(yùn)營(yíng)服務(wù)及企業(yè)形象設(shè)計(jì);創(chuàng)新互聯(lián)公司擁有眾多專業(yè)的高端網(wǎng)站制作開(kāi)發(fā)團(tuán)隊(duì),資深的高端網(wǎng)頁(yè)設(shè)計(jì)團(tuán)隊(duì)及經(jīng)驗(yàn)豐富的架構(gòu)師高端網(wǎng)站策劃團(tuán)隊(duì);我們始終堅(jiān)持從客戶的角度出發(fā),為客戶量身訂造網(wǎng)絡(luò)營(yíng)銷方案,解決網(wǎng)絡(luò)營(yíng)銷疑問(wèn)。

設(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ì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

網(wǎng)頁(yè)標(biāo)題:Android模仿用戶設(shè)置密碼實(shí)例
轉(zhuǎn)載來(lái)于:http://muchs.cn/article10/gddodo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站內(nèi)鏈建站公司、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)