添加设置密码功能

这是一个小Demo,可以设置密码,然后更改密码。可以添加到你需要的功能上面,比如需要输入密码才能查看个人信息大笑

很简单,先看效果图,在写代码啦。界面有点儿丑,自己可以根据需求修改。



SettingPwdActy.java

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SettingPwdActy extends Activity{
	
    String fileName = "syspass.txt";
    public boolean fileIsExists(){
        try{
            File f = new File(fileName);
            if(f.exists()==true){
                return false;
            }else{
                return true;
            }
        }catch(Exception e){
            return false;
        }
    }
    String syspassword = null;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.twodialog);
		
		inputTitleDialog();
		
		
	}


	 private void inputTitleDialog() {//huoqu shurumima   panduanshifouzhengque
	        if(!fileIsExists()==false){
	                FileInputStream is = null;
	                ByteArrayOutputStream os2 = null;

	                try{
	                    is = openFileInput(fileName);
	                    os2 = new ByteArrayOutputStream();
	                    int len = 0;
	                    byte[] buffer = new byte[1024];
	                    while((len=is.read(buffer))!=-1){
	                        os2.write(buffer,0,len);
	                    }
	                }catch(IOException e){
	                    Log.i("SettingsActivity","error");
	                }finally{
	                    try{
	                        if(os2!=null){
	                            os2.close();
	                        }
	                        if(is!=null){
	                            is.close();
	                        }
	                    }catch(IOException e){
	                        Log.i("SettingsActivity","error");
	                    }
	                }

	                if(os2!=null){
	                    byte[] content_byte = os2.toByteArray();
	                    syspassword = new String(content_byte);
	                }
	        }else{
	            syspassword=null;
	        }
	        final String syspass = syspassword;
	        if(syspass==null){
	            showSetDialog();
	        }else{
	            final EditText passEdit = new EditText(this);
	            AlertDialog builder = new AlertDialog.Builder(this)
	            .setTitle("键入密码")
	            .setIcon(R.drawable.ic_launcher)
	            .setView(passEdit)
	            .setOnKeyListener(new OnKeyListener(){
	                public boolean onKey(DialogInterface dialog,int keyCode,KeyEvent event){
	                    if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
	                    	SettingPwdActy.this.finish();
	                        return false;
	                    }
	                    return false;
	                }
	            })
	            .setNegativeButton(getString(R.string.preference_change_password_title),//change the password
	                    new DialogInterface.OnClickListener() {
	                        public void onClick(DialogInterface dialog, int which) {
	                                String inputPassWord = passEdit.getText().toString();
	                                if(!(inputPassWord.equals(syspass)) || inputPassWord==null){
	                                    Toast.makeText(SettingPwdActy.this,"旧密码错误,请重试",Toast.LENGTH_SHORT).show();
	                                    // SettingsActivity.this.finish();
	                                    passEdit.setText("");
	                                    try{
	                                        //The fllowing three sentences contorl the closing of the frame
	                                        Field field=dialog.getClass().getSuperclass().getDeclaredField("mShowing");
	                                        field.setAccessible(true);
	                                        field.set(dialog,false);//true:Close the dialog box  false:The dailog box is not closed
	                                    }catch(Exception e){
	                                        e.printStackTrace();
	                                    }
	                                    
	                                }else{
	                                    showSetDialog();
	                                }
	                        }
	            })
	            .setPositiveButton(getString(R.string.lockpassword_ok_label),
	                    new DialogInterface.OnClickListener() {
	                        public void onClick(DialogInterface dialog, int which) {
	                                String inputPassWord = passEdit.getText().toString();
	                                if(!(inputPassWord.equals(syspass)) ){ 
	                                    Toast.makeText(SettingPwdActy.this,R.string.lockpassword_confirm_passwords_dont_match,Toast.LENGTH_SHORT).show();
	                                    SettingPwdActy.this.finish();
	                                }
	                        }
	            })
	            .show();
	            builder.setCanceledOnTouchOutside(false);
	        }
	    }

	    public void showSetDialog() {  //she zhi chu shi mi ma
	            LayoutInflater factory = LayoutInflater.from(this);  
	            final View textEntryView = factory.inflate(R.layout.dialog, null);  
	            final EditText editTextPass = (EditText) textEntryView.findViewById(R.id.editTextPass);  
	            final EditText editTextrePass = (EditText)textEntryView.findViewById(R.id.editTextrePass);  
	            AlertDialog ad1 = new AlertDialog.Builder(this)
	            .setTitle("设置密码:")
//	            .setIcon(R.drawable.ic_settings_security) 
	            .setView(textEntryView)
	            .setOnKeyListener(new OnKeyListener(){
	                public boolean onKey(DialogInterface dialog,int keyCode,KeyEvent event){
	                    if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
	                    	SettingPwdActy.this.finish();
	                        return false;
	                    }
	                    return false;
	                }
	            })
	            .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	                public void onClick(DialogInterface dialog, int i) {   
	                    // Prompt two passwords do not matchdestv 
	                    if(!(editTextPass.getText().toString().equals(editTextrePass.getText().toString())) || editTextPass.getText().toString().equals("") || editTextrePass.getText().toString().equals("")){// 
	                        Toast.makeText(SettingPwdActy.this,R.string.lockpassword_confirm_passwords_dont_match,Toast.LENGTH_LONG).show();
	                        SettingPwdActy.this.finish();
	                    }else{
	                        FileOutputStream os = null;
	                        try{
	                            os = openFileOutput(fileName,Context.MODE_PRIVATE);
	                            os.write((editTextPass.getText().toString()).getBytes());
	                        }catch(IOException e){
	                            Log.i("SettingsActivity","error");
	                        }finally{
	                            try{
	                                os.close();
	                            }catch(IOException e){
	                                Log.i("SettingsActivity","error");
	                            }
	                        }
	                    }
	                }  
	            })   
	            .show();
	            ad1.setCanceledOnTouchOutside(false);
	    }
}

setContentView(R.layout.twodialog); //twodialog.xml 这个布局文件我什么也没有写,设置密码是一开始就启动的,所以这个界面根据自己的需求写吧。



dialog.xml

这个界面就两个输入框

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editTextPass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textView1"
        android:hint="密码" />

    <EditText
        android:id="@+id/editTextrePass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_below="@+id/editTextPass"
        android:layout_toRightOf="@+id/textView2"
        android:ems="10"
        android:hint="确认密码" />
</RelativeLayout>

还有几个字体

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>    
<string name="preference_change_password_title">更改密码</string>
    <string name="lockpassword_ok_label">确定</string>
    <string name="lockpassword_confirm_passwords_dont_match">旧密码不匹配</string>
</resources>


demo下载:http://download.csdn.net/detail/qq_32611951/9652955

好啦,大概就这些了,需要的就拿去玩儿吧 吐舌头



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值