android软件开发:类似微信的简单软件程序,包含聊天界面,播放音乐界面,联系人界面以及相册界面
时间: 2023-12-06 13:03:11 浏览: 89
以下是一个类似微信的简单软件的Android应用程序的示例代码,包含聊天界面、播放音乐界面、联系人界面以及相册界面。
activity_main.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">
<Button
android:id="@+id/chatBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="聊天" />
<Button
android:id="@+id/musicBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放音乐" />
<Button
android:id="@+id/contactBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="联系人" />
<Button
android:id="@+id/galleryBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相册" />
</LinearLayout>
```
ChatActivity.java代码文件:
```
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class ChatActivity extends AppCompatActivity {
private ArrayList<String> messagesList = new ArrayList<>();
private ListView messageListView;
private EditText messageEditText;
private Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
messageListView = findViewById(R.id.messageListView);
messageEditText = findViewById(R.id.messageEditText);
sendButton = findViewById(R.id.sendButton);
final MessageAdapter adapter = new MessageAdapter(this, messagesList);
messageListView.setAdapter(adapter);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = messageEditText.getText().toString();
if (message.isEmpty()) {
Toast.makeText(ChatActivity.this, "请输入消息", Toast.LENGTH_SHORT).show();
return;
}
messagesList.add(message);
adapter.notifyDataSetChanged();
messageEditText.setText("");
}
});
}
}
```
MusicActivity.java代码文件:
```
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MusicActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
mediaPlayer = MediaPlayer.create(this, R.raw.sample_music);
Button playButton = findViewById(R.id.playButton);
Button pauseButton = findViewById(R.id.pauseButton);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
} else {
Toast.makeText(MusicActivity.this, "音乐正在播放", Toast.LENGTH_SHORT).show();
}
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
Toast.makeText(MusicActivity.this, "音乐已暂停", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onStop() {
super.onStop();
mediaPlayer.release();
}
}
```
ContactActivity.java代码文件:
```
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class ContactActivity extends AppCompatActivity {
private ArrayList<String> contactList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
contactList.add("张三");
contactList.add("李四");
contactList.add("王五");
ListView contactListView = findViewById(R.id.contactListView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, contactList);
contactListView.setAdapter(adapter);
}
}
```
GalleryActivity.java代码文件:
```
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
public class GalleryActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 1;
private ArrayList<String> imageList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
if (ContextCompat.checkSelfPermission(GalleryActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(GalleryActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
} else {
loadImages();
}
}
private void loadImages() {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
imageList.add(imagePath);
}
cursor.close();
}
GridView gridView = findViewById(R.id.gridView);
ImageAdapter adapter = new ImageAdapter(this, imageList);
gridView.setAdapter(adapter);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadImages();
}
}
}
}
```
在这个示例中,我们使用了ListView、MediaPlayer、ArrayAdapter和GridView等Android控件和API来实现聊天界面、播放音乐界面、联系人界面以及相册界面。这个简单的示例可能需要根据您的具体需求进行更改和扩展。
阅读全文