[英]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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.