我有一个应用程序,其菜单中包含一个ListView,我希望链接到一个具有相同布局样式的活动,但点击后内容应该不同。
ListView包含歌曲列表,单击时单个项目的布局包含标题(TextView),歌词(TextView),“next_button”按钮,在歌曲之间移动的“previous_button”按钮,“ back_button“按钮返回ListView,”play_tune“按钮播放该特定歌曲的音频。并非所有歌曲都包含音频,但“play_tune”按钮所在的位置,我希望应用程序图标ic_launcher-web.png显示在那里。
我想要显示的XML文件(对于带有音频的歌曲)如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/song"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScoutSongs" >
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<ImageButton
android:id="@+id/play_tune"
android:src="@drawable/play_tune"
android:contentDescription="@string/play_tune"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/Title"
android:layout_alignTop="@+id/Title" />
<ScrollView
android:id="@+id/LyricsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/back_button"
android:layout_alignLeft="@+id/Title"
android:layout_alignRight="@+id/play_tune"
android:layout_below="@+id/play_tune" >
<TextView
android:id="@+id/Lyrics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF" />
</ScrollView>
<Button
android:id="@+id/back_button"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/LyricsView"
android:text="@string/back"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/previous_button"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/back_button"
android:layout_alignBottom="@+id/back_button"
android:layout_alignLeft="@+id/LyricsView"
android:text="@string/previous"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/next_button"
android:layout_width="95dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/previous_button"
android:layout_alignBottom="@+id/previous_button"
android:layout_toRightOf="@+id/previous_button"
android:text="@string/next"
android:textColor="#FFFFFF" />
我当前的SongActivity.java文件如下所示:
package com.lmarshall1995.scoutsongs;
import com.lmarshall1995.scoutsongs.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class SongActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.song);
Intent i = getIntent();
String Title = i.getStringExtra("Title");
String Lyrics = i.getStringExtra("Lyrics");
String TuneToast = i.getStringExtra("TuneToast");
String Tune = i.getStringExtra("Tune");
setupNavigationButton();
Toast toast = Toast.makeText(this, TuneToast , Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
final Button back = (Button) findViewById(R.id.back_button);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
final Button previous = (Button) findViewById(R.id.previous_button);
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent previousIntent = new Intent(SongActivity.this, SongActivity.class);
SongActivity.this.startActivity(previousIntent);
finish();
}
});
final Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent nextIntent = new Intent(SongActivity.this, SongActivity.class);
SongActivity.this.startActivity(nextIntent);
finish();
}
});
final ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
play_tune.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
play_tune.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp = MediaPlayer.create(SongActivity.this, Tune);
public void onClick(View arg0) {
if (mp.isPlaying()) {
mp.stop();
mp.prepareAsync();
mp.seekTo(0);
} else {
mp.start();
}
}
});
}
});
}
private void setupNavigationButton() {}
}
我的ListView的Menu.java就在这里:
package com.lmarshall1995.scoutsongs;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
public class Menu extends ListActivity{
String classes[] = {"Song_AliceTheCamel_Activity", "Song_Bingo_Activity",
"Song_CampfiresBurning_Activity", "Song_DownAtTheStation_Activity",
"Song_EverywhereWeGo_Activity", "Song_FeFi_Activity",
"Song_GingGangGooly_Activity", "Song_HeadShouldersKneesToes_Activity",
"Song_IfYoureHappyAndYouKnowIt_Activity", "Song_JoesButtonFactory_Activity",
"Song_Kumbaya_Activity", "Song_LittleGreenFrog_Activity",
"Song_Meatball_Activity", "Song_NationalAnthem_Activity",
"Song_OldMacDonald_Activity", "Song_PizzaHut_Activity",
"Song_QuartermastersStores_Activity", "Song_RowRowRowYourBoat_Activity",
"Song_ShineUpYourButtons_Activity", "Song_ThreeBlindJellyfish_Activity",
"Song_Underwear_Activity", "Song_Valerie_Activity", "Song_Worms_Activity",
"Song_XCommissionersInTheTown_Activity", "Song_YogiBear_Activity",
"Song_ZipADeeDooDah_Activity"};
String items[] = {"Alice The Camel", "Bingo", "Campfire\'s Burning", "Down At The Station",
"Everywhere We Go", "Fe Fi", "Ging Gang Gooly", "Head, Shoulders, Knees & Toes",
"If You\'re Happy And You Know It", "Joe\'s Button Factory", "Kumbaya",
"Little Green Frog", "Meatball", "National Anthem (UK)", "Old MacDonald", "Pizza Hut",
"Quartermaster\'s Stores", "Row, Row, Row Your Boat", "Shine Up Your Buttons",
"Three Blind Jellyfish", "Underwear", "Valerie", "Worms",
"X-Commissioner\'s In The Town", "Yogi Bear", "Zip A Dee Doo Dah"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
final ImageButton settings = (ImageButton) findViewById(R.id.settings_button);
settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent settingsIntent = new Intent(Menu.this, Settings.class);
Menu.this.startActivity(settingsIntent);
}
});
final ImageButton back = (ImageButton) findViewById(R.id.exit_button);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, items));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(Menu.this, SongActivity.class);
i.putExtra("Title", "Song_" + classes + "_Title");
i.putExtra("Lyrics", "Song_" + classes + "_Lyrics");
i.putExtra("TuneToast", "To be sung in the tune of \n" + classes);
startActivity(i);
}
}
我的所有标题和歌词目前都在我的Values \ strings.xml文件中。
在ListView中选中后,如何让我的所有歌曲都在一个“SongActivity”类中?我相信如果你不得不多次写出任何东西,应该有一个更简单的方法来做到这一点。
修改:如何让MediaPlayer mp = MediaPlayer.create(SongActivity.this, Tune);
和String Tune = i.getStringExtra("Tune");
一起工作?
答案 0 :(得分:1)
选择列表项时,请在意图中使用额外内容调用SongActivity,例如SongName和其他所需的参数。然后阅读它并相应地填充字段。
onItemSelected方法:
Intent i = new Intent(this, SongActivity.class);
i.putExtra("songName", "Song name here"); //get song name from the item selected
....
startActivity(i);
在您的SongActivity类中:
onCreate(...){
Intent i = getIntent();
String songName = i.getStringExtra("songName");
...
}
编辑:这样所有其他固定文字都可以包含在SongActivity中,只需将歌曲发送到该活动
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String song = l.getItemAtPosition(position).toString(); //this is the selected song
Intent i = new Intent(Menu.this, SongActivity.class);
i.putExtra("Song", song);
startActivity(i);
}