Android studio用java写一个日记本app
时间: 2023-10-07 16:05:02 浏览: 217
基于Android Studio开发的安卓的记事本app
5星 · 资源好评率100%
好的,下面是一个简单的日记本App的实现代码:
1. 首先,在Android Studio中创建一个新项目并在布局文件中添加以下控件:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTextTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"/>
<EditText
android:id="@+id/editTextContent"
android:layout_below="@id/editTextTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Content"/>
<Button
android:id="@+id/buttonSave"
android:layout_below="@id/editTextContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"/>
<Button
android:id="@+id/buttonView"
android:layout_below="@id/editTextContent"
android:layout_toRightOf="@id/buttonSave"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View"/>
</RelativeLayout>
```
2. 在MainActivity类中,我们将在保存按钮和查看按钮上设置点击事件。在保存按钮上,我们将使用SharedPreferences将标题和内容保存在本地,而在查看按钮上,我们将打开一个新的Activity来显示所有保存的日记。
```java
public class MainActivity extends AppCompatActivity {
private EditText editTextTitle, editTextContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTitle = findViewById(R.id.editTextTitle);
editTextContent = findViewById(R.id.editTextContent);
Button buttonSave = findViewById(R.id.buttonSave);
Button buttonView = findViewById(R.id.buttonView);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveDiary();
}
});
buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewDiary();
}
});
}
private void saveDiary() {
SharedPreferences sharedPreferences = getSharedPreferences("diary", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("title", editTextTitle.getText().toString());
editor.putString("content", editTextContent.getText().toString());
editor.apply();
Toast.makeText(this, "Diary saved!", Toast.LENGTH_SHORT).show();
}
private void viewDiary() {
Intent intent = new Intent(this, ViewDiaryActivity.class);
startActivity(intent);
}
}
```
3. 在ViewDiaryActivity类中,我们将从SharedPreferences获取所有保存的日记并将它们显示在一个TextView中。
```java
public class ViewDiaryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_diary);
TextView textViewDiary = findViewById(R.id.textViewDiary);
SharedPreferences sharedPreferences = getSharedPreferences("diary", Context.MODE_PRIVATE);
String title = sharedPreferences.getString("title", "");
String content = sharedPreferences.getString("content", "");
if (!title.equals("") && !content.equals("")) {
textViewDiary.setText("Title: " + title + "\n\n" + "Content: " + content);
} else {
textViewDiary.setText("No diary found!");
}
}
}
```
4. 最后,在AndroidManifest.xml文件中,我们将添加ViewDiaryActivity的声明。
```xml
<activity android:name=".ViewDiaryActivity"/>
```
这就是一个简单的日记本App的实现。当用户输入标题和内容并点击保存按钮时,它们将被保存在SharedPreferences中。当用户点击查看按钮时,它们将被检索并显示在一个TextView中。
阅读全文