繁体   English   中英

如何在 android 工作室的应用程序(已创建)中制作笔记部分

[英]how to make notes section in an app (already created) in android studio

这可能很难解释,但我正在 android 工作室中使用 java 开展项目。 我不确定如何开始,是否使用 SQL 等等。我实际上只是想要一个能够键入并可能存储其中写入的“注释”的空白“页面”? 我已经尝试过教程,但是当我只想添加笔记部分时,他们正在制作一个整体的笔记应用程序? 我提出了一个非常吸引人的想法,我的代码低于我从教程中尝试过的代码! 任何帮助表示赞赏!

在这里画白板哈哈

<EditText
 android:layout_width="391dp"
 android:layout_height="523dp"
 android:hint="Type here..."
 android:gravity="top"
 android:id="@+id/EditText1"
 app:layout_constraintTop_toTopOf="parent"/>
//SAVING FILES BELOW
CS
public void Save(String fileName) {
    try {
        OutputStreamWriter out =
            new OutputStreamWriter(openFileOutput(fileName, 0));
        out.write(EditText1.);
        out.close();
        Toast.makeText(this, "Note Saved!", Toast.LENGTH_SHORT).show();
    } catch (Throwable t) {
        Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
    }
}

//LOADING FILES CODE BELOW
public boolean FileExists(String fname){
  File file = getBaseContext().getFileStreamPath(fname);
  return file.exists();
}

//MAIN CODE BELOW
//YOU CAN CHANGE - COULDN'T MAKE JUST ONE SECTION
public class MainActivity extends AppCompatActivity {
    EditText EditText1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Save("Note1.txt");
            }
        });

        EditText1 = (EditText) findViewById(R.id.EditText1);
        EditText1.setText(Open("Note1.txt"));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void Save(String fileName) {
        try {
            OutputStreamWriter out =
                new OutputStreamWriter(openFileOutput(fileName, 0));
            out.write(EditText1.getText().toString());
            out.close();
            Toast.makeText(this, "Note saved!", Toast.LENGTH_SHORT).show();
        } catch (Throwable t) {
            Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
        }
    }

    public String Open(String fileName) {
        String content = "";
        if (FileExists(fileName)) {
            try {
                InputStream in = openFileInput(fileName);
                if ( in != null) {
                    InputStreamReader tmp = new InputStreamReader( in );
                    BufferedReader reader = new BufferedReader(tmp);
                    String str;
                    StringBuilder buf = new StringBuilder();
                    while ((str = reader.readLine()) != null) {
                        buf.append(str + "\n");
                    } in .close();
                    content = buf.toString();
                }
            } catch (java.io.FileNotFoundException e) {} catch (Throwable t) {
                Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
            }
        }
        return content;
    }

    public boolean FileExists(String fname) {
        File file = getBaseContext().getFileStreamPath(fname);
        return file.exists();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

根据您要存储的数据类型,您可能需要使用其他方式。 本文详细概述了您拥有的所有数据和文件存储选项。

我猜你想存储可能的大数据,所以我认为在你的情况下,最合适和最佳的选择是使用Room本地持久性库。

由于您提到是否使用 SQL,如上述文档中所述,Room 提供以下好处

  • SQL 查询的编译时验证。
  • 方便的注释,最大限度地减少重复和容易出错的样板代码。
  • 简化的数据库迁移路径。

因此,建议您使用 Room 库,而不是直接使用 SQLite API。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM