基于安卓写一个简单的笔记app代码
时间: 2024-05-01 17:23:16 浏览: 91
以下是一个简单的安卓笔记app的代码示例,包含了添加笔记、编辑笔记、删除笔记和查看笔记的功能:
MainActivity.java
```
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView notesListView;
ArrayList<String> notesList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notesListView = findViewById(R.id.notes_list_view);
notesList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, notesList);
notesListView.setAdapter(adapter);
notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
editNoteDialog(i);
}
});
}
public void addNoteDialog(View view) {
AddNoteDialog dialog = new AddNoteDialog();
dialog.show(getSupportFragmentManager(), "addNoteDialog");
dialog.setOnDialogCloseListener(new AddNoteDialog.OnDialogCloseListener() {
@Override
public void onDialogClose(String note) {
notesList.add(note);
adapter.notifyDataSetChanged();
}
});
}
public void editNoteDialog(final int position) {
EditNoteDialog dialog = new EditNoteDialog();
Bundle bundle = new Bundle();
bundle.putString("note", notesList.get(position));
dialog.setArguments(bundle);
dialog.show(getSupportFragmentManager(), "editNoteDialog");
dialog.setOnDialogCloseListener(new EditNoteDialog.OnDialogCloseListener() {
@Override
public void onDialogClose(String note) {
notesList.set(position, note);
adapter.notifyDataSetChanged();
}
});
}
public void deleteNoteDialog(View view) {
DeleteNoteDialog dialog = new DeleteNoteDialog();
dialog.show(getSupportFragmentManager(), "deleteNoteDialog");
dialog.setOnDialogCloseListener(new DeleteNoteDialog.OnDialogCloseListener() {
@Override
public void onDialogClose(boolean delete) {
if (delete) {
int position = notesListView.getCheckedItemPosition();
if (position != ListView.INVALID_POSITION) {
notesList.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Note deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Please select a note to delete", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/add_note_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Note"
android:onClick="addNoteDialog"/>
<Button
android:id="@+id/edit_note_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit Note"
android:layout_toRightOf="@id/add_note_button"
android:layout_marginLeft="8dp"
android:onClick="editNoteDialog"/>
<Button
android:id="@+id/delete_note_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Note"
android:layout_toRightOf="@id/edit_note_button"
android:layout_marginLeft="8dp"
android:onClick="deleteNoteDialog"/>
<ListView
android:id="@+id/notes_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/add_note_button"/>
</RelativeLayout>
```
AddNoteDialog.java
```
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;
public class AddNoteDialog extends AppCompatDialogFragment {
private EditText noteEditText;
private Button addButton, cancelButton;
private OnDialogCloseListener onDialogCloseListener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_add_note, null);
noteEditText = view.findViewById(R.id.note_edit_text);
addButton = view.findViewById(R.id.add_button);
cancelButton = view.findViewById(R.id.cancel_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String note = noteEditText.getText().toString();
if (!TextUtils.isEmpty(note)) {
onDialogCloseListener.onDialogClose(note);
dismiss();
}
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
builder.setView(view);
return builder.create();
}
public void setOnDialogCloseListener(OnDialogCloseListener onDialogCloseListener) {
this.onDialogCloseListener = onDialogCloseListener;
}
public interface OnDialogCloseListener {
void onDialogClose(String note);
}
}
```
dialog_add_note.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/note_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter note"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end"
android:layout_marginTop="16dp">
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_marginLeft="16dp"/>
</LinearLayout>
</LinearLayout>
```
EditNoteDialog.java
```
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;
public class EditNoteDialog extends AppCompatDialogFragment {
private EditText noteEditText;
private Button saveButton, cancelButton;
private OnDialogCloseListener onDialogCloseListener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_edit_note, null);
noteEditText = view.findViewById(R.id.note_edit_text);
saveButton = view.findViewById(R.id.save_button);
cancelButton = view.findViewById(R.id.cancel_button);
Bundle bundle = getArguments();
if (bundle != null) {
String note = bundle.getString("note");
noteEditText.setText(note);
}
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String note = noteEditText.getText().toString();
if (!TextUtils.isEmpty(note)) {
onDialogCloseListener.onDialogClose(note);
dismiss();
}
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
builder.setView(view);
return builder.create();
}
public void setOnDialogCloseListener(OnDialogCloseListener onDialogCloseListener) {
this.onDialogCloseListener = onDialogCloseListener;
}
public interface OnDialogCloseListener {
void onDialogClose(String note);
}
}
```
dialog_edit_note.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/note_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter note"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end"
android:layout_marginTop="16dp">
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"/>
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_marginLeft="16dp"/>
</LinearLayout>
</LinearLayout>
```
DeleteNoteDialog.java
```
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialogFragment;
public class DeleteNoteDialog extends AppCompatDialogFragment {
private Button deleteButton, cancelButton;
private OnDialogCloseListener onDialogCloseListener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_delete_note, null);
deleteButton = view.findViewById(R.id.delete_button);
cancelButton = view.findViewById(R.id.cancel_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onDialogCloseListener.onDialogClose(true);
dismiss();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
builder.setView(view);
return builder.create();
}
public void setOnDialogCloseListener(OnDialogCloseListener onDialogCloseListener) {
this.onDialogCloseListener = onDialogCloseListener;
}
public interface OnDialogCloseListener {
void onDialogClose(boolean delete);
}
}
```
dialog_delete_note.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Are you sure you want to delete this note?"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="end"
android:layout_marginTop="16dp">
<Button
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"/>
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_marginLeft="16dp"/>
</LinearLayout>
</LinearLayout>
```
注意:以上代码仅供参考,具体实现可能会因为不同的需求和场景而有所不同。
阅读全文