android备忘录教学_Android Studio实战 - 备忘录实验
时间: 2023-06-26 13:07:34 浏览: 193
好的,下面我来为您讲解如何在Android Studio中实现备忘录应用的开发。
首先,我们需要创建一个新的Android Studio项目。在创建项目时,您可以选择空白活动或使用模板(例如带有导航抽屉的活动)。在这里,我们将使用空白活动来创建我们的备忘录应用。
接下来,我们需要设计应用的用户界面。我们将使用RecyclerView来显示备忘录列表,并且使用一个FloatingActionButton来添加新的备忘录。使用Android Studio的布局编辑器,您可以轻松地设计出这个用户界面。
在我们开始编写代码之前,我们需要添加RecyclerView和FloatingActionButton的依赖库。在app/build.gradle文件中,添加以下依赖项:
```
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
}
```
接下来,我们需要创建一个备忘录数据模型类Memo,该类将具有备忘录的标题和内容属性。例如:
```
public class Memo {
private String title;
private String content;
public Memo(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;
}
}
```
接下来,我们需要创建一个备忘录列表适配器MemoAdapter,该适配器将显示备忘录列表。例如:
```
public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.ViewHolder> {
private List<Memo> memos;
public MemoAdapter(List<Memo> memos) {
this.memos = memos;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_memo, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Memo memo = memos.get(position);
holder.titleTextView.setText(memo.getTitle());
holder.contentTextView.setText(memo.getContent());
}
@Override
public int getItemCount() {
return memos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView titleTextView;
private TextView contentTextView;
public ViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
contentTextView = itemView.findViewById(R.id.contentTextView);
}
}
}
```
接下来,我们需要创建一个活动MainActivity,该活动将显示备忘录列表。在MainActivity中,我们需要将RecyclerView与MemoAdapter进行绑定,并在FloatingActionButton的单击事件中添加新的备忘录。例如:
```
public class MainActivity extends AppCompatActivity {
private List<Memo> memos = new ArrayList<>();
private MemoAdapter memoAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
memoAdapter = new MemoAdapter(memos);
recyclerView.setAdapter(memoAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddMemoDialog();
}
});
}
private void showAddMemoDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_memo, null);
final EditText titleEditText = view.findViewById(R.id.titleEditText);
final EditText contentEditText = view.findViewById(R.id.contentEditText);
builder.setView(view)
.setTitle("Add Memo")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String title = titleEditText.getText().toString();
String content = contentEditText.getText().toString();
Memo memo = new Memo(title, content);
memos.add(memo);
memoAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
```
最后,在这个应用程序中,我们需要添加一些布局文件。例如:
activity_main.xml
```
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/ic_add" />
</android.support.design.widget.CoordinatorLayout>
```
item_memo.xml
```
<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="8dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
```
dialog_add_memo.xml
```
<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="8dp">
<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title" />
<EditText
android:id="@+id/contentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Content" />
</LinearLayout>
```
现在您已经完成了备忘录应用的开发。您可以在模拟器或实际设备上运行该应用程序,并开始添加和查看备忘录了!
阅读全文