<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:background="#FF4D60"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="40dp"
android:textColor="#fff"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/headimg"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:background="@drawable/ruan"
android:layout_below="@+id/headimg"
android:layout_marginTop="30dp"
android:id="@+id/username"
android:hint="在?输一下名字?"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="30dp"
android:background="@drawable/ruan"
android:layout_below="@+id/username"
android:id="@+id/passwords"
android:hint="为什么不输入密码????"
/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_below="@+id/passwords"
android:text="点击注册"
android:layout_marginTop="30dp"
android:background="@drawable/ruaner"
android:textColor="#fff"
android:id="@+id/logbtn"
/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_below="@+id/logbtn"
android:text="点击登录"
android:layout_marginTop="30dp"
android:background="@drawable/ruaner"
android:textColor="#fff"
android:id="@+id/gogobtn"
/>
</RelativeLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private ImageView headimg;
private EditText username;
private EditText passwords;
private Button logbtn;
private Button gogobtn;
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 || resultCode == 101){
username.setText(data.getStringExtra("names"));
passwords.setText(data.getStringExtra("pwds"));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
headimg = (ImageView) findViewById(R.id.headimg);
username = (EditText) findViewById(R.id.username);
passwords = (EditText) findViewById(R.id.passwords);
logbtn = (Button) findViewById(R.id.logbtn);
gogobtn = (Button) findViewById(R.id.gogobtn);
gogobtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = username.getText().toString();
String s1 = passwords.getText().toString();
Intent intent = new Intent(MainActivity.this, Main3Activity.class);
startActivity(intent);
}
});
}
}
public class MusicAdapter extends BaseAdapter {
private List<Music> lists;
private Context context;
public MusicAdapter(List<Music> list, Context context) {
this.lists = list;
this.context = context;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int i) {
return lists.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null){
holder = new ViewHolder();
view = View.inflate(context, R.layout.song_layout,null);
holder.text_artist = view.findViewById(R.id.text_artist);
holder.text_duration = view.findViewById(R.id.text_duration);
holder.text_title = view.findViewById(R.id.text_title);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.text_title.setText(lists.get(i).getTitle());
holder.text_duration.setText(lists.get(i).getDuration());
holder.text_artist.setText(lists.get(i).getArtist());
return view;
}
private class ViewHolder{
private TextView text_title,text_artist,text_duration;
}
}
public class MusicUtil {
public static List<Music> getMusic(Context context){
List<Music> lists = new ArrayList<>();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor != null){
while (cursor.moveToNext()){
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String size = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
Music music = new Music(title, artist, duration, data, size);
lists.add(music);
}
cursor.close();
return lists;
}
return null;
}
}
public class Main4Activity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private List<Music> lists;
private MusicAdapter adapter;
private ListView listView;
private MediaPlayer mediaPlayer;
private SeekBar seek;
private Timer timer = new Timer();
private int index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
seek = findViewById(R.id.seek);
listView = findViewById(R.id.listView);
seek.setProgress(0);
mediaPlayer = new MediaPlayer();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b == true){
mediaPlayer.seekTo(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
lists = new MusicUtil().getMusic(this);
Log.i(TAG, "" + lists);
adapter = new MusicAdapter(lists, this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
index = i;
seek.setProgress(0);
mediaPlayer.reset();
try {
seek.setMax(Integer.parseInt(lists.get(i).getDuration()));
mediaPlayer.setDataSource(lists.get(i).getData());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
timer.schedule(new TimerTask() {
@Override
public void run() {
seek.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
}
public void click(View view) {
switch (view.getId()){
case R.id.button_start:
mediaPlayer.start();
break;
case R.id.button_pause:
mediaPlayer.pause();
break;
case R.id.button_last:
index--;
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(lists.get(index).getData());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.button_next:
index++;
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(lists.get(index).getData());
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
08-14