У меня есть следующий макет, в котором я пытаюсь зарегистрировать OnClickListener для кнопки list_item_setup_step_button_start
. Кнопка получает события касания, но не события кликов. любая помощь приветствуется.Android-кнопка получает событие OnTouch, но не OnClick
<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 который игнорирует событие прикосновения, так что его можно прокручивать только программно), журнал в «Clicked» никогда не появляется, и функция не называется.
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, который не использует сами события. – DarthPaghius
Родительский FrameLayout имеет 'android: clickable =" false "', возможно, это не позволяет ему передать onClick до кнопки. Проверьте, исправляет ли его значение true. – Vucko