Android实现风蓝记事本(4):增加记事功能
package com.example.notes; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.app.Dialog; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class Add extends Activity{ private LinearLayout layout; //布局容器 private EditText add_content; //内容框 private EditText add_title; //标题框 private ImageButton backBtn; //返回 private ImageButton clearBtn; //清空 private ImageButton saveBtn; //保存 private DatabaseManage dm = null;// 数据库管理对象 private Cursor cursor = null; private int background;//背景 private SharedPreferences sp;//存储数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add); layout=(LinearLayout)findViewById(R.id.add); add_content=(EditText)findViewById(R.id.add_content); add_title=(EditText)findViewById(R.id.add_title); dm = new DatabaseManage(this); background=R.drawable.skin_first; getBackground(); //拿到主界面地方传过来的数据,并显示出来,显示say感悟和帮助信息 if(getIntent().hasExtra("title")) { Bundle data=getIntent().getExtras(); if(data.containsKey("title")) add_title.setText(data.getString("title")); if(data.containsKey("content")) add_content.setText(data.getString("content")); } saveBtn=(ImageButton)findViewById(R.id.save_btn); backBtn=(ImageButton)findViewById(R.id.back_btn); clearBtn=(ImageButton)findViewById(R.id.clear_btn); ImageButton[] btns={backBtn,clearBtn,saveBtn}; for(ImageButton btn:btns) btn.setOnClickListener(click); } //设置背景 private void getBackground() { sp = getSharedPreferences("backgrounds", 0); background = sp.getInt("background", background); layout.setBackgroundResource(background); } private OnClickListener click=new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.back_btn: back(); break; case R.id.clear_btn: clear(); break; case R.id.save_btn: save(); default: break; } } //清空内容 private void clear() { // TODO Auto-generated method stub View deleteView=View.inflate(Add.this, R.layout.deletenote, null); final Dialog clearDialog=new Dialog(Add.this,R.style.dialog); clearDialog.setContentView(deleteView); Button yesBtn=(Button)deleteView.findViewById(R.id.delete_yes); Button noBtn=(Button)deleteView.findViewById(R.id.delete_no); yesBtn.setText(R.string.clear_note); noBtn.setText(R.string.clear_cancel); yesBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub add_title.setText(""); add_content.setText(""); clearDialog.dismiss(); } }); noBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub clearDialog.dismiss(); } }); clearDialog.show(); } //返回主界面 private void back() { // TODO Auto-generated method stub Intent intent=new Intent(Add.this,MainActivity.class); startActivity(intent); finish(); } //增加记事记录 private void save() { // TODO Auto-generated method stub String n_title=add_title.getText().toString().trim(); if(n_title.length()==0) n_title="无标题"; String n_content=add_content.getText().toString().trim(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String n_time=df.format(new Date()); if(n_content.trim().length()>0){ dm.open(); long flag=dm.insert(n_title, n_content, n_time); Toast.makeText(Add.this, R.string.note_saved, Toast.LENGTH_SHORT).show(); Intent intent=new Intent(Add.this,MainActivity.class); startActivity(intent); finish(); } else Toast.makeText(Add.this, R.string.note_null, Toast.LENGTH_SHORT).show(); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { return true; } }
用到的布局:setContentView(R.layout.add)--》add.xml
<LinearLayout 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" android:id="@+id/add" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:background="@drawable/skin_first" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:id="@+id/back_btn" android:contentDescription="@string/back_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/transparent" android:src="@drawable/back" android:padding="8dp" /> <TextView android:id="@+id/title_add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="@string/new_note" android:textSize="20sp" android:textColor="@color/blue" android:padding="8dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="1dp" > <EditText android:id="@+id/add_title" android:inputType="text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:singleLine="true" android:padding="8dp" android:textSize="18sp" android:hint="@string/title_hint" android:textColorHint="@color/darkgray" /> </LinearLayout> <EditText android:id="@+id/add_content" android:inputType="textMultiLine" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="left|top" android:padding="8dp" android:textSize="18sp" android:hint="@string/content_hint" android:textColorHint="@color/darkgray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <ImageButton android:contentDescription="@string/clear_btn" android:id="@+id/clear_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/gone" android:padding="8dp" android:background="@color/transparent" /> <ImageButton android:contentDescription="@string/save_btn" android:id="@+id/save_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:src="@drawable/save" android:padding="8dp" android:background="@color/transparent" /> </LinearLayout> </LinearLayout>