改良《第一行代码》第二版第三章制作聊天界面并保存聊天数据,做法如下:在主界面发放置一个RecycleView用于显示聊天信息内容,又放置一个EditText用于输入消息,还放置一个Button用于发送消息。使用SharedPreferences保存发送的消息数据,下次再打开该界面时,已发送的消息仍保存在界面上
时间: 2024-02-03 11:13:49 浏览: 133
好的,下面是改良版的代码实现:
1. 在布局文件中添加RecycleView、EditText和Button控件
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/chat_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<EditText
android:id="@+id/chat_input_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/chat_input_hint"/>
<Button
android:id="@+id/chat_send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chat_send_button_text"/>
</LinearLayout>
```
2. 创建一个ChatAdapter类用于显示聊天信息
```java
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<String> mChatList;
static class ViewHolder extends RecyclerView.ViewHolder {
TextView chatText;
public ViewHolder(View view) {
super(view);
chatText = view.findViewById(R.id.chat_text);
}
}
public ChatAdapter(List<String> chatList) {
mChatList = chatList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String chat = mChatList.get(position);
holder.chatText.setText(chat);
}
@Override
public int getItemCount() {
return mChatList.size();
}
}
```
3. 在MainActivity中初始化RecycleView、EditText和Button控件,以及从SharedPreferences中读取已发送的消息数据
```java
public class MainActivity extends AppCompatActivity {
private List<String> mChatList = new ArrayList<>();
private ChatAdapter mAdapter;
private SharedPreferences mSharedPreferences;
private EditText mInputEditText;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSharedPreferences = getSharedPreferences("chat_data", MODE_PRIVATE);
Set<String> chatSet = mSharedPreferences.getStringSet("chat_list", new HashSet<String>());
mChatList.addAll(chatSet);
mAdapter = new ChatAdapter(mChatList);
mRecyclerView = findViewById(R.id.chat_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
mRecyclerView.setAdapter(mAdapter);
mInputEditText = findViewById(R.id.chat_input_edit_text);
Button sendButton = findViewById(R.id.chat_send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText = mInputEditText.getText().toString();
if (!TextUtils.isEmpty(inputText)) {
mChatList.add(inputText);
mAdapter.notifyItemInserted(mChatList.size() - 1);
mRecyclerView.scrollToPosition(mChatList.size() - 1);
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putStringSet("chat_list", new HashSet<>(mChatList));
editor.apply();
mInputEditText.setText("");
}
}
});
}
}
```
4. 创建一个chat_item.xml布局文件用于显示聊天信息
```xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"/>
```
这样,就完成了在主界面制作聊天界面并保存聊天数据的改良版实现。
阅读全文