Android按钮收到OnTouch事件,但不是OnClick [英] Android Button receives OnTouch event but not OnClick
本文介绍了Android按钮收到OnTouch事件,但不是OnClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下布局,我正在尝试为list_item_setup_step_button_start
按钮注册OnClickListener。按钮接收触摸事件,但不接收点击事件。如有任何帮助,我们不胜感激。
<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
将重新开始工作。我真的不知道为什么。有什么理论吗?
推荐答案
我在处理另一个项目时遇到了同样的问题,并找出了这里真正的问题所在。
问题本身提出的问题源于requestPermissions
方法的使用。尽管在试图获取的权限已被授予的情况下调用此方法可能看起来不会在当前活动上显示对话框,但似乎无论如何都会添加一些看不见的覆盖,这似乎也是可单击的。
因此,解决方案是始终检查用户是否已拥有要尝试获取的权限,并仅在尚未授予这些权限的情况下启动requestPermissions
。
这篇关于Android按钮收到OnTouch事件,但不是OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文