实验步从登陆注册界面跳转到APP主界面。点击不同的RadioButton上对应不同的Activity。


项目结构:

Chat.java:
package com.example.test5;
public class Chat {
    private String nickname;
    private String content;
    private int imgId;
    private String datetime;
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public int getImgId() {
        return imgId;
    }
    public void setImgId(int imgId) {
        this.imgId = imgId;
    }
    public String getDatetime() {
        return datetime;
    }
    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }
    public Chat() {
    }
    public Chat(String nickname, String content, int imgId, String datetime) {
        this.nickname = nickname;
        this.content = content;
        this.imgId = imgId;
        this.datetime = datetime;
    }
}
chatAcitivity.java:
package com.example.test5;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class chatAcitivity extends AppCompatActivity {
    List<Chat> list1,list2;
    ListView listView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        listView = findViewById(R.id.list);
        Intent intent = getIntent();
        String type = intent.getStringExtra("type");
        ChatAdapter chatAdapter;
        if (type.equals("list1")){
            list1 = new ArrayList<>();
            list1.add(new Chat("高启强","告诉老默,我想吃鱼了",R.mipmap.touxiang2,"上午8:00"));
            chatAdapter= new ChatAdapter(chatAcitivity.this,R.layout.comment_main,list1);
            listView.setAdapter(chatAdapter);
        }else if (type.equals("list2")){
            list2 = new ArrayList<>();
            list2.add(new Chat("老默","好的",R.mipmap.a,"中午12:00"));
            chatAdapter= new ChatAdapter(chatAcitivity.this,R.layout.comment_main,list2);
            listView.setAdapter(chatAdapter);
        }
    }
}
ChatAdapter.java:
package com.example.test5;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class ChatAdapter extends ArrayAdapter<Chat> {
    public ChatAdapter(@NonNull Context context, int resource, @NonNull List<Chat> objects) {
        super(context, resource, objects);
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
       Chat comment = getItem(position);
        // 给子项加载布局
        View view = LayoutInflater.from(getContext()).inflate(R.layout.comment_main, parent, false);
        // 获取子布局的控件
        ImageView avatar = view.findViewById(R.id.avatar_image);
        TextView author = view.findViewById(R.id.comment_name);
        TextView contentBox = view.findViewById(R.id.comment_content);
        TextView date = view.findViewById(R.id.date_time);
        // 给控件设置内容
        avatar.setImageResource(comment.getImgId());
        author.setText(comment.getNickname());
        contentBox.setText(comment.getContent());
        date.setText(comment.getDatetime());
        return view;
    }
}
MainActivity.java:
package com.example.test5;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    EditText username,password;
    Button loginCommit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = findViewById(R.id.name);
        password = findViewById(R.id.pwd);
        loginCommit = findViewById(R.id.login);
        loginCommit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (username.getText().toString().trim().equals("laomo")&&password.getText().toString().trim().equals("123456")){
                    Toast.makeText(MainActivity.this, "欢迎你,老默!", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this,chatAcitivity.class);
                    intent.putExtra("type","list1");
                    startActivity(intent);
                }
                else if (username.getText().toString().trim().equals("qiangzi")&&password.getText().toString().trim().equals("123456")){
                    Toast.makeText(MainActivity.this, "欢迎你,高启强!", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this,chatAcitivity.class);
                    intent.putExtra("type","list2");
                    startActivity(intent);
                }
            }
        });
    }
}
activity_main.xml;
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@mipmap/beijingtu"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="200dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="用户名:"
                android:textColor="@color/black"
                android:textSize="18dp"
                android:textStyle="bold" />
            <EditText
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:background="#c2ccd0"
                android:hint="请输入用户名" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.3"
                android:gravity="center"
                android:text="密    码:"
                android:textColor="@color/black"
                android:textSize="18dp"
                android:textStyle="bold" />
            <EditText
                android:id="@+id/pwd"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:background="#c2ccd0"
                android:hint="请输入密码"
                android:inputType="textPassword" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100px"
            android:orientation="horizontal">
            <Button
                android:id="@+id/login"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:text="登录"
                android:textStyle="bold" />
            <Button
                android:id="@+id/reg"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:text="注册"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
comment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f5f5">
    <ImageView
        android:id="@+id/avatar_image"
        android:layout_marginTop="6dp"
        android:layout_marginLeft="3dp"
        android:layout_width="59dp"
        android:layout_height="47dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/comment_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:textColor="#3f51b5"
                android:textSize="15sp" />
            <TextView
                android:id="@+id/date_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dp"
                android:textSize="15sp"
                />
        </LinearLayout>
        <TextView
            android:id="@+id/comment_content"
            android:layout_width="143dp"
            android:layout_height="33dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="6dp"
            android:textColor="#7f817e"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>
index.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal"
        android:background="#ededed">
        <EditText
            android:id="@+id/et_searchtext"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:hint="搜索"
            android:background="@null"
            android:textSize="18sp"
            android:drawableLeft="@android:drawable/ic_menu_search"
            android:singleLine="true"
            android:imeOptions="actionSearch"
            />
    </LinearLayout>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="#00000000"
        android:dividerHeight="5dp"
        />
    <RadioGroup
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:background="#f2f2f2"
        android:gravity="bottom"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/communication"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:text="会话"
            android:textStyle="bold"
            android:textSize="20dp"
            android:background="#8d4bbb"
            android:textAlignment="center" />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/white" />
        <RadioButton
            android:id="@+id/tellist"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:text="通讯录"
            android:textStyle="bold"
            android:textSize="20dp"
            android:background="#8d4bbb"
            android:textAlignment="center" />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/white" />
        <RadioButton
            android:id="@+id/settings"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:textStyle="bold"
            android:textSize="20dp"
            android:text="设置"
            android:background="#8d4bbb"
            android:textAlignment="center" />
    </RadioGroup>
</LinearLayout>
mainfest配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Test5"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".chatAcitivity"/>
    </application>
</manifest>
                 
                   
                   
                   
                   
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   6240
					6240
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            