【问题标题】:Android FloatingActionButton OnClickListener doesn't workAndroid FloatingActionButton OnClickListener 不起作用
【发布时间】:2018-10-21 10:44:43
【问题描述】:

我是 Android 的新手,因为我上周开始了一个学校项目。 我想从本教程 (https://www.youtube.com/watch?v=xtElLuzjA0U part2) 中实现一个计时器,我必须在 Java 中进行翻译,因为它在 Krotlin 中。

我的问题:当我点击“播放”按钮时,没有任何动作发生。我尝试实现一个 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>

我的 TimerActivity.java 类的 OnCreate 方法是这样的:

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

很奇怪的是,当我点击播放按钮时,什么都没有出现(日志中没有任何内容表明他传入了方法),但是当我点击暂停按钮时,应用程序崩溃了(因为启动时没有初始化) 我可以在我的日志中看到它有效。

对此有什么建议吗? 非常感谢您的帮助。

【问题讨论】:

  • 请贴上错误日志,调试起来会更方便
  • 我试过你的代码。它工作正常。

标签: android onclicklistener floating-action-button


【解决方案1】:

您的约束布局只是与按钮重叠。您必须在此约束布局中移动您的按钮,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>

【讨论】:

  • 它仍然不起作用。抱歉,我没有日志,因为没有任何操作。我不知道我是否创建了另一个错误,但是将按钮放置在 constraintLayout 中会将所有按钮放在左上角。我添加了约束以使它们出现在底部。
  • @Roman 你解决过这个问题吗?我的浮动操作按钮也没有点击
猜你喜欢
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多