Android浮动操作按钮OnClickListener不工作

z9smfwbn  于 2023-01-28  发布在  Android
关注(0)|答案(1)|浏览(188)

我是一个非常新的Android,因为我上周开始了一个学校项目。我想实现一个计时器从本教程(https://www.youtube.com/watch?v=xtElLuzjA0U第2部分),我必须翻译成java,因为它在Kotlin。
我的问题是:当我点击我的“播放”按钮时没有任何动作发生。我试图实现一个OnClick侦听器,因为它不起作用,所以只是layout.xml中的一个“OnClick”方法,但它们都不起作用。
我的activity_timer. xml文件:

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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=".presentation.TimerActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <android.support.constraint.ConstraintLayout 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"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context=".presentation.TimerActivity"
            tools:showIn="@layout/activity_timer">
        <TextView
            android:id="@+id/txtCountDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="10:00"
            android:textSize="70sp"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <me.zhanghai.android.materialprogressbar.MaterialProgressBar
            android:id="@+id/progress_countdown"
            style="@style/Widget.MaterialProgressBar.ProgressBar"
            android:minWidth="306dp"
            android:minHeight="306dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/startbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="64dp"
            app:srcCompat="@drawable/icon_play" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/pausebtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center"
            android:layout_margin="64dp"
            app:srcCompat="@drawable/icon_pause" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/stopbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|start"
            android:layout_margin="64dp"
            app:srcCompat="@drawable/icon_stop" />
    </android.support.design.widget.CoordinatorLayout>

我的www.example.com类上的OnCreate方法TimerActivity.java如下所示:

public class TimerActivity extends AppCompatActivity{

        private CountDownTimer timer;
        private Long timerLengthSeconds = 0L;
        private Long secondsRemaning = 0L;
        private TimerState timerState = TimerState.STOPPED;
        private FloatingActionButton fab_start;
        private FloatingActionButton fab_pause;
        private FloatingActionButton fab_stop;
        private ProgressBar progress_countdown;
        private TextView textView_countdown;
        private Toolbar toolbar;

        public enum TimerState{
            STOPPED, PAUSED, RUNNING
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_timer);
            textView_countdown = findViewById(R.id.txtCountDown);
            progress_countdown = findViewById(R.id.progress_countdown);

            fab_start = (FloatingActionButton) findViewById(R.id.startbtn);
            fab_start.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Log.e("start Cick", "Tu as clické sur Start");
                    startTimer();
                    Log.e("startTimer", "Le timer est starté");
                    timerState = RUNNING;
                    Log.e("RUNNING", "Enum a Running");
                    updateButtons();
                    Log.e("updateButtons", "Update the Buttons");
                }
            });

            fab_pause = (FloatingActionButton) findViewById(R.id.pausebtn);
            fab_pause.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Log.e("pause Cick", "Tu as clické sur Pause");
                    timer.cancel();
                    timerState = TimerState.PAUSED;
                    updateButtons();
                }
            });

            fab_stop = (FloatingActionButton) findViewById(R.id.stopbtn);
            fab_stop.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Log.e("pause Stop", "Tu as clické sur Stop");
                    timer.cancel();
                    onTimerFinished();
                }
            });
        }

一个非常奇怪的事情是,当我点击播放按钮,没有发生任何事情(没有在日志中显示他传递的方法),但当我点击暂停按钮,应用程序崩溃(因为启动未初始化),我可以看到我的日志,它的工作.
对此有什么建议吗?非常感谢您的帮助。

eyh26e7m

eyh26e7m1#

您的constraintLayout刚好与按钮重叠。您必须在此constraintLayout中移动按钮,onClick才能正常工作。

<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".presentation.TimerActivity"
    tools:showIn="@layout/activity_timer">

    <TextView
        android:id="@+id/txtCountDown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:textSize="70sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="10:00" />

    <me.zhanghai.android.materialprogressbar.MaterialProgressBar
        android:id="@+id/progress_countdown"
        style="@style/Widget.MaterialProgressBar.ProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:minHeight="306dp"
        android:minWidth="306dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/startbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="64dp"
        app:srcCompat="@drawable/icon_play" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/pausebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:layout_margin="64dp"
        app:srcCompat="@drawable/icon_pause" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/stopbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="64dp"
        app:srcCompat="@drawable/icon_stop" />
</android.support.constraint.ConstraintLayout>

相关问题