Android记事本开发

数据库实现部分          界面设计

package com.jk;

import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.jk.R;
import com.jk.service.DbHelper;
import com.jk.service.DiaryAdapter;

public class MainActivity extends ListActivity {
	private static final int NEWDIARY = 0;
	private static final int HELP = 1;
	private static final int EXIT = 2;
	private static final int EDIT = 1;
	private static final int DELETE = 2;
	private static final int CANCEL = 4;
	private static final int DELETEALL = 3;
	private ListView listview;
	private int list_position;
	private DbHelper dbOpenHelper;
	private MyAdapter adapter;
	private List<Diary> mData;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbOpenHelper = new DbHelper(this);
		showView();
	}
	// 刷新ListView界面数据
	public void showView() {
		setContentView(R.layout.main);
		listview = (ListView) findViewById(android.R.id.list);// 使用系统默认的list																
		DiaryAdapter diaryAdapter = new DiaryAdapter(this);   // ID资源标识符
		setTitle(getResources().getString(R.string.app_name)
				+ getResources().getString(R.string.gongyou) + " "
				+ (int) diaryAdapter.getCount() + " "
				+ getResources().getText(R.string.pianriji));
		if ((int) diaryAdapter.getCount() == 0) {
			setTitle(getResources().getString(R.string.app_name)
					+ getResources().getString(R.string.no_diary));
			Toast.makeText(MainActivity.this, R.string.No_data, 1).show();
		} else {
			mData = diaryAdapter.getScrollData(0, (int) diaryAdapter.getCount());
			adapter = new MyAdapter(this);
			listview.setAdapter(adapter);
			registerForContextMenu(getListView());
		}
	}
	// 创建菜单选项
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, NEWDIARY, 0, R.string.menu_newDiary).setIcon(
				android.R.drawable.ic_menu_crop);
		menu.add(0, HELP, 0, R.string.menu_help).setIcon(
				android.R.drawable.ic_menu_help);
		menu.add(0, EXIT, 0, R.string.menu_exit).setIcon(
				android.R.drawable.ic_menu_close_clear_cancel);
		return super.onCreateOptionsMenu(menu);
	}
	// 菜单按钮点击事件处理
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;
		switch (item.getItemId()) {
		case NEWDIARY:
			intent = new Intent(MainActivity.this, NewDiaryActivity.class);
			startActivity(intent);
			break;
		case HELP:
			intent = new Intent(MainActivity.this, About.class);
			startActivity(intent);
			break;
		case EXIT:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	// 单击每个列表项产生的事件
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		intent.putExtra("id", mData.get(position).getId());
		intent.putExtra("title", mData.get(position).getTitle());
		intent.putExtra("body", mData.get(position).getBody());
		intent.putExtra("time", mData.get(position).getTime());
		intent.setClass(MainActivity.this, ShowSingleDiary.class);
		startActivity(intent);
	}
	// 产生类似于windows上的右键菜单,轻松处理列表中的项	
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle(R.string.option).add(0, EDIT, 0,
				R.string.contextmenu_enter);
		menu.add(0, DELETE, 0, R.string.contextmenu_delete);
		menu.add(0, DELETEALL, 0, R.string.delete_all);
		menu.add(0, CANCEL, 0, R.string.new_cancelButton);
	}
	// 为上下文菜单写事件
	public boolean onContextItemSelected(MenuItem item) {
		// 在上下文菜单中得到点击的item的position,然后可对item进行操作
		list_position = ((AdapterContextMenuInfo) item.getMenuInfo()).position;// 列表位置从0开始
		Intent intent = new Intent();
		switch (item.getItemId()) {
		case EDIT:
			intent.putExtra("id", mData.get(list_position).getId());
			intent.putExtra("title", mData.get(list_position).getTitle());
			intent.putExtra("body", mData.get(list_position).getBody());
			intent.setClass(MainActivity.this, UpdateDiaryActivity.class);
			startActivity(intent);
			break;
		case DELETE:
			showOptional();
			break;
		case DELETEALL:
			showOptional2();
		case CANCEL:
		}
		return super.onContextItemSelected(item);
	}
	// 返回主页面时调用的函数
	protected void onRestart() {
		super.onRestart();
		showView();
	}
	public void showOptional() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.AlertDialog_title)
				.setMessage(getResources().getString(R.string.optional_msg))
				.setPositiveButton(
						getResources().getString(R.string.optional_left),
						new OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								DiaryAdapter adapter = new DiaryAdapter(
										MainActivity.this);
								adapter.delete(mData.get(list_position).getId()); // 注意索引ID
								showView();
							}
						})
				.setNegativeButton(
						getResources().getString(R.string.optional_right),
						new OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								showView();
							}
						}).show();
	}
	public void showOptional2() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.AlertDialog_title)
				.setMessage(getResources().getString(R.string.optional_msg))
				.setPositiveButton(
						getResources().getString(R.string.optional_left),
						new OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								SQLiteDatabase db = dbOpenHelper
										.getWritableDatabase();
								db.delete("diary", null, null);
								db.close();
								Toast.makeText(MainActivity.this,
										R.string.Toast_deleteAll, 0).show();
								showView();
							}
						})
				.setNegativeButton(
						getResources().getString(R.string.optional_right),
						new OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								showView();
							}
						}).show();
	}
	class MyAdapter extends BaseAdapter {
		ViewHolder holder;
		private LayoutInflater mInflater;
		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}
		public int getCount() {
			return mData.size();
		}
		public Object getItem(int arg0) {
			return null;
		}
		public long getItemId(int position) {
			return 0;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.diary_item, null);
				holder.id = (TextView) convertView.findViewById(R.id.item_id);
				holder.title = (TextView) convertView.findViewById(R.id.item_title);
				holder.body = (TextView) convertView.findViewById(R.id.item_preRead);
				holder.time = (TextView) convertView.findViewById(R.id.item_time);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.id.setText(mData.get(position).getId() + "");
			holder.title.setText(mData.get(position).getTitle());
			holder.body.setText(mData.get(position).getBody());
			holder.time.setText(mData.get(position).getTime());
			return convertView;
		}
	}
}


package com.jk;

import java.sql.Date;

import com.jk.R;
import com.jk.service.DiaryAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NewDiaryActivity extends Activity {
private Button button_save;
private Button button_exit;
private EditText text_title;
private EditText text_body;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newdiary);
		
		 findView();
		 setListener();
	}

	public void findView() {
		setTitle(R.string.createNew);
		button_save=(Button) findViewById(R.id.new_save);
		button_exit=(Button) findViewById(R.id.new_cancel);
		text_title = (EditText)findViewById(R.id.text_title);
		text_body = (EditText)findViewById(R.id.text_body);
		
		text_title.setHint(R.string.title_hint);
		text_body.setHint(R.string.body_hint);
	}
	
	public void setListener() {
		button_save.setOnClickListener(new View.OnClickListener(){
          //保存按钮点击事件
			@Override
			public void onClick(View v) {
				 DiaryAdapter adapter = new DiaryAdapter(NewDiaryActivity.this);
				 Diary diary = new Diary(text_title.getText().toString(), 
						text_body.getText().toString(),new Date(System.currentTimeMillis()).toString());
				 adapter.save(diary);
				 //提交后将输入框清空
				 text_title.setText("");
				 text_body.setText("");
				 button_exit.setText(R.string.resume);
				 text_body.clearFocus();//让焦点回到title上
				Toast.makeText(NewDiaryActivity.this, R.string.Toast_save, 0).show();
			}});
		
		button_exit.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
			finish();
			}});
	}
	
}


package com.jk;

import com.jk.R;
import com.jk.R.id;
import com.jk.R.layout;
import com.jk.R.string;
import com.jk.service.DiaryAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
/**
 * 这个版本的menu菜单是好的,可通过这个来修复新版本的menu菜单
 * @author Administrator
 *
 */
public  class ShowSingleDiary extends Activity {
	private TextView view_id;
	private TextView view_title;
	private TextView view_body;
	private TextView view_time;
	int id;
	private static final int MODIFY = 0;
	private static final int NEW = 1;
	private static final int DELETE = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去掉Title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		showView();
	}

	public void showView() {
		Intent intent = getIntent();
		id = intent.getIntExtra("id", 0);
		String title = intent.getStringExtra("title");
		String body = intent.getStringExtra("body");
		String time = intent.getStringExtra("time");
		
		setContentView(R.layout.show_single_diary);
		view_id = (TextView)findViewById(R.id.simple_text_id);
		view_title = (TextView) findViewById(R.id.simple_text_title);
		view_body = (TextView) findViewById(R.id.simple_text_body);
		view_time= (TextView) findViewById(R.id.simple_text_time);
		
		view_id.setText(id+"");
		view_title.setText(title);
		view_body.setText(body);
		view_time.setText(time);
		
		view_id.setVisibility(TextView.INVISIBLE);
		
	}
	
	// 创建菜单选项
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MODIFY, 0, R.string.contextmenu_enter).setIcon(
				android.R.drawable.ic_menu_crop);
		menu.add(0, NEW, 0, R.string.menu_newDiary).setIcon(
				android.R.drawable.btn_star_big_on);
		menu.add(0, DELETE, 0, R.string.contextmenu_delete).setIcon(
				android.R.drawable.ic_menu_help);
		return super.onCreateOptionsMenu(menu);
	}

	// 菜单按钮点击事件处理
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {
		case MODIFY:
			intent.putExtra("id",id);
			intent.putExtra("title",view_title.getText());
			intent.putExtra("body",view_body.getText());
			intent.setClass(ShowSingleDiary.this, UpdateDiaryActivity.class);					
			startActivity(intent);
			break;
		case NEW:
			intent.setClass(ShowSingleDiary.this, NewDiaryActivity.class);					
			startActivity(intent);
			break;
		case DELETE:
			showOptional();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void showOptional() {
		new AlertDialog.Builder(this)
		.setTitle(R.string.AlertDialog_title)
		.setMessage(getResources().getString(R.string.optional_msg))
		.setPositiveButton(getResources().getString(R.string.optional_left),new OnClickListener() {
					public void onClick(DialogInterface dialog,int which) {
						DiaryAdapter adapter = new DiaryAdapter(ShowSingleDiary.this);
						adapter.delete(id); // 注意索引ID
						finish();
					}
				})
		.setNegativeButton(getResources().getString(R.string.optional_right),new OnClickListener() {
					public void onClick(DialogInterface dialog,int which) {
						showView();
					}
				}).show();
	}	
}

package com.jk;

import java.sql.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jk.R;
import com.jk.R.id;
import com.jk.R.layout;
import com.jk.R.string;
import com.jk.service.DiaryAdapter;

public class UpdateDiaryActivity extends Activity {

	private EditText text_title;
	private EditText text_body;
	private Button button_save;
	private Button button_exit;
	private int id;
	private String title;
	private String body;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newdiary);
		showView();
		setListener();
	}

	public void showView() {
		Intent intent = getIntent();
	    id = intent.getIntExtra("id", 0);
		title = intent.getStringExtra("title");
		body = intent.getStringExtra("body");

		setTitle(R.string.title_update);
		text_title = (EditText) findViewById(R.id.text_title);
		text_body = (EditText) findViewById(R.id.text_body);
		button_save=(Button) findViewById(R.id.new_save);
		button_exit=(Button) findViewById(R.id.new_cancel);
		text_title.setText(title);
		text_body.setText(body);

	}

	public void setListener() {
		button_save.setOnClickListener(new View.OnClickListener() {
			// 保存按钮点击事件
			@Override
			public void onClick(View v) {
				DiaryAdapter adapter = new DiaryAdapter(
						UpdateDiaryActivity.this);
				Diary diary = new Diary(text_title.getText().toString(),
						text_body.getText().toString(), new Date(System
								.currentTimeMillis()).toString());
				diary.setId(id);
				adapter.update(diary);
				Toast.makeText(UpdateDiaryActivity.this, R.string.Toast_Update, 1)
						.show();
				finish();
			}
		});

		button_exit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
}

package com.jk;

import android.widget.TextView;

public class ViewHolder {
	public TextView id;
	public TextView title;  
    public TextView body;
    public TextView time;  

}

package com.jk;

import com.jk.R;
import com.jk.R.id;
import com.jk.R.layout;
import com.jk.R.string;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class About extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTheme(android.R.style.Theme_Dialog);
		setContentView(R.layout.about);
		setTitle(R.string.about_window_title);
		TextView textView = (TextView) findViewById(R.id.about_title),
		textView2 = (TextView)findViewById(R.id.about_msg);
		textView.setText(R.string.about_title);
		textView2.setText("   " + getResources().getString(R.string.about_msg));
	}
}

package com.jk;

public class Diary {
	
	private int diaryId;
	private String title;
	private String body;
	private String time;
	
	public Diary(int diaryId, String title, String body, String time) {
		this.diaryId = diaryId;
		this.title = title;
		this.body = body;
		this.time = time;
	}

	public Diary( String title, String body, String time) {
		this(0, title, body ,time);
	}
	public int getId() {
		return diaryId;
	}

	public void setId(int diaryId) {
		this.diaryId = diaryId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}
	public String toString() {
		return "Diary [diaryId=" + diaryId + ", title=" + title + ", body="
				+ body + ", time=" + time + "]";
	}
	
}


Android记事本开发,布布扣,bubuko.com

Android记事本开发

上一篇:nagios系列-----Redhat5.4下安装EPEL


下一篇:微信小程序记账本进度一