android - 为什么 SetOnClickListener 并不总是有效?
问题描述
我发现有时当我单击视图时 setOnClickListener 不起作用。当我运行应用程序并单击视图时,它没有响应,没有错误消息它只是不起作用。
在项目的另一部分 setOnClickListener 运行良好,我找不到任何区别。即使代码相同,我也不知道为什么它不起作用。
这是我的活动文件。
class SplashActivity : AppCompatActivity() {
// 애니메이션을 처리하기 위한 runnable 객체
private val mRunnable : Runnable = Runnable {
if (!isFinishing) { // 끝나지 않았을 때
// 슬라이드업 애니메이션 실행
slideUp(illo_logo, 500)
fadeIn(illo_copy, 500)
slideUp(illo_copy, 500)
fadeIn(btn_login, 500)
slideUp(btn_login, 500)
fadeIn(btn_sign, 500)
slideUp(btn_sign, 500)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
// 핸들러 이용해 0.5초 딜레이 후 mRunnable 실행
Handler().postDelayed(mRunnable, 500)
// 로그인 버튼
btn_login.setOnClickListener {
Toast.makeText(this@SplashActivity, "로그인 버튼 클릭", Toast.LENGTH_SHORT).show()
val intent = Intent(this@SplashActivity, LoginActivity::class.java)
startActivity(intent)
finish()
}
// 가입 버튼
btn_sign.setOnClickListener {
Toast.makeText(this@SplashActivity, "가입 버튼 클릭", Toast.LENGTH_SHORT).show()
val intent = Intent(this@SplashActivity, SignActivity0::class.java)
startActivity(intent)
finish()
}
}
// 위로 올리기
fun slideUp(view : View, time : Int) {
val animation = TranslateAnimation(0f, 0f, 0f, -300f ) // 애니메이션 인스턴스화
animation.duration = time.toLong() // 애니메이션 지속시간 설정
animation.fillAfter = true // 애니메이션 종료 후 상태 유지
view.startAnimation(animation) // 애니메이션 실행
}
// 페이드 인
fun fadeIn(view: View, time: Int) {
val anim = AnimationUtils.loadAnimation(this@SplashActivity, R.anim.fade_in) // 애니메이션 로드
anim.duration = time.toLong() // 애니메이션 지속시간
anim.fillAfter = true // 애니메이션 종료 후 상태 유지
view.startAnimation(anim) // 애니메이션 실행
}
}
这是我的 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/illo_base_color"
tools:context=".SplashActivity">
<ImageView
android:id="@+id/illo_logo"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_marginBottom="250dp"
android:src="@drawable/app_icon_default"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/illo_copy"
android:layout_width="141dp"
android:layout_height="26dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="206dp"
android:fontFamily="@font/tmoneyroundwind_extrabold"
android:letterSpacing="0"
android:text="우리는 illo 모임"
android:textColor="@color/illo_light_purple"
android:textSize="20sp"
android:textStyle="normal"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/illo_logo"
app:layout_constraintStart_toStartOf="@+id/illo_logo"
app:layout_constraintTop_toBottomOf="@+id/illo_logo"
app:layout_constraintVertical_bias="0.0" />
<RelativeLayout
android:id="@+id/btn_login"
android:layout_width="312dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="24dp"
android:background="@drawable/button_long"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/illo_copy">
<TextView
style="@style/IlloKrTitle1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="로그인하기"
android:textColor="@color/illo_white100"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/btn_sign"
android:layout_width="312dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:background="@drawable/button_long"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_login">
<TextView
style="@style/IlloKrTitle1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="가입하기"
android:textColor="@color/illo_white100"
android:textSize="16dp" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
在您的小部件上添加android:clickable = "true"
和android:focusable = "true"
,例如:
<RelativeLayout
android:id="@+id/btn_login"
android:layout_width="312dp"
android:layout_height="48dp"
android:layout_marginStart="24dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="24dp"
android:clickable="true"
android:focusable="true"
android:background="@drawable/button_long"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/illo_copy">
额外:添加此属性android:foreground="?attr/selectableItemBackground"
该代码在 RelativeLayout 上添加可选动画,但如果您在 android:background 属性上添加,效果会更好。与任何小部件一起尝试并玩得很好!;)
推荐阅读
- python - 从 google.cloud 导入语言 ModuleNotFoundError:没有名为“google.cloud”的模块
- javascript - jQuery按键不适用于简单的必应搜索
- scala - 线程“主”java.lang.NoClassDefFoundError 中的异常:org/rogach/scallop/ScallopConf
- javascript - 无法返回对象数组属性的总和
- r - 使用ggplot在绘图图中对散点进行双重注释
- python - 如何将 JSON 字符串保存到字典中?
- javascript - 在 PHP 中读取 Json 请求
- tensorflow - Keras flow_from_directory() 只从选定的子目录中读取
- node.js - 接收 TypeError:AWS.RDSDataService 不是构造函数
- java - 是否必须消耗自动更新的android应用内订阅?