用android studio实现简单的日记本,可增加日记并且能用查看所有记录显示在listview中
时间: 2023-12-10 10:38:23 浏览: 84
好的,以下是实现步骤和代码示例:
1. 创建一个新的 Android Studio 项目。
2. 在布局文件中添加一个 EditText 用于输入日记内容,一个 Button 用于保存日记,一个 ListView 用于显示所有日记记录。
3. 创建一个 Java 类 Diary,该类包含两个属性:日期和内容。
4. 创建一个 DiaryAdapter 类,继承自 BaseAdapter,用于将 Diary 对象列表显示到 ListView 中。
5. 在 MainActivity 中创建一个 List<Diary> 类型的列表,用于存储所有的 Diary 对象。
6. 当用户点击保存按钮时,创建一个新的 Diary 对象,将其添加到 Diary 列表中,并更新 ListView 中的显示。
7. 当用户点击 ListView 中的某个日记时,显示该日记的详细内容。
以下是代码示例:
activity_main.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/diary_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入日记内容"/>
<Button
android:id="@+id/save_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"/>
<ListView
android:id="@+id/diary_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
Diary.java
```java
public class Diary {
private String date;
private String content;
public Diary(String date, String content) {
this.date = date;
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
DiaryAdapter.java
```java
public class DiaryAdapter extends BaseAdapter {
private List<Diary> diaryList;
private LayoutInflater inflater;
public DiaryAdapter(Context context, List<Diary> list) {
diaryList = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return diaryList.size();
}
@Override
public Object getItem(int position) {
return diaryList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.diary_item_layout, null);
holder = new ViewHolder();
holder.dateTextView = convertView.findViewById(R.id.date_text_view);
holder.contentTextView = convertView.findViewById(R.id.content_text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Diary diary = diaryList.get(position);
holder.dateTextView.setText(diary.getDate());
holder.contentTextView.setText(diary.getContent());
return convertView;
}
private class ViewHolder {
TextView dateTextView;
TextView contentTextView;
}
}
```
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private EditText diaryEditText;
private Button saveButton;
private ListView diaryListView;
private List<Diary> diaryList = new ArrayList<>();
private DiaryAdapter diaryAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
diaryEditText = findViewById(R.id.diary_edit_text);
saveButton = findViewById(R.id.save_button);
diaryListView = findViewById(R.id.diary_list_view);
diaryAdapter = new DiaryAdapter(this, diaryList);
diaryListView.setAdapter(diaryAdapter);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = diaryEditText.getText().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
Diary diary = new Diary(date, content);
diaryList.add(diary);
diaryAdapter.notifyDataSetChanged();
diaryEditText.setText("");
}
});
diaryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Diary diary = diaryList.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(diary.getDate());
builder.setMessage(diary.getContent());
builder.setPositiveButton("确定", null);
builder.show();
}
});
}
}
```
diary_item_layout.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/date_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#333333"/>
<TextView
android:id="@+id/content_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#666666"/>
</LinearLayout>
```
这样就完成了一个简单的日记本应用,用户可以在输入框中输入日记内容,并点击保存按钮保存日记。所有的日记会显示在 ListView 中,用户可以点击某个日记查看其详细内容。
阅读全文