android实战:密码箱三

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


登录界面源代码login.java:

package com.mimaxiang;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class login extends Activity implements View.OnClickListener
{
	//全局变量定义
	SharedPreferences mima_config;
	SharedPreferences.Editor editor;
	TextView txt_mima;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
		
        //获得配置文件
        mima_config = getSharedPreferences("mima_config", 0);
        editor = mima_config.edit();
        
        //写入配置:Id:10000 Name:Jdh
        //editor.putString("MiMaMoRen", "1234");
        //editor.commit();
        
        Button btn_ok = (Button)findViewById(R.id.button1);
        btn_ok.setOnClickListener(this);
        Button btn_quit = (Button)findViewById(R.id.button2);
        btn_quit.setOnClickListener(this);
        
        //开辟控件空间
        txt_mima = (TextView)findViewById(R.id.editText1);
    }
    
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        //android.os.Process.killProcess(android.os.Process.myPid());  
    }  

	@Override
	public void onClick(View v) 
	{
		// TODO Auto-generated method stub
		switch (v.getId())
    	{
    	case R.id.button1:
	    	{
	    		//判断密码是否正确
	    		String str_mima;
	    		str_mima = txt_mima.getText().toString();
	    		 //读取配置
	    		String mima = mima_config.getString("MiMa","1234");
	    		
	    		if (str_mima.equalsIgnoreCase(mima))
	    		{
	    			//切换界面
		    		Intent intent = new Intent(this,mimaxiang.class);
		    		startActivity(intent);
		    		login.this.finish();
	    		}
	    		else
	    		{
	    			txt_mima.setText("密码错误");
	    		}
	    		break;
	    	}
    	case R.id.button2:
    	{
    		//退出程序
    		login.this.finish();
    		break;
    	}
    	default:
    		break;
    	}
	}
}

主界面源代码mimaxiang.java:

package com.mimaxiang;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class mimaxiang extends Activity implements View.OnClickListener
{
	DatabaseHelper mOpenHelper;

	private static final String DATABASE_NAME = "mimaxiang.db";
	private static final int DATABASE_VERSION = 1;
	
	//数据库内容标号
	int index = 0;
	
	TextView txt_id;
	TextView txt_pwd;
	TextView txt_explain;
	boolean flag = false;
	
	private static class DatabaseHelper extends SQLiteOpenHelper 
	{
		DatabaseHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			//新建一个用户表
			db.execSQL("CREATE TABLE MiMaTable ('ID' VARCHAR,'PWD' VARCHAR,'EXPLAIN' VARCHAR)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
		{
		}
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mOpenHelper = new DatabaseHelper(this);
        
        Button btn_previous = (Button)findViewById(R.id.button1);
        btn_previous.setOnClickListener(this);
        Button btn_next = (Button)findViewById(R.id.button2);
        btn_next.setOnClickListener(this);
        Button btn_del = (Button)findViewById(R.id.button3);
        btn_del.setOnClickListener(this);
        Button btn_new = (Button)findViewById(R.id.button4);
        btn_new.setOnClickListener(this);
        Button btn_edit_mima = (Button)findViewById(R.id.button5);
        btn_edit_mima.setOnClickListener(this);
        Button btn_quit = (Button)findViewById(R.id.button6);
        btn_quit.setOnClickListener(this);
        
        //开辟控件空间
        txt_id = (TextView)findViewById(R.id.editText1);
        txt_pwd = (TextView)findViewById(R.id.editText2);
        txt_explain = (TextView)findViewById(R.id.editText3);
        
        //更新界面
		//获取数据库
    	SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        //db.execSQL("DELETE FROM MiMaTable");
    	//db.execSQL("INSERT INTO MiMaTable VALUES (?,?,?)", new Object[]{"jdh1","123456",null});
        //db.execSQL("INSERT INTO MiMaTable VALUES (?,?,?)", new Object[]{"jdh2","123456",null});
    	Cursor cursor = db.rawQuery("SELECT * FROM MiMaTable",new String[]{}); 
    	
    	//获得数据库数目
    	int count = cursor.getCount();
    	//判断数据库是否为空
    	if (count != 0)
    	{
    		//非空
    		//判断index是否越界
    		if (index > count - 1)
    		{
    			//越界
    			index = 0;
    		}
    		//更新界面
    		cursor.moveToPosition(index);
    		txt_id.setText(cursor.getString(0));
    		txt_pwd.setText(cursor.getString(1));
    		txt_explain.setText(cursor.getString(2)); 
    	}
    	else
    	{
    		//空
    		//清空界面
    		txt_id.setText("数据库空");
    		txt_pwd.setText("数据库空");
    		txt_explain.setText("数据库空");
    	}
    }
    
    @Override
	public void onClick(View v) 
	{
    	//获取数据库
    	SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    	
		// TODO Auto-generated method stub
		switch (v.getId())
    	{
    	case R.id.button1:
	    	{
	    		//后退键
	    		if (index > 0)
	    		{
	    			index--;
	    		}
	    		else
	    		{
	    			index = 0;
	    		}
	    		break;
	    	}
    	case R.id.button2:
	    	{
	    		//前进键
	    		index++;
	    		break;
	    	}
    	case R.id.button3:
	    	{
	    		//删除键
	    		//获取当前ID,MIMA
	    		String id_tmp = txt_id.getText().toString();
	    		String pwd_tmp = txt_pwd.getText().toString();
	    		//db.execSQL("DELETE * FROM MiMaTable WHERE ID = ?",new String[]{"jdh1"});
	    		db.delete("MiMaTable","ID = ? AND PWD = ?",new String[]{id_tmp,pwd_tmp});
	    		break;
	    	}
    	case R.id.button4:
	    	{
	    		//新建键
	    		//切换界面
    			Intent intent = new Intent(this,new_pwd.class);
	    		startActivity(intent);
	    		flag = true;
	    		mimaxiang.this.finish();
	    		break;
	    	}
    	case R.id.button5:
	    	{
	    		//修改密码键
	    		Intent intent = new Intent(this,edit_mima.class);
	    		startActivity(intent);
	    		flag = true;
	    		mimaxiang.this.finish();	    		
	    		break;
	    	}
    	case R.id.button6:
	    	{
	    		//退出键
	    		flag = false;
	    		mimaxiang.this.finish();
	    		break;
	    	}
    	default:
    		break;
    	}
		
		//更新界面
    	Cursor cursor = db.rawQuery("SELECT * FROM MiMaTable",new String[]{}); 
    	//获得数据库数目
    	int count = cursor.getCount();
    	//判断数据库是否为空
    	if (count != 0)
    	{
    		//非空
    		//判断index是否越界
    		if (index > count - 1)
    		{
    			//越界
    			index = 0;
    		}
    		//更新界面
    		cursor.moveToPosition(index);
    		txt_id.setText(cursor.getString(0));
    		txt_pwd.setText(cursor.getString(1));
    		txt_explain.setText(cursor.getString(2)); 
    	}
    	else
    	{
    		//空
    		//清空界面
    		txt_id.setText("数据库空");
    		txt_pwd.setText("数据库空");
    		txt_explain.setText("数据库空");
    	}
	}
    
    @Override  
    protected void onDestroy() 
    {  
    	super.onDestroy(); 
    	if (!flag)
    	{
    		android.os.Process.killProcess(android.os.Process.myPid()); 
    	}
    }  
}

修改密码界面源代码edit_mima.java:

package com.mimaxiang;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class edit_mima extends Activity implements View.OnClickListener
{
	//全局变量定义
	SharedPreferences mima_config;
	SharedPreferences.Editor editor;
	TextView txt_old_mima;
	TextView txt_new_mima1;
	TextView txt_new_mima2;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_mima);
        
        Button btn_ok = (Button)findViewById(R.id.button1);
        btn_ok.setOnClickListener(this);
        Button btn_quit = (Button)findViewById(R.id.button2);
        btn_quit.setOnClickListener(this);
        
        //获得配置文件
        mima_config = getSharedPreferences("mima_config",0);
        editor = mima_config.edit();
        
        //写入配置:Id:10000 Name:Jdh
        //editor.putString("MiMaMoRen", "1234");
        //editor.commit();
        
        //开辟控件空间
        txt_old_mima = (TextView)findViewById(R.id.editText1);
        txt_new_mima1 = (TextView)findViewById(R.id.editText2);
        txt_new_mima2 = (TextView)findViewById(R.id.editText3);
    }
    
    @Override
	public void onClick(View v) 
	{
		// TODO Auto-generated method stub
		switch (v.getId())
    	{
    	case R.id.button1:
	    	{
	    		//确认按键
	    		String old_mima;
	    		old_mima = txt_old_mima.getText().toString();
	    		String new_mima1;
	    		new_mima1 = txt_new_mima1.getText().toString();
	    		String new_mima2;
	    		new_mima2 = txt_new_mima2.getText().toString();
	    		 //读取配置
	    		String mima = mima_config.getString("MiMa","1234");
	    		//判断原密码是否正确
	    		if (!mima.equalsIgnoreCase(old_mima))
	    		{
	    			//不正确
	    			txt_old_mima.setText("原密码输入有误!");
	    			break;
	    		}
	    		//判断新密码是否相同
	    		if (!new_mima1.equalsIgnoreCase(new_mima2))
	    		{
	    			//不正确
	    			txt_new_mima2.setText("新密码两次输入不同!");		
	    			break;
	    		}
	    		//修改密码
	            editor.putString("MiMa",new_mima1);
	            editor.commit();
	            
	    		//切换界面
	    		Intent intent = new Intent(this,mimaxiang.class);
	    		startActivity(intent);
	    		edit_mima.this.finish();
	    		
	    		break;
	    	}
    	case R.id.button2:
    		{
    			//退出按键		
    			//切换界面
    			Intent intent = new Intent(this,mimaxiang.class);
	    		startActivity(intent);
	    		edit_mima.this.finish();
	    		
    			break;
    		}
    	default:
    		break;
    	}
	}
    
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        //android.os.Process.killProcess(android.os.Process.myPid());  
    }  
}

新帐户密码记录界面源代码new_pwd.java:

package com.mimaxiang;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class new_pwd extends Activity implements View.OnClickListener
{
	DatabaseHelper mOpenHelper;

	private static final String DATABASE_NAME = "mimaxiang.db";
	private static final int DATABASE_VERSION = 1;
	
	TextView txt_id;
	TextView txt_pwd;
	TextView txt_explain;
	
	private static class DatabaseHelper extends SQLiteOpenHelper 
	{
		DatabaseHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			//新建一个用户表
			//db.execSQL("CREATE TABLE MiMaTable ('ID' VARCHAR,'PWD' VARCHAR,'EXPLAIN' VARCHAR)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
		{
		}
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_pwd);
        
        mOpenHelper = new DatabaseHelper(this);
        
        Button btn_ok = (Button)findViewById(R.id.button1);
        btn_ok.setOnClickListener(this);
        Button btn_quit = (Button)findViewById(R.id.button2);
        btn_quit.setOnClickListener(this);
        
        //开辟控件空间
        txt_id = (TextView)findViewById(R.id.editText1);
        txt_pwd = (TextView)findViewById(R.id.editText2);
        txt_explain = (TextView)findViewById(R.id.editText3);
    }
    
    @Override
	public void onClick(View v) 
	{
    	//获取数据库
    	SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    	
		// TODO Auto-generated method stub
		switch (v.getId())
    	{
    	case R.id.button1:
	    	{
	    		//确认键
	    		db.execSQL("INSERT INTO MiMaTable VALUES (?,?,?)", new Object[]{txt_id.getText().toString(),
	    				txt_pwd.getText().toString(),txt_explain.getText().toString()});
	    		//切换界面
    			Intent intent = new Intent(this,mimaxiang.class);
	    		startActivity(intent);
	    		new_pwd.this.finish();
	    		break;
	    	}
    	case R.id.button2:
	    	{
	    		//退出键
	    		//切换界面
    			Intent intent = new Intent(this,mimaxiang.class);
	    		startActivity(intent);
	    		new_pwd.this.finish();
	    		break;
	    	}
    	default:
    		break;
    	}
	}
    
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        //android.os.Process.killProcess(android.os.Process.myPid());  
    }  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值