我的日记设计文档
三年级 日记 16105字 329人浏览 北漂浪子任以品

我的日记设计文档

项目名称:mydiary2 程序目录:

一、最终效果图

二、实现功能

1. 实现保存密码的功能

具体代码:

sp =getSharedPreferences("userinfo" ,Context. MODE_PRIVATE); name .setText(sp .getString("USERNAME" , "" )); if (sp .getBoolean("REMBERPWD" , false )){

pwd .setText(sp .getString("PASSWORD" , "" )); }else {

pwd .setText("" );

}

rm_pass.setChecked(sp .getBoolean("REMBERPWD" , false ));

if (rm_pass.isChecked()){

editor.putString("USERNAME" , username); editor.putString("PASSWORD" , password); editor.putBoolean("REMBERPWD" , true );

editor.commit();

}else {

editor.putBoolean("REMBERPWD" , false );

editor.commit();

}

2. 显示登录进度

具体代码: void initHandler() {

handler =new @Override

publicvoid handleMessage(Message msg){

switch (msg.what ) {

case CONTINUE :

if (!Thread.currentThread ().isInterrupted()){ progressBar .setProgress(progress ); }

break ;

case STOP :

Intent intent=new Intent();

intent.setClass(getApplicationContext(), twoActivity. class );

startActivity(intent);

finish();

break ;

default :

break ;

} super .handleMessage(msg); } }; progress =0; progressBar .setProgress(progress ); progressBar .setMax(MAX ); progressBar .setVisibility(View.VISIBLE ); new Thread(new Runnable() { @Override publicvoid run() { // try { for (int i=0;i<5;i++) { progress =(i+1)*20; Thread. sleep (1000); if (i==4){ Message msg=new Message(); msg. what =STOP ; handler .sendMessage(msg); break ; }else { Message msg=new Message(); msg. what =CONTINUE ; handler .sendMessage(msg); } } }catch (InterruptedException e){ e.printStackTrace(); }

} }).start();

3. 保存日记到数据库

具体代码:

private SQLiteOpenHelper helper ;

private SQLiteDatabase DB ;

privatestaticfinal String DATABASETable ="dia" ;

privatevoid initsql() {

helper =new mysqlitehelper(twoActivity.this ); //实例化 DB =helper .getWritableDatabase();

}

ContentValues va=new ContentValues();

va.put("wz" , context);

DB .insert(DATABASETable , null , va);

//

//

Toast. makeText (twoActivity.this , " 保存成功!" , Toast. LENGTH_SHORT).show();

DB .close();

4. 退出菜单显示

具体代码:

//退出菜单

@Override

publicboolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main , menu);

returntrue ;

}

@Override

publicboolean onOptionsItemSelected(MenuItem item) { //

switch (item.getItemId()) {

case R.id.nihao :

Toast. makeText (getApplicationContext(), "mydiary 1.0", Toast. LENGTH_SHORT).show();

break ;

case R.id.out :

creatDialog();

break ;

default :

break ;

}

returnsuper .onOptionsItemSelected(item);

}

}

5. 点击退出,弹出对话框

具体代码:

privatevoid creatDialog() {

//

AlertDialog.Builder builder=new

AlertDialog.Builder(MainActivity.this );

builder.setMessage(" 确定退出吗?" );

builder.setTitle(" 提示" );

builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setPositiveButton(" 确认" , new

DialogInterface.OnClickListener() {

@Override

publicvoid onClick(DialogInterface dialog, int which) { //

finish();

}

});

builder.setNegativeButton(" 取消" , new

DialogInterface.OnClickListener() {

@Override

publicvoid onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub

}

});

builder.create().show();

}

三、代码设计

文件名称:mainactivity.java package com.example.mydiary;

import android.app.Activity;

import android.app.ActionBar;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder; import android.app.Fragment;

import android.content.Context;

import android.content.DialogInterface; import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor; import android.graphics.LightingColorFilter; import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.LayoutInflater; import android.view.Menu;

import android.view.MenuItem;

import android.view.SubMenu;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.ProgressBar; import android.widget.Toast;

import android.os.Build;

public class MainActivity extends Activity { private EditText name,pwd;

private CheckBox rm_pass;

private Button btn;

private SharedPreferences sp; private ProgressBar progressBar; private Handler handler;

private static final int STOP=0x111; private static final int CONTINUE=0x112; private static final int MAX=100; private int progress;

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setfindview(); sp(); setListener(); initHandler(); } void initHandler() { handler=new Handler(){ @Override public void handleMessage(Message msg){ switch (msg.what) { case CONTINUE: if(!Thread.currentThread().isInterrupted()){ progressBar.setProgress(progress); } break; case STOP: Intent intent=new Intent(); intent.setClass(getApplicationContext(), twoActivity.class); startActivity(intent); finish(); break; default: break; } super.handleMessage(msg); } }; }

private void sp() {

sp=getSharedPreferences("userinfo",Context.MODE_PRIVATE);

name.setText(sp.getString("USERNAME", ""));

if(sp.getBoolean("REMBERPWD", false)){

pwd.setText(sp.getString("PASSWORD", ""));

}else{

pwd.setText("");

}

rm_pass.setChecked(sp.getBoolean("REMBERPWD", false));

progress=0;

progressBar.setProgress(progress);

progressBar.setMax(MAX);

}

private void setListener() {

//

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String username=String.valueOf(name.getText());

String password=String.valueOf(pwd.getText()); if((!username.equals("admin"))||(!password.equals("admin"))){ Toast.makeText(MainActivity.this, "用户名或者密码错误!", Toast.LENGTH_LONG).show();

}else{

Editor editor=sp.edit();

if(rm_pass.isChecked()){

editor.putString("USERNAME", username);

editor.putString("PASSWORD", password);

editor.putBoolean("REMBERPWD", true);

editor.commit();

}else{

editor.putBoolean("REMBERPWD",false);

editor.commit();

}

name.setEnabled(false);

pwd.setEnabled(false);

btn.setEnabled(false);

progressBar.setVisibility(View.VISIBLE);

new Thread(new Runnable() {

@Override

public void run() { // try{ for(int i=0;i<5;i++) { progress=(i+1)*20; Thread.sleep(1000); if(i==4){ Message msg=new Message(); msg.what=STOP; handler.sendMessage(msg); break; }else{ Message msg=new Message(); msg.what=CONTINUE; handler.sendMessage(msg); } } }catch(InterruptedException e){ e.printStackTrace(); } } }).start(); }} }); } private void setfindview() { //

btn=(Button) findViewById(R.id.btn); name=(EditText) findViewById(R.id.et_name);

pwd=(EditText) findViewById(R.id.et_pass); rm_pass=(CheckBox) findViewById(R.id.checkBox1); progressBar=(ProgressBar) findViewById(R.id.progressBar1); } //对话框 private void creatDialog() { // AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setMessage("确定退出吗?"); builder.setTitle("提示"); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }); builder.create().show(); } //退出菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { //

switch (item.getItemId()) { case R.id.nihao:

Toast.makeText(getApplicationContext(), "mydiary 1.0", Toast.LENGTH_SHORT).show();

break;

case R.id.out:

creatDialog();

break;

default:

break;

}

return super.onOptionsItemSelected(item);

}

}

文件名称:twoactivity.java

package com.example.mydiary;

import android.app.Activity;

import android.app.ActionBar;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.app.Fragment;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.graphics.LightingColorFilter;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.SubMenu;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.ProgressBar;

import android.widget.Toast;

import android.os.Build;

public class MainActivity extends Activity {

private EditText name,pwd;

private CheckBox rm_pass;

private Button btn;

private SharedPreferences sp;

private ProgressBar progressBar;

private Handler handler;

private static final int STOP=0x111;

private static final int CONTINUE=0x112;

private static final int MAX=100;

private int progress;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

setfindview();

sp();

setListener();

initHandler();

}

void initHandler() {

handler=new Handler(){

@Override

public void handleMessage(Message msg){

switch (msg.what) {

case CONTINUE:

if(!Thread.currentThread().isInterrupted()){

progressBar.setProgress(progress);

}

break;

case STOP:

Intent intent=new Intent();

intent.setClass(getApplicationContext(), twoActivity.class); startActivity(intent);

finish();

break;

default:

break;

}

super.handleMessage(msg);

}

};

}

private void sp() {

sp=getSharedPreferences("userinfo",Context.MODE_PRIVATE); name.setText(sp.getString("USERNAME", ""));

if(sp.getBoolean("REMBERPWD", false)){

pwd.setText(sp.getString("PASSWORD", ""));

}else{

pwd.setText("");

}

rm_pass.setChecked(sp.getBoolean("REMBERPWD", false));

progress=0;

progressBar.setProgress(progress);

progressBar.setMax(MAX);

}

private void setListener() {

//

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String username=String.valueOf(name.getText());

String password=String.valueOf(pwd.getText());

if((!username.equals("admin"))||(!password.equals("admin"))){ Toast.makeText(MainActivity.this, "用户名或者密码错误!", Toast.LENGTH_LONG).show();

}else{

Editor editor=sp.edit();

if(rm_pass.isChecked()){

editor.putString("USERNAME", username);

editor.putString("PASSWORD", password);

editor.putBoolean("REMBERPWD", true); editor.commit(); }else{ editor.putBoolean("REMBERPWD",false); editor.commit(); } name.setEnabled(false); pwd.setEnabled(false); btn.setEnabled(false); progressBar.setVisibility(View.VISIBLE); new Thread(new Runnable() { @Override public void run() { // try{ for(int i=0;i<5;i++) { progress=(i+1)*20; Thread.sleep(1000); if(i==4){ Message msg=new Message(); msg.what=STOP; handler.sendMessage(msg); break; }else{ Message msg=new Message(); msg.what=CONTINUE; handler.sendMessage(msg); } } }catch(InterruptedException e){ e.printStackTrace();

} }

}).start(); }} }); } private void setfindview() { // btn=(Button) findViewById(R.id.btn); name=(EditText) findViewById(R.id.et_name); pwd=(EditText) findViewById(R.id.et_pass); rm_pass=(CheckBox) findViewById(R.id.checkBox1); progressBar=(ProgressBar) findViewById(R.id.progressBar1); } //对话框 private void creatDialog() { // AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setMessage("确定退出吗?"); builder.setTitle("提示"); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub

}

});

builder.create().show();

}

//退出菜单

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) { //

switch (item.getItemId()) {

case R.id.nihao:

Toast.makeText(getApplicationContext(), "mydiary 1.0", Toast.LENGTH_SHORT).show();

break;

case R.id.out:

creatDialog();

break;

default:

break;

}

return super.onOptionsItemSelected(item);

}

}

文件名称:mysqlitehelper.java

package com.example.mydiary;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper;

public class mysqlitehelper extends SQLiteOpenHelper {

private static final String DATABASE="diary.db";

private static final int DATABASEversion=1;

private static final String DATABASETable="dia";

public mysqlitehelper(Context context) {

super(context, DATABASE, null, DATABASEversion);

}

@Override

public void onCreate(SQLiteDatabase db) {

//创建表SQL 语句

String stu_table="create table "+DATABASETable+"(_id integer primary key autoincrement,wz varchar(50) not null)";

//执行SQL 语句

db.execSQL(stu_table);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

} 文件名称:activity_main.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/bg8">

<LinearLayout

android:id="@+id/linearLayout1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_centerVertical="true"

android:orientation="vertical" >

<EditText

android:id="@+id/et_name"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="@string/n"/>

<EditText

android:id="@+id/et_pass"

android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10"

android:hint="@string/p"

android:inputType="textPassword" />

<CheckBox

android:id="@+id/checkBox1"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/j"

android:layout_gravity="right" />

<Button

android:id="@+id/btn"

android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/b"/>

</LinearLayout >

<ProgressBar

android:id="@+id/progressBar1"

style ="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content"

android:layout_below="@+id/linearLayout1" android:layout_centerHorizontal="true" android:visibility="gone" />

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:layout_above="@+id/linearLayout1" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" android:text="@string/wo"

android:textSize="35sp" />

</RelativeLayout >

文件名称:activity_two.java

<?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:background="@drawable/bg8"

android:orientation="vertical" >

<EditText

android:id="@+id/diary"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="8"

android:hint=""

android:inputType="textMultiLine"

android:minLines="15"

android:scrollbars="vertical"

android:singleLine="false" >

<requestFocus android:layout_width="wrap_content"/>

</EditText >

<Button

android:id="@+id/btn1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="@string/btn1"/>

</LinearLayout >