请登录后探索更多精彩内容!
做完了主界面之后开始做添加和修改笔记功能,当用户点击主界面的添加之后跳转到另一个编辑界面,点击添加将数据保存到数据库。当用户选择某一条笔记然后点击修改之后跳转到另一个界面,把数据库里面的数据取出来放到对应的编辑框。
添加笔记功能的实现代码
package com.example.notebook; import java.sql.Date; import java.text.SimpleDateFormat; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.text.format.Time; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /*----------------------------------添加功能------------------------------------*/ public class AddnoteActivity extends Activity { private EditText titleEditText,eventEditText; private Button addeventsButton; //添加功能代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addnote); //绑定组件 titleEditText=(EditText)findViewById(R.id.addnote_title); eventEditText=(EditText)findViewById(R.id.addnote_edtnote); addeventsButton=(Button)findViewById(R.id.addnote_btnadd); //监听“添加”按钮事件 addeventsButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 调用数据库 MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(AddnoteActivity.this); //将编辑框里面的数据赋值 String eventsString=eventEditText.getText().toString(); String titleString=titleEditText.getText().toString(); //获取时间 Time t=new Time(); //或者Time t=new Time("GTM+8"); t.setToNow(); // 取得系统时间。 int year = t.year; int month = t.month+1; int day = t.monthDay; int hour = t.hour+8; //相差8小时+8 int minute = t.minute; int second = t.second; String dateString=year+"年"+month+"月"+day+"日 "+hour+":"+minute+":"+second; //判断输入信息 if (eventsString.equals("")) { //当输入框为空时 Toast.makeText(AddnoteActivity.this, "请输入事件!", Toast.LENGTH_LONG).show(); } else { //当输入框有内容时 Toast.makeText(AddnoteActivity.this, "添加成功!", Toast.LENGTH_LONG).show(); mydatabaseHelper.eventsinsert(titleString,eventsString, dateString); //添加成功后跳转到显示界面 Intent intent = new Intent(AddnoteActivity.this, NoteActivity.class); startActivity(intent); } } }); } }
修改笔记功能的实现代码
package com.example.notebook; import android.content.Intent; import android.os.Bundle; import android.text.format.Time; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /*----------------------------------修改功能------------------------------------*/ public class EditActivity extends MainActivity { private EditText titleEditText,eventEditText; private Button saveButton; private String idString=""; //修改功能主代码 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit); //绑定组件 titleEditText=(EditText)findViewById(R.id.edit_title); eventEditText=(EditText)findViewById(R.id.edit_change); saveButton=(Button)findViewById(R.id.edit_save); //获取从显示界面传过来的intent数据,调用getintent方法 Intent intent=getIntent(); //从intent对象取出数据 Bundle bundle=intent.getExtras(); Notelist notelist=(Notelist)bundle.getSerializable("note"); idString=String.valueOf(notelist.getId()); eventEditText.setText(notelist.getEventString()); titleEditText.setText(notelist.getTitleString()); //保存按钮点击事件 saveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(EditActivity.this); String titleString=titleEditText.getText().toString(); String eventsString=eventEditText.getText().toString(); //获取时间 Time t=new Time(); //或者Time t=new Time("GTM+8"); t.setToNow(); // 取得系统时间。 int year = t.year; int month = t.month+1; int day = t.monthDay; int hour = t.hour+8; //相差8小时+8 int minute = t.minute; int second = t.second; String dateString=year+"年"+month+"月"+day+"日 "+hour+":"+minute+":"+second; //判断修改内容 if (eventEditText.getText().toString().equals("")) { //修改内容为空时 Toast.makeText(EditActivity.this, "修改内容不能为空!", Toast.LENGTH_LONG).show(); } else { //修改内容不为空时 Toast.makeText(EditActivity.this, "添加成功", Toast.LENGTH_LONG).show(); mydatabaseHelper.eventsupdate(idString,titleString,eventsString, dateString); //修改成功跳转到显示界面 Intent intent=new Intent(EditActivity.this,NoteActivity.class); startActivity(intent); } } }); } }
暂无评论
请先登录后发表评论!
暂无评论