我怎样才能使多种活动是一项活动只​​改变琴弦? [英] How can I make multiple activities be in one activity changing only strings?

查看:160
本文介绍了我怎样才能使多种活动是一项活动只​​改变琴弦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含在它的菜单一个ListView,我想被点击后链接到一个活动具有相同的布局风格,但里面的内容也应有所不同的应用程序。

I have an app that contains a ListView in its menu which I want to link to one activity with the same layout style but the content inside should be different after being clicked on.

的的ListView包含的歌曲列表,并点击时的个别项目的布局包含标题(TextView的),歌词(TextView的),一个next_button按钮,一个previous_button按钮,歌曲之间移动,一个back_button按钮返回到ListView和一个play_tune按钮,效力于该特定歌曲的音频。并非所有的歌曲包含音频,但这里的play_tune按钮的谎言,我想应用程序图标ic_launcher-web.png而不是显示那里。

The ListView contains a list of songs, and the layout of an individual item when clicked contains a Title (TextView), Lyrics (TextView), a "next_button" button, a "previous_button" button to move between songs, a "back_button" button to get back to the ListView and a "play_tune" button which plays audio for that specific song. Not all of the songs contain audio, but where the "play_tune" button lies, I would like the app icon ic_launcher-web.png showing there instead.

我想显示(与音频歌)的XML文件,如下所示:

The XML file I would like to show (for a song with audio) is shown below:

<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文件如下所示:

My current SongActivity.java file is shown below:

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() {}
}

和我Menu.java为我的ListView是在这里:

And my Menu.java for my ListView is here:

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);
    }

}

我所有的标题和歌词的是目前在我的价值观\\ strings.xml档案。

All of my Titles and Lyrics are currently in my Values\strings.xml file.

我怎样才能让我的所有歌曲是在一个SongActivity类的ListView被选中后?我相信,如果你有不止一次写东西出来,应该做一个简单的方法。

How can I make all my songs be in one "SongActivity" class after being selected in ListView? I believe that if you have to write anything out more than once, there should be an easier way to do it.

编辑:我怎样才能让 MediaPlayer的MP = MediaPlayer.create(SongActivity.this,调); 字符串调整= i.getStringExtra( 调); 共同努力

How can I make MediaPlayer mp = MediaPlayer.create(SongActivity.this, Tune); and String Tune = i.getStringExtra("Tune"); work together?

推荐答案

在选择列表项,调用与意图演员,如SONGNAME,你需要其他的PARAMS SongActivity。然后阅读并相应地填充字段。

When selecting a list item, call your SongActivity with extras in the intent, such as SongName and other params you need. Then read it and populate the fields accordingly.

onItemSelected方式:

onItemSelected method:

Intent i = new Intent(this, SongActivity.class);
i.putExtra("songName", "Song name here"); //get song name from the item selected
....
startActivity(i);

在您SongActivity类:

In your SongActivity class:

onCreate(...){
    Intent i = getIntent();
    String songName = i.getStringExtra("songName");
    ...
}

编辑:这种方式,所有其他固定的文字可以包含在SongActivity,只要发送歌曲给活动

this way all other fixed text can be included in SongActivity, just send the song to that activity

@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);
}

这篇关于我怎样才能使多种活动是一项活动只​​改变琴弦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆