【问题标题】:Android Button receives OnTouch event but not OnClickAndroid Button 接收 OnTouch 事件但不接收 OnClick
【发布时间】:2017-04-08 09:44:28
【问题描述】:

我有以下布局,我正在尝试为按钮list_item_setup_step_button_start 注册一个 OnClickListener。 Button 接收触摸事件但没有点击事件。任何帮助表示赞赏。

<RelativeLayout
    android:id="@+id/list_item_setup_step_layout_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/list_item_setup_step_label"
        android:layout_width="@dimen/list_item_setup_state_label_width"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@color/activity_setup_step_state_next_color"
        android:clickable="false"/>

    <FrameLayout
        android:id="@+id/list_item_setup_step_frame_layout_progress"
        android:layout_width="@dimen/list_item_setup_step_progress_bar_size"
        android:layout_height="match_parent">

        <com.mikhaellopez.circularprogressbar.CircularProgressBar
            android:id="@+id/list_item_setup_step_progress_bar"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:cpb_background_progressbar_color="#FFCDD2"
            app:cpb_progressbar_color="#F44336"
            android:visibility="invisible"
            android:layout_gravity="center_vertical|center_horizontal"/>

        <Button
            android:id="@+id/list_item_setup_step_button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="@dimen/text_margin_normal"
            android:background="@color/color_accent_default"
            android:textColor="@color/text_color_light"
            android:visibility="invisible"
            android:clickable="true"
            android:textSize="@dimen/text_size_normal"
            android:layout_marginLeft="@dimen/text_margin_normal"/>

    </FrameLayout>

    <LinearLayout
        android:layout_toLeftOf="@id/list_item_setup_step_label"
        android:layout_toRightOf="@id/list_item_setup_step_frame_layout_progress"
        android:layout_marginRight="@dimen/text_margin_normal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/list_item_setup_step_text_view_title"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="16sp"
            android:gravity="right|center_vertical"
            android:textColor="@color/text_color_light"/>

        <TextView
            android:id="@+id/list_item_setup_step_text_view_status"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="right|center_vertical"
            android:textColor="@color/text_color_light" />

    </LinearLayout>

</RelativeLayout>

这是监听器的代码(我有一个自定义的 ScrollView,它忽略触摸事件,因此只能以编程方式滚动),“点击”日志永远不会出现,也不会调用函数。

private void handleStartButton() {
    currentStepStartButton = (Button) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_button_start);
    currentStepStartButton.setTypeface(BaseActivity.getFont(this));
    final TextView status = (TextView) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_text_view_status);
    progressBar = (CircularProgressBar) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_progress_bar) ;
    progressBar.setProgress(0);
    StyleHelper.applyStyle(this, progressBar);
    status.setTypeface(BaseActivity.getFont(this));
    if(currentStep == 0 && currentPhase == 0) {
        status.setText(getString(R.string.activity_setup_press_button_to_begin));
        currentStepStartButton.setText(getString(R.string.commons_start));
    } else {
        status.setText(getString(R.string.activity_setup_press_button_to_continue));
        currentStepStartButton.setText(getString(R.string.commons_start));
    }
    currentStepStartButton.setVisibility(View.VISIBLE);
    currentStepStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e(Constants.DEBUG_TAG, "Clicked") ;
            currentStepStartButton.setVisibility(View.INVISIBLE);
            handleCurrentStepAction();
        }
    });
}

编辑:

在设置监听器的方法之前调用了以下方法:

private void requestPermissions() {
        int currentAPIVersion = android.os.Build.VERSION.SDK_INT ;
        if (currentAPIVersion >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    android.Manifest.permission.SEND_SMS,
                    android.Manifest.permission.RECEIVE_SMS
            }, 1);
        }
    }

删除此方法后,OnClickListener 再次开始工作。我真的不知道为什么。有什么理论吗?

【问题讨论】:

  • 这是在 ListView 中吗?那么这就是问题所在。 ListView 在到达按钮之前消耗点击
  • @cricket_007 正如问题中提到的,它在一个不消耗事件本身的自定义 ScrollView 中。
  • 父 FrameLayout 有android:clickable="false",也许这不会让它将 onClick 传递给按钮。检查是否将其设置为 true 修复它。
  • @Vucko 可点击设置为 false 的 FrameLayout 不是相关按钮的父级。无论如何,我删除了它,它仍然是一样的。
  • 哦,我明白了。我误以为那 2 FrameLayouts :D 嗯,然后很有趣...

标签: android onclicklistener


【解决方案1】:

您的其他代码似乎有一些干扰(在此问题中不可见),因为如果您提供的代码,按钮单击方法有效,除了那些不可见的部分,被插入到一个新项目中。

Java:

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

    final Button currentStepStartButton = (Button) findViewById(R.id.list_item_setup_step_button_start);
    currentStepStartButton.setVisibility(View.VISIBLE);
    currentStepStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("MainActivity", "Clicked");
            currentStepStartButton.setVisibility(View.INVISIBLE);
        }
    });
}

XML:

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

<RelativeLayout
    android:id="@+id/list_item_setup_step_layout_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/list_item_setup_step_frame_layout_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/list_item_setup_step_button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:visibility="invisible"
            android:clickable="true"/>

    </FrameLayout>

</RelativeLayout>
</LinearLayout>

尝试暂时禁用不必要的功能,以便按钮在问题上归零。

【讨论】:

  • 我做到了,并且不知何故找到了问题的根源,但我不知道它为什么以及如何影响按钮。请检查已编辑的问题。
【解决方案2】:

好吧,我在处理另一个项目时遇到了同样的问题,并发现这里真正出了什么问题。

正如问题本身所建议的那样,问题源于使用requestPermissions 方法。尽管在其尝试获取的权限已被授予的情况下调用此方法可能似乎不会显示当前活动的对话框,但似乎无论如何添加了一些不可见的覆盖层,这些覆盖层似乎也是可点击的。

因此,解决方案是始终检查一个人是否已经拥有试图获取的权限,并且仅在尚未授予这些权限的情况下启动requestPermissions。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2011-12-07
    • 2015-12-15
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多