在按钮中更改图像和文字的问题

时间:2018-10-30 03:20:23

标签: button android-mediaplayer assets

我正在解决一个困扰我的问题。我正在尝试设置一个“播放”按钮,单击该按钮会将“播放”图像更改为“暂停”图像,然后再次返回,以及将标签文本从“播放”更改为“暂停”,然后再次返回。我似乎无法正确编写代码,我想知道大家是否有任何建议?这是我的代码:

这是activity_read.xml文件。我试图将按钮ID设置为“ play_pause”

<?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"
    tools:context="com.example.yhussein.myapplication.ReadActivity"
    android:background="#fcfcfc">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="@dimen/card_image_height"
                android:background="#2d2d2d"
                android:id="@+id/book_img_id"
                android:scaleType="centerCrop"
                android:contentDescription="@string/todo" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/play_pause"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableTop="@drawable/ic_play_arrow_black_24dp"
                    android:text="@string/play"
                    android:textColor="?attr/colorPrimary" />

                <Button
                    android:id="@+id/lang"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableTop="@drawable/ic_language_black_24dp"
                    android:text="@string/lang"
                    android:textColor="?attr/colorPrimary" />

                <Button
                    android:id="@+id/close"
                    style="?android:attr/borderlessButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:drawableTop="@drawable/ic_close_black_24dp"
                    android:text="@string/close"
                    android:textColor="?attr/colorPrimary" />
            </LinearLayout>

            <TextView
                android:id="@+id/txtDesc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/book_title"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="Book Title" />

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

然后,我尝试在ReadActivity中设置“ play_pause setOnClickListener。我尝试设置的代码以粗体显示[我希望]:

 play_pause.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    play_pause.setVisibility(View.GONE);
                    lang.setVisibility(View.GONE);
                    close.setVisibility(View.GONE);
                    bookmark++;
                    if (bookmark > st.size() - 1) {
                        bookmark = st.size() - 1;
                    }
                    section++;
                    if (section > st.size() - 1) {
                        section = st.size() - 1;
                    }
                    img.setImageResource(pixId);
                   /* tvdescription.setText(st.get(bookmark));*/
                    final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), soundId);
                    mp.start();

                    **//Pauses media player when the button is clicked ; changes the drawable and the label text
                    if(mp.isPlaying()){
                        mp.pause();
                        play_pause.setBackgroundResource(R.drawable.ic_pause_black_24dp);
                        play_pause.setText(R.string.Pause);

                    } else {
                        mp.start();
                        play_pause.setBackgroundResource(R.drawable.ic_play_arrow_black_24dp);
                        play_pause.setText(R.string.Play);
                    }**

                    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        public void onCompletion(MediaPlayer mp) {
                            finish(); // finish current activity
                            Toast.makeText(getApplicationContext(), "End of sound!", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });

程序运行时,这是我单击“播放”按钮时发生的情况: enter image description here

有人知道我在做什么错吗?

0 个答案:

没有答案