ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签

一、简介

1.在main.xml中用TabHost、TabWidget、FrameLayout标签作布局

2.在MainActivity中生成TabHost、TabSpec,调用setIndicator()、setContent()、addTab(),用Intent指明要跳转的tab对应的class

3.在onResume中写获取本地mp3信息的代码,代码在onResume中,则切换tab时,mp3信息每次都会更新

二、代码
1.xml
(1)main.xml

 1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:id="@android:id/tabhost" android:layout_width="fill_parent"
 3     android:layout_height="fill_parent">
 4     <LinearLayout android:orientation="vertical"
 5         android:layout_width="fill_parent" android:layout_height="fill_parent"
 6         android:padding="5dp">
 7         <TabWidget android:id="@android:id/tabs"
 8             android:layout_width="fill_parent" android:layout_height="wrap_content" />
 9         <FrameLayout android:id="@android:id/tabcontent"
10             android:layout_width="fill_parent" android:layout_height="fill_parent"
11             android:padding="5dp" />
12     </LinearLayout>
13 </TabHost> 

(2)AndroidManifest.xml

增加activity

1 <activity android:name=".LocalMp3ListActivity" android:label="@string/app_name"/>
2 <activity android:name=".Mp3ListActivity" android:label="@string/app_name"/>

2.java
(1)MainActivity.java

 1 package tony.mp3player;
 2
 3 import android.app.TabActivity;
 4 import android.content.Intent;
 5 import android.content.res.Resources;
 6 import android.os.Bundle;
 7 import android.widget.TabHost;
 8
 9 public class MainActivity extends TabActivity {
10
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.main);
15         //得到TabHost对象,对TabActivity的操作通常都有这个对象完成
16         TabHost tabHost = getTabHost();
17         //生成一个Intent对象,该对象指向一个Activity
18         Intent remoteIntent = new Intent();
19         remoteIntent.setClass(this, Mp3ListActivity.class);
20         //生成一个TabSpec对象,这个对象代表了一个页
21         TabHost.TabSpec remoteSpec = tabHost.newTabSpec("Remote");
22         Resources res = getResources();
23         //设置该页的indicator
24         remoteSpec.setIndicator("Remote", res.getDrawable(android.R.drawable.stat_sys_download));
25         //设置该页的内容
26         remoteSpec.setContent(remoteIntent);
27         //将设置好的TabSpec对象添加到TabHost当中
28         tabHost.addTab(remoteSpec);
29
30         Intent localIntent = new Intent();
31         localIntent.setClass(this, LocalMp3ListActivity.class);
32         TabHost.TabSpec localSpec = tabHost.newTabSpec("Local");
33         localSpec.setIndicator("Local", res.getDrawable(android.R.drawable.stat_sys_upload));
34         localSpec.setContent(localIntent);
35         tabHost.addTab(localSpec);
36     }
37 }

(2)FileUtils.java增加函数

 1     public List<Mp3Info> getMp3Infos(String path) {
 2         List<Mp3Info> list = new ArrayList<Mp3Info>();
 3         File file = new File(SDCardRoot + File.separator + path);
 4         File[] files = file.listFiles();
 5         for(File tempFile : files) {
 6             if(tempFile.getName().endsWith(".mp3")) {
 7                 list.add(new Mp3Info(tempFile.getName(), tempFile.length()+""));
 8             }
 9         }
10         return list;
11     }
时间: 2024-12-14 11:39:49

ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签的相关文章

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过android.media.MediaPlayer实现播放,暂停.停止 二.代码1.xml(1)player.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:andr

ANDROID_MARS学习笔记_S01原始版_010_ContentProvider

一.简介 一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill

ANDROID_MARS学习笔记_S01原始版_005_ProgressBar

一.代码 1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_p

ANDROID_MARS学习笔记_S01原始版_006_ListView

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="fill_pare

ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

一.代码 1.xml(1)radio.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_