用Android Studio软件编写微信界面代码
时间: 2023-08-05 17:36:07 浏览: 139
以下是一个简单的微信界面代码示例,包括底部导航栏、联系人列表和聊天列表等。
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private FrameLayout fragmentContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottom_navigation);
fragmentContainer = findViewById(R.id.fragment_container);
// 设置底部导航栏的点击事件
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.tab_chat:
showChatFragment();
break;
case R.id.tab_contact:
showContactFragment();
break;
default:
break;
}
return true;
}
});
// 默认显示聊天界面
showChatFragment();
}
private void showChatFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, new ChatFragment());
transaction.commit();
}
private void showContactFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, new ContactFragment());
transaction.commit();
}
}
```
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@+id/bottom_navigation"
android:layout_alignParentTop="true"
android:layout_weight="1" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
app:itemIconTint="@color/bottom_navigation_item_color"
app:itemTextColor="@color/bottom_navigation_item_color"
app:menu="@menu/menu_bottom_navigation" />
</RelativeLayout>
```
ChatFragment.java:
```java
public class ChatFragment extends Fragment {
private RecyclerView recyclerView;
private List<ChatMessage> chatMessageList;
private ChatAdapter chatAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_chat, container, false);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
chatMessageList = new ArrayList<>();
chatAdapter = new ChatAdapter(chatMessageList);
recyclerView.setAdapter(chatAdapter);
// 添加聊天消息到列表中
ChatMessage chatMessage = new ChatMessage("Hello, world!", ChatMessage.TYPE_RECEIVED);
chatMessageList.add(chatMessage);
chatAdapter.notifyItemInserted(chatMessageList.size() - 1);
recyclerView.scrollToPosition(chatMessageList.size() - 1);
return view;
}
}
```
fragment_chat.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
ContactFragment.java:
```java
public class ContactFragment extends Fragment {
private EditText searchText;
private RecyclerView recyclerView;
private List<Contact> contactList;
private ContactAdapter contactAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact, container, false);
searchText = view.findViewById(R.id.search_text);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
contactList = new ArrayList<>();
contactAdapter = new ContactAdapter(contactList);
recyclerView.setAdapter(contactAdapter);
// 添加联系人到列表中
Contact contact1 = new Contact("Tom");
contactList.add(contact1);
Contact contact2 = new Contact("Jerry");
contactList.add(contact2);
contactAdapter.notifyDataSetChanged();
// 实现搜索功能
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
private void filter(String text) {
List<Contact> filteredList = new ArrayList<>();
for (Contact contact : contactList) {
if (contact.getName().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(contact);
}
}
contactAdapter.filterList(filteredList);
}
}
```
fragment_contact.xml:
```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="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/search_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
```
ChatAdapter.java:
```java
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ChatMessage> chatMessageList;
public ChatAdapter(List<ChatMessage> chatMessageList) {
this.chatMessageList = chatMessageList;
}
@Override
public int getItemViewType(int position) {
return chatMessageList.get(position).getType();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == ChatMessage.TYPE_RECEIVED) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_received, parent, false);
return new ReceivedViewHolder(view);
} else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_sent, parent, false);
return new SentViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ChatMessage chatMessage = chatMessageList.get(position);
if (holder instanceof ReceivedViewHolder) {
((ReceivedViewHolder) holder).messageText.setText(chatMessage.getMessage());
} else if (holder instanceof SentViewHolder) {
((SentViewHolder) holder).messageText.setText(chatMessage.getMessage());
}
}
@Override
public int getItemCount() {
return chatMessageList.size();
}
public static class ReceivedViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public ReceivedViewHolder(@NonNull View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.message_text);
}
}
public static class SentViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public SentViewHolder(@NonNull View itemView) {
super(itemView);
messageText = itemView.findViewById(R.id.message_text);
}
}
}
```
item_chat_received.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:background="@drawable/chat_received_bg"
android:orientation="vertical"
android:padding="12dp">
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white" />
</LinearLayout>
```
item_chat_sent.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:background="@drawable/chat_sent_bg"
android:orientation="vertical"
android:padding="12dp">
<TextView
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white" />
</LinearLayout>
```
ContactAdapter.java:
```java
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private List<Contact> contactList;
private List<Contact> filteredList;
public ContactAdapter(List<Contact> contactList) {
this.contactList = contactList;
this.filteredList = new ArrayList<>(contactList);
}
@NonNull
@Override
public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
return new ContactViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
Contact contact = filteredList.get(position);
holder.nameText.setText(contact.getName());
}
@Override
public int getItemCount() {
return filteredList.size();
}
public void filterList(List<Contact> filteredList) {
this.filteredList = filteredList;
notifyDataSetChanged();
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
public TextView nameText;
public ContactViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.name_text);
}
}
}
```
item_contact.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/name_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp" />
```
ChatMessage.java:
```java
public class ChatMessage {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SENT = 1;
private String message;
private int type;
public ChatMessage(String message, int type) {
this.message = message;
this.type = type;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
```
Contact.java:
```java
public class Contact {
private String name;
public Contact(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
注意:以上代码只是一个简单的示例,实际开发中可能需要更多的功能和优化。
阅读全文