日记基于Android开发代码
时间: 2023-12-14 16:21:12 浏览: 86
基于android的日记本
5星 · 资源好评率100%
以下是一个简单的日记本Android App的代码示例,包括添加、编辑、删除和展示日记的功能:
1. MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private ListView diaryListView;
private ArrayList<Diary> diaryArrayList;
private DiaryAdapter diaryAdapter;
private static final int ADD_DIARY_REQUEST_CODE = 1;
private static final int EDIT_DIARY_REQUEST_CODE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
diaryListView = findViewById(R.id.diary_list_view);
diaryArrayList = new ArrayList<>();
diaryAdapter = new DiaryAdapter(MainActivity.this, diaryArrayList);
diaryListView.setAdapter(diaryAdapter);
diaryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Diary diary = diaryArrayList.get(position);
Intent intent = new Intent(MainActivity.this, EditDiaryActivity.class);
intent.putExtra("diary", diary);
intent.putExtra("position", position);
startActivityForResult(intent, EDIT_DIARY_REQUEST_CODE);
}
});
diaryListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
diaryArrayList.remove(position);
diaryAdapter.notifyDataSetChanged();
return true;
}
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddDiaryActivity.class);
startActivityForResult(intent, ADD_DIARY_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ADD_DIARY_REQUEST_CODE:
Diary diary = (Diary) data.getSerializableExtra("diary");
diaryArrayList.add(diary);
diaryAdapter.notifyDataSetChanged();
break;
case EDIT_DIARY_REQUEST_CODE:
Diary diary1 = (Diary) data.getSerializableExtra("diary");
int position = data.getIntExtra("position", -1);
if (position != -1) {
diaryArrayList.set(position, diary1);
diaryAdapter.notifyDataSetChanged();
}
break;
}
}
}
}
```
2. AddDiaryActivity.java
```
public class AddDiaryActivity extends AppCompatActivity {
private EditText titleEditText;
private EditText contentEditText;
private Button addButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_diary);
titleEditText = findViewById(R.id.title_edit_text);
contentEditText = findViewById(R.id.content_edit_text);
addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = titleEditText.getText().toString();
String content = contentEditText.getText().toString();
Diary diary = new Diary(title, content);
Intent intent = new Intent();
intent.putExtra("diary", diary);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
3. EditDiaryActivity.java
```
public class EditDiaryActivity extends AppCompatActivity {
private EditText titleEditText;
private EditText contentEditText;
private Button saveButton;
private Diary diary;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_diary);
titleEditText = findViewById(R.id.title_edit_text);
contentEditText = findViewById(R.id.content_edit_text);
saveButton = findViewById(R.id.save_button);
diary = (Diary) getIntent().getSerializableExtra("diary");
position = getIntent().getIntExtra("position", -1);
if (diary != null) {
titleEditText.setText(diary.getTitle());
contentEditText.setText(diary.getContent());
}
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = titleEditText.getText().toString();
String content = contentEditText.getText().toString();
diary.setTitle(title);
diary.setContent(content);
Intent intent = new Intent();
intent.putExtra("diary", diary);
intent.putExtra("position", position);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
4. Diary.java
```
public class Diary implements Serializable {
private String title;
private String content;
public Diary(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
5. DiaryAdapter.java
```
public class DiaryAdapter extends ArrayAdapter<Diary> {
private Context context;
private ArrayList<Diary> diaryArrayList;
public DiaryAdapter(Context context, ArrayList<Diary> diaryArrayList) {
super(context, 0, diaryArrayList);
this.context = context;
this.diaryArrayList = diaryArrayList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_diary, parent, false);
}
TextView titleTextView = convertView.findViewById(R.id.title_text_view);
TextView contentTextView = convertView.findViewById(R.id.content_text_view);
Diary diary = diaryArrayList.get(position);
titleTextView.setText(diary.getTitle());
contentTextView.setText(diary.getContent());
return convertView;
}
}
```
这些代码示例提供了一个简单的日记本应用程序的实现,其中包括添加、编辑、删除和展示日记的功能。你可以根据自己的需求进行修改和补充。
阅读全文