Android Studio——记事本案例

这篇具有很好参考价值的文章主要介绍了Android Studio——记事本案例。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、布局界面

        1、记事本界面布局

main_notepad.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fefefe">

    <TextView
        android:id="@+id/note_name"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@android:color/darker_gray"
        android:text="记事本"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:textSize="20sp" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:divider="#E4E4E4E4"
        android:dividerHeight="1dp"
        android:fadingEdge="horizontal"
        android:listSelector="#00000000"
        android:scrollbars="none"
        android:layout_below="@+id/note_name">

    </ListView>

    <ImageView
        android:id="@+id/add"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/add"
        android:layout_marginBottom="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

        2、记事本Item布局界面

activity_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="12dp">
    <TextView
        android:id="@+id/ietm_content"
        android:textColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:ellipsize="end"
        android:lineSpacingExtra="3dp"
        android:paddingTop="10dp"/>

    <TextView
        android:id="@+id/item_time"
        android:textColor="#fb7a6a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:paddingBottom="7dp"/>

</LinearLayout>

        3、添加、修改界面布局

activity_record.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fefefe">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#E4E4E4"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/note_back"
            android:layout_width="45dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:background="@drawable/back"/>
        <TextView
            android:id="@+id/note_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="记事本"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:textStyle="bold" />
    </RelativeLayout>
    <TextView
        android:id="@+id/tv_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center"
        android:visibility="gone"
        android:textColor="#fb7a6a"/>
    <EditText
        android:id="@+id/note_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="请输入要添加的内容"
        android:paddingLeft="5dp"
        android:textColor="@android:color/black"
        android:background="#fefefe"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#fb7a6a"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>
        <ImageView
            android:id="@+id/delete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/delete"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>

        <ImageView
            android:id="@+id/note_save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/save"
            android:paddingTop="9dp"
            android:paddingBottom="15dp" />
        <ImageView
            android:id="@+id/none2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>

    </LinearLayout>
</LinearLayout>

二、封装记录信息实体类

        记事本的每个记录都会有记录内容和记录时间这两个属性,因此需要建立一个实体类用于存放这些属性。

NotepadBean.java

public class NotepadBean {
    private String id;
    private String notepadContent;
    private String notepadTime;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNotepadContent() {
        return notepadContent;
    }

    public void setNotepadContent(String notepadContent) {
        this.notepadContent = notepadContent;
    }

    public String getNotepadTime() {
        return notepadTime;
    }

    public void setNotepadTime(String notepadTime) {
        this.notepadTime = notepadTime;
    }
}

三、编写记事本界面列表适配器

        因为记事本的纪录列表是使用ListView控件展示的,因此需要建立一个数据适配器对ListView控件进行数据适配。

NotepadAdapter.java

public class NotepadAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private List<NotepadBean> list;
    public NotepadAdapter(Context context, List<NotepadBean> list){        
        this.layoutInflater=LayoutInflater.from(context);
        this.list=list;
    }
    @Override
    //获取Item条目的总数
    public int getCount(){
        return list==null? 0: list.size();
    }
    @Override
    //根据position(位置)获取某个Item的对象
    public Object getItem(int position){
        return list.get(position);
    }
    @Override
    //根据position(位置)获取某个Item的id
    public long getItemId(int position){
        return position;
    }
    @Override
    //获取相应position对应的Item视图,position是当前Item的位置,convertView用于复用旧视图,parent用于加载xml布局
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder viewHolder;
        //通过inflate()方法加载Item布局,并将获取的数据显示到对应的控件上,并判断旧视图是否为空,若为空,则创建一个ViewHolder对象
        //通过set.Tag()方法将该对象添加到convertView中进行缓存,否则把获取的旧视图进行缓存
        if(convertView==null){
            convertView=layoutInflater.inflate(R.layout.activity_item,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        NotepadBean noteInfo=(NotepadBean) getItem(position);
        viewHolder.tvNoteoadContent.setText(noteInfo.getNotepadContent());
        viewHolder.tvNotepadTime.setText(noteInfo.getNotepadTime());
        return convertView;
    }
    class ViewHolder{
        TextView tvNoteoadContent;
        TextView tvNotepadTime;
        public ViewHolder(View view){
            tvNoteoadContent=(TextView) view.findViewById(R.id.ietm_content);
            tvNotepadTime=(TextView) view.findViewById((R.id.item_time));
        }
    }
}

四、创建数据库

        因为记事本的存储和读取记录的数据都是通过数据库完成的,因此需要创建数据库

        1、先创建一个DBUtils类,在该类中定义数据库的名称、表名、数据库版本、数据库表中的列名以及获取当前日期等信息。

DBUtils.java

public class DBUtils {
    public static final String DATABASE_NAME="Notepad";
    public static final String DATABASE_TABLE="Note";
    public static final int DATABASE_VERSION=1;
    //数据库表中的列名
    public static final String NOTEPAD_ID="id";
    public static  final String NOTEPAD_CONTENT="content";
    public static final String NOTEPAD_TIME="notetime";
    //获取当前日期
    public static final String getTime(){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日HH:mm;ss");
        Date date=new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
}

        2、创建SQLiteHelper类,在该类中实现增删改查操作

SQLiteHelper.java

public class SQLiteHelper extends SQLiteOpenHelper {
    private SQLiteDatabase sqLiteDatabase;

    //创建数据库
    public SQLiteHelper(Context context){
        super(context, DBUtils.DATABASE_NAME,null,DBUtils.DATABASE_VERSION);
        sqLiteDatabase=this.getWritableDatabase();
    }
    //创建表
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table "+DBUtils.DATABASE_TABLE+"("+DBUtils.NOTEPAD_ID+" integer primary key autoincrement,"+DBUtils.NOTEPAD_CONTENT+" text,"+DBUtils.NOTEPAD_TIME+ " text)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){}
    //添加数据
    public boolean insertData(String userContent,String userTime){
        ContentValues contentValues=new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT,userContent);
        contentValues.put(DBUtils.NOTEPAD_TIME,userTime);
        return sqLiteDatabase.insert(DBUtils.DATABASE_TABLE,null,contentValues)>0;
    }
    //删除数据
    public boolean deleteData(String id){
        String sql=DBUtils.NOTEPAD_ID+"=?";
        String [] contentValuesArray=new String[]{String.valueOf(id)};
        return sqLiteDatabase.delete(DBUtils.DATABASE_TABLE,sql,contentValuesArray)>0;
    }
    //修改数据
    public boolean updateData(String id,String content,String userYear){
        ContentValues contentValues=new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT,content);
        contentValues.put(DBUtils.NOTEPAD_TIME,userYear);
        String sql=DBUtils.NOTEPAD_ID+"=?";
        String [] strings=new String[] {id};
        return sqLiteDatabase.update(DBUtils.DATABASE_TABLE,contentValues,sql,strings)>0;
    }
    //查询数据
    public List<NotepadBean> query(){
        List<NotepadBean> list =new ArrayList<NotepadBean>();
        Cursor cursor=sqLiteDatabase.query(DBUtils.DATABASE_TABLE,null,null,null,null,null,DBUtils.NOTEPAD_ID+" desc");
        if(cursor!=null){
            while(cursor.moveToNext()){
                NotepadBean noteInfo=new NotepadBean();
                @SuppressLint("range")
                String id=String.valueOf(cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID)));
                @SuppressLint("range")
                String content=cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT));
                @SuppressLint("range")
                String time=cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_TIME));
                noteInfo.setId(id);
                noteInfo.setNotepadContent(content);
                noteInfo.setNotepadTime(time);
                list.add(noteInfo);
            }
            cursor.close();
        }
        return list;
    }
}

五、实现添加记录界面功能

        因为添加记录界面有保存和删除按钮,因此创建记录类实现点击事件,将编写的内容添加到数据库中。

RecordActivity.java

public class RecordActivity extends AppCompatActivity implements View.OnClickListener{
    ImageView delete,note_save,note_back;
    TextView noteName,note_time;
    EditText content;
    SQLiteHelper mSQLiteHelper;
    String id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        //通过findViewById获取界面控件
        note_back=(ImageView) findViewById(R.id.note_back);
        note_save=(ImageView) findViewById(R.id.note_save);
        delete=(ImageView) findViewById(R.id.delete);
        noteName=(TextView) findViewById(R.id.note_name);
        note_time=(TextView) findViewById(R.id.tv_time);
        content=(EditText) findViewById(R.id.note_content);
        //设置启动器
        note_back.setOnClickListener(this);
        delete.setOnClickListener(this);
        note_save.setOnClickListener(this);
        //初始化
        initData();
    }
    protected void initData(){
        //创建数据库
        mSQLiteHelper=new SQLiteHelper(this);
        noteName.setText("添加记录");
        //接收记事本传来的消息
        //getIntent()方法获取Intent对象
        Intent intent=getIntent();
        if(intent!=null){
            //获取传递的记录id
            id=intent.getStringExtra("id");
            if(id!=null){
                noteName.setText("修改记录");
                content.setText(intent.getStringExtra("content"));
                note_time.setText(intent.getStringExtra("time"));
                note_time.setVisibility(View.VISIBLE);
            }
        }
    }
    public void onClick(View v){
        switch (v.getId()){
            //后退,删除,保存事件
            case R.id.note_back:
                finish();
                break;
            case R.id.delete:
                content.setText("");
                break;
            case R.id.note_save:
                //通过getText()获取输入内容
                String noteContent=content.getText().toString().trim();
                if(id!=null){
                    if(noteContent.length()>0){
                        if(mSQLiteHelper.updateData(id,noteContent,DBUtils.getTime())){
                            showToast("修改成功");
                            setResult(2);
                            finish();
                        }
                        else{
                            showToast("保存失败");
                        }
                    }
                    else{
                        showToast("保存内容不能为空");
                    }
                }
             //向数据库添加数据
                else{
                    if(noteContent.length()>0){
                        if(mSQLiteHelper.insertData(noteContent,DBUtils.getTime())){
                            showToast("保存成功");
                            setResult(2);
                            finish();
                        }
                        else{
                            showToast("保存失败");
                        }
                    }
                    else{
                        showToast("修改内容不能为空");
                    }
                }
                break;
            }
    }
    public void showToast(String Message){
        Toast.makeText(RecordActivity.this,Message,Toast.LENGTH_SHORT).show();
    }
}

六、实现记事本界面的显示功能

        等于就是刚进入程序的界面的选择与操作

MainActivity.java

public class MainActivity extends Activity {
    ListView listView;
    List<NotepadBean> list;
    SQLiteHelper mSQLiteHelper;
    NotepadAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad);
        //用于显示记录的列表
        listView=(ListView) findViewById(R.id.listview);
        ImageView add=(ImageView) findViewById(R.id.add);
        //设置添加按钮事件
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使用显示Intent指定要跳转的目标Activity,并通过startActivityForResult()方法开启目标Activity
                Intent intent=new Intent(MainActivity.this,RecordActivity.class);
                startActivityForResult(intent,1);
            }
        });
        initData();
    }
    protected void initData() {
        //创建数据库
        mSQLiteHelper = new SQLiteHelper(this);
        showQueryData();
        //处理查看记事本详细信息
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //通过get方法获取对应的Item数据
                NotepadBean notepadBean=list.get(position);
                //通过putExtra()方法封装到Intent对象中
                Intent intent=new Intent(MainActivity.this,RecordActivity.class);
                intent.putExtra("id",notepadBean.getId());
                intent.putExtra("time",notepadBean.getNotepadTime());
                intent.putExtra("content",notepadBean.getNotepadContent());
                MainActivity.this.startActivityForResult(intent,1);
            }
        });
        //删除记事本记录
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog dialog;
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this)
                        .setMessage("是否删除此纪录?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //获取Item对象
                                NotepadBean notepadBean=list.get(position);
                                if(mSQLiteHelper.deleteData(notepadBean.getId())){
                                    //删除对应的Item
                                    list.remove(position);
                                    //更新记事本界面
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                dialog=builder.create();
                dialog.show();
                return true;
            }
        });
    }
    private void showQueryData(){
        if(list!=null){
            list.clear();
        }
        //从数据库中查询数据
        list=mSQLiteHelper.query();
        adapter=new NotepadAdapter(this,list);
        listView.setAdapter(adapter);
    }
    @Override
    //重写,当关闭添加记录界面时,程序回调该方法
    // 并在该方法中调用showQueryData()方法重新获取数据库中保存的记录数据并显示到记录列表中
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==1&&resultCode==2){
            showQueryData();
        }
    }
}

七、运行结果截图

        主界面

Android Studio——记事本案例

         选择添加按钮之后进入编写界面

Android Studio——记事本案例Android Studio——记事本案例

         修改界面

Android Studio——记事本案例Android Studio——记事本案例文章来源地址https://www.toymoban.com/news/detail-448637.html

到了这里,关于Android Studio——记事本案例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android记事本

    1、项目需求分析 1.1、记事功能需求分析: 1.1.1、显示记事 用户打开记事本可以看到之前所写的所有记事内容,进入主页后,软件应该从数据库中搜索出该用户所写的全部记事,并将所有的数据进行显示。 1.1.2、添加记事 设置添加按钮,点击添加按钮之后可以编辑记事的标题

    2024年02月03日
    浏览(44)
  • Android开发_记事本(1)

    TextView中有下述几个属性: id: 为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id! layout_width: 组件的宽度,一般写: wrap_content 或者 match_parent(fill_parent) ,前

    2023年04月10日
    浏览(56)
  • Android——记事本功能业务(完整代码)

    目录 实现效果 一、搭建记事本页面布局activity_notepad.xml 二、搭建记事本界面Item布局notepad_item_layout.xml 三、封装记录信息实体类NotepadBean类 四、编写记事本界面列表适配器NotepadAdapter类 五、创建数据库 六、实现记事本界面的显示功能NotepadAdapter.java  七、搭建添加记录界面和

    2024年02月03日
    浏览(57)
  • 基于Android的记事本设计和模块开发

    有一万五千字论文,完美运行。 由于编程技术的迅速发展,各种记事本APP随处可见,在人们的日常生活中经常使用的到。于是各种记事本APP也跟着发展起来。本文在通过在Android Studio开发平台上开发一个简单的多功能语音输入记事本APP的过程,同时了解记事本APP的功能实现,

    2024年02月03日
    浏览(46)
  • Android 备忘录,记事本程序设计

    android备忘录实现,使用ObjectBox数据库框架进行数据存储,增删改查等操作。代码使用kotlin编写。 1、下面看看ObjectBox数据库封装 需要注意的是:    /**      * 你只有配置好之后, 点击 Make Model \\\'你的model名字\\\', 才会创建 MyObjectBox对象      * 对于MyObjectBox的包名, 目前我发现的

    2024年01月23日
    浏览(38)
  • 【Android 记事本,笔记本,可注册登录,增删改查(附源码)】

    简介 用Sqlite做数据库,用来存储账号以及笔记信息,实现了账号的注册以及登录功能,笔记方面实现了新增、删除、修改、搜索功能,列表展示笔记使用的是listView(懒得弄分割线,就使用listView的默认分割线了); 运行效果 代码讲解 我代码里使用了两个依赖,一个是工具

    2024年02月04日
    浏览(39)
  • 基于安卓系统(android)记事本APP管理系统设计与实现

    目录 摘要 I Abstract II 1 绪论 1.1 课题来源、目的和意义 1 1.2 国内外基本研究情况 1 2 需求分析 2.1 用户需求 4 2.2 功能需求 4 2.3 数据库选择 6 2.4 性能需求 6 3 概要设计 3.1 功能概要设计 7 3.2 数据库概要设计 13 4 详细设计 4.1 功能设计 15 4.2 数据库设计 30 5 系统功能实现 5.1 系统架

    2024年02月11日
    浏览(32)
  • Android:实现安卓小程序-记事本(备忘录)的开发,教你如何增加拿到BAT大厂offer几率

    public MyBaseAdapter(Context context, List data) { this.context = context; this.data = data; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public int getCount() { if(data==null) return 0; return data.size(); } } 接着在编写NoteAdapter类继承上类,初始化view, 将Note条目的

    2024年04月25日
    浏览(41)
  • 电脑记事本在哪里?电脑桌面显示记事本要怎么设置?

    绝大多数上班族在使用电脑办公时,都需要随手记录一些琐碎或重要的事情,例如工作注意事项、常用的文案、某项工作的具体要求、多个平台的账号和密码等。于是就有不少小伙伴想要使用电脑记事本软件来记录,那么电脑记事本在哪里呢?想要电脑桌面显示记事本怎么设

    2024年02月15日
    浏览(33)
  • WIN11右键打开方式没有记事本,记事本无法使用问题

    背景: 个人手欠把记事本卸载了,然后又安装了,结果记事本可以打开,但是应用里面没有,右击打开方式也没有!!(检索过程发现很多人人用不了是因为升级W11) 问题: 1.如题,右击打开方式里没有记事本选项! 2.右击更多打开方式,通过查找到notepad.exe打开,出现程序

    2024年02月04日
    浏览(95)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包