如何创建在单击时播放不同声音的按钮列表视图

How to create a ListView of Buttons that play different sounds on click

我需要用一个 ListView 制作一个 Activity,它可以有超过 50 个 ImageButton,每个 ImageButton 播放不同的声音。

这是主要的 activity(会有按钮):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"

    tools:context="com.werewolve.freaksound.sounds"
    android:background="@drawable/f_background_fit">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:src="@drawable/f_logo" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/soundList"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp" />
</RelativeLayout>

这是我为列表视图的每一行自定义的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/list_item_string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:textSize="18sp"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/play_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#00000000"
        android:src="@drawable/f_button_s"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp" />

</RelativeLayout>

每个按钮将通过 onClick "play_btn" 播放不同的声音,并根据声音播放带有字符串 "list_item_string".

的文本

示例:


*(笑声)*(播放按钮)***


解决方案是创建一个自定义 ListView 对象,该对象具有用于显示按钮名称的 TextViewImageButton。然后,您可以使用自定义 Adapter 将这些对象添加到您的 ListViewImageButton 的替代方法是简单地使用常规图像并使其成为 Clickable。可以在此处找到创建可以显示自定义对象的 ListView 的一个很好的示例(包括如何创建自定义 Adapter):http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/

您应该像这样创建自定义适配器:

public class PlaySoundsAdapter extends BaseAdapter implements View.OnClickListener {

    SoundExample[] sounds;
    Activity context;
    PlaySoundAlert soundPlayerAlert;

    public PlaySoundsAdapter(Activity context, SoundExample[] soundsArray) {
        this.context = context;
        this.sounds = soundsArray;

        // Hooks up the PlaySoundAlert.PlaySound in MainActivity
        this.soundPlayerAlert = (PlaySoundAlert)context;
    }

    @Override
    public int getCount() {
        return sounds == null ? 0 : sounds.length;
    }

    @Override
    public Object getItem(int i) {
        return sounds[i];
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        SoundExample item = (SoundExample)getItem(i);

        if (view == null) // reuse existing view
            view = context.getLayoutInflater().inflate(R.layout.custom_sound_layout,
                    viewGroup, false);

        // Set the TextView to the name of the sound
        TextView t = (TextView)view.findViewById(R.id.txtName);
        t.setText(item.getSoundName());

        // Set the tag of the button to the sound resource id (uri)
        Button b = (Button)view.findViewById(R.id.play_btn);
        b.setTag(item.getSoundUri());

        // When the button is clicked, play the associated sound
        b.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        Button b = (Button) view;
        if (b != null) {
            int soundUri = (int)b.getTag();

            // Notify listener (MainActivity) to play the required sound
            if (soundPlayerAlert != null) {
                soundPlayerAlert.playSound(soundUri);
            }
        }
    }
}

接下来创建一个您可以在 activity 中实现的接口来播放这样的声音:

public interface PlaySoundAlert {
    public void playSound(int uri);
}

如您所见,我在上面创建的 Adapter 使用此接口触发事件以播放所需的声音。

您的 SoundExample class 可能喜欢这样的内容:

public class SoundExample {

    private int soundUri;
    private String soundName;

    public String getSoundName() {
        return soundName;
    }

    public void setSoundName(String soundName) {
        this.soundName = soundName;
    }

    public int getSoundUri() {
        return soundUri;
    }

    public void setSoundUri(int soundUri) {
        this.soundUri = soundUri;
    }
}

要在 ActivityFragment 中使用它,请使用以下内容:

public class MainActivity extends Activity implements PlaySoundAlert {

    ListView lstSounds;
    PlaySoundsAdapter soundsAdapter;
    SoundExample[] mySounds;

    // Media player for playing sounds
    MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lstSounds = (ListView) findViewById(R.id.soundList);

        // Create sound list & add two SoundExample objects
        mySounds = new SoundExample[2];
        SoundExample s1 = new SoundExample();
        SoundExample s2 = new SoundExample();

        // Set sound one to a beep
        s1.setSoundName("Beep Sound");
        s1.setSoundUri(R.raw.beep);

        // Set sound two to an applause sound
        s2.setSoundName("Applause Sound");
        // NOTE: I am using a sound titled applause.mp3 inside a folder called "raw"
        s2.setSoundUri(R.raw.applause);

        // Add sounds to the list
        mySounds[0] = s1;
        mySounds[1] = s2;

        // Instantiate the adapter and apply to the ListView
        soundsAdapter = new PlaySoundsAdapter(this, mySounds);
        lstSounds.setAdapter(soundsAdapter);
    }

    @Override
    public void playSound(int uri) {
        // Play sound
        mp = MediaPlayer.create(this, uri);
        if (!mp.isPlaying())
            mp.start();
    }
}

这就是您所需要的!