我有一个包含三个 TextView 的布局,两个 TextView 正在响应单击事件,但最后一个没有响应。我正在使用导航导航到其他片段。OnClick 方法在实现 view.OnClickListener 的适配器内部调用。
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_marginStart="@dimen/defaultMargin"
android:layout_height="wrap_content">
<TextView
android:text="How CADO works"
style="@style/CircularBook"
android:drawableEnd="@drawable/arrow_right"
android:layout_width="0dp"
android:layout_height="@dimen/buttonHeight"
android:id="@+id/howCadoWorks"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="@id/howCadoWorks"
android:layout_width="match_parent"
android:background="@color/pinkishGray"
android:layout_height="0.5dp"
android:id="@+id/view3"></View>
<TextView
android:text="FAQ"
style="@style/CircularBook"
android:layout_width="0dp"
android:drawableEnd="@drawable/arrow_right"
android:layout_height="@dimen/buttonHeight"
android:id="@+id/faqs"
app:layout_constraintTop_toBottomOf="@+id/view3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<View
app:layout_constraintTop_toBottomOf="@id/faqs"
android:layout_width="match_parent"
android:background="@color/pinkishGray"
android:layout_height="0.5dp"
android:id="@+id/view4"></View>
<TextView
android:text="Contact Us"
android:drawableEnd="@drawable/arrow_right"
style="@style/CircularBook"
android:layout_width="0dp"
android:layout_height="@dimen/buttonHeight"
android:id="@+id/contactus"
app:layout_constraintTop_toBottomOf="@+id/view4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
单击方法
public void onClick(@Nullable View view) {
NavController var3 = Navigation.findNavController(view);
if(view.getId()==R.id.contactus){
**var3.navigate(R.id.contactus_fragment);**
return;
}
else if (view.getId()==R.id.faqs){
var3.navigate(R.id.faqFragmnt);
return;
}
else if (view.getId()==R.id.howCadoWorks){
Toast.makeText(view.getContext(),"Feature is in Progress", Toast.LENGTH_SHORT).show();
// var3.navigate(R.id.contactus_fragment);
return;
}
else
{
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了多种方法,也重新检查了 id,但没有任何效果。
正如@Nilesh Rathod所说,您需要确保所有文本视图都设置了一个单击侦听器,例如:
textView.setOnClickListener(this);
Run Code Online (Sandbox Code Playgroud)
或者您也可以:
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Here you put the code you want to use for the specific textView click event
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |