使用AndroidStudio完成一个简易备忘录功能,主页点击加号按钮,弹出可编辑对话框,点击保存之后动态在该布局加入数据并使用sharedpreferencs保存在该页面,动态增长该布局的长度;点击减号删除该信息,以上功能都在主界面成,请写出示例代码
时间: 2024-02-09 19:12:53 浏览: 67
好的,下面是一个简单的备忘录功能的示例代码:
1. 在主界面布局中添加一个加号按钮和一个垂直布局,用于显示备忘录列表。
```
<androidx.constraintlayout.widget.ConstraintLayout
...
<ImageButton
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/btn_add"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0"
app:layout_constraintVertical_weight="1">
<LinearLayout
android:id="@+id/layout_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
```
2. 在主界面对应的Activity中,获取按钮和布局,并为按钮设置点击事件。
```
public class MainActivity extends AppCompatActivity {
private ImageButton btnAdd;
private LinearLayout layoutList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取按钮和布局
btnAdd = findViewById(R.id.btn_add);
layoutList = findViewById(R.id.layout_list);
// 设置按钮点击事件
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 弹出可编辑对话框
// 点击保存后调用 addMemo 方法添加备忘录,并保存到 SharedPreferences 中
}
});
// 初始化备忘录列表
initMemoList();
}
// 初始化备忘录列表
private void initMemoList() {
// 从 SharedPreferences 中读取备忘录列表
// 使用 addMemo 方法添加每个备忘录的布局
}
// 添加一个备忘录
private void addMemo(String memo) {
// 创建备忘录布局
TextView tvMemo = new TextView(this);
tvMemo.setText(memo);
tvMemo.setTextSize(16);
tvMemo.setPadding(16, 16, 16, 16);
// 添加备忘录布局到列表
layoutList.addView(tvMemo);
// 保存备忘录列表到 SharedPreferences 中
// 遍历备忘录列表,保存每个备忘录的文字到 SharedPreferences 中
}
// 删除一个备忘录
private void removeMemo(View v) {
// 从列表中移除备忘录布局
layoutList.removeView(v);
// 保存备忘录列表到 SharedPreferences 中
// 遍历备忘录列表,保存每个备忘录的文字到 SharedPreferences 中
}
}
```
3. 在弹出的可编辑对话框中,添加一个编辑框和保存按钮,并为保存按钮设置点击事件。
```
public void showEditDialog() {
// 创建对话框布局
LinearLayout layoutDialog = new LinearLayout(this);
layoutDialog.setOrientation(LinearLayout.VERTICAL);
layoutDialog.setPadding(16, 16, 16, 16);
// 创建编辑框
final EditText etMemo = new EditText(this);
etMemo.setHint("请输入备忘录内容");
layoutDialog.addView(etMemo);
// 创建保存按钮
Button btnSave = new Button(this);
btnSave.setText("保存");
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取编辑框中的备忘录内容
String memo = etMemo.getText().toString();
// 添加备忘录
addMemo(memo);
// 关闭对话框
dialog.dismiss();
}
});
layoutDialog.addView(btnSave);
// 创建对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layoutDialog);
dialog = builder.create();
dialog.show();
}
```
4. 在备忘录列表中,为每个备忘录布局设置长按事件,用于删除该备忘录。
```
// 初始化备忘录列表
private void initMemoList() {
// 从 SharedPreferences 中读取备忘录列表
// 使用 addMemo 方法添加每个备忘录的布局
SharedPreferences sp = getSharedPreferences("memo", MODE_PRIVATE);
int count = sp.getInt("count", 0);
for (int i = 0; i < count; i++) {
String memo = sp.getString("memo_" + i, "");
addMemo(memo);
}
}
// 添加一个备忘录
private void addMemo(String memo) {
// 创建备忘录布局
TextView tvMemo = new TextView(this);
tvMemo.setText(memo);
tvMemo.setTextSize(16);
tvMemo.setPadding(16, 16, 16, 16);
// 添加备忘录布局到列表
layoutList.addView(tvMemo);
// 保存备忘录列表到 SharedPreferences 中
SharedPreferences sp = getSharedPreferences("memo", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int count = sp.getInt("count", 0);
editor.putString("memo_" + count, memo);
editor.putInt("count", count + 1);
editor.apply();
// 设置备忘录布局的长按事件,用于删除备忘录
tvMemo.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
removeMemo(v);
return true;
}
});
}
// 删除一个备忘录
private void removeMemo(View v) {
// 从列表中移除备忘录布局
layoutList.removeView(v);
// 保存备忘录列表到 SharedPreferences 中
SharedPreferences sp = getSharedPreferences("memo", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int count = sp.getInt("count", 0);
int index = layoutList.indexOfChild(v);
editor.remove("memo_" + index);
for (int i = index + 1; i < count; i++) {
String memo = sp.getString("memo_" + i, "");
editor.putString("memo_" + (i - 1), memo);
}
editor.putInt("count", count - 1);
editor.apply();
}
```
以上就是一个简单的备忘录功能的示例代码,可以根据实际需求进行修改和优化。
阅读全文