Android 为什么不同的按钮具有相同的ID?
大家好, 我正在用Kotlin编写代码,并试图从两个不同的按钮获得不同的响应:Android 为什么不同的按钮具有相同的ID?,android,android-studio,kotlin,Android,Android Studio,Kotlin,大家好, 我正在用Kotlin编写代码,并试图从两个不同的按钮获得不同的响应: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_language_choice) supportActionBar?.setDisplayHomeAsUpEnabled(true) mVis
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_language_choice)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
mVisible = true
// Set up the user interaction to manually show or hide the system UI.
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
val btnCN = findViewById<LinearLayout>(R.id.custom_button_cn)
val btnUK = findViewById<LinearLayout>(R.id.custom_button_uk)
btnCN.setOnClickListener(this)
btnUK.setOnClickListener(this)
println(this)
println(btnCN)
println(btnUK)
}
override fun onClick(v: View?) {
when(v?.id){
R.id.custom_button_cn -> {
println("CHINESE")
println(v)
}
R.id.custom_button_uk -> {
println("ENGLISH")
println(v)
}
else -> {
println("ELSE!!!")
}
}
}
以下是我创建按钮的方式:
<RelativeLayout
android:id = "@+id/rel_lay"
android:layout_width="1280dp"
android:layout_height="1280dp"
android:layout_marginTop="35pt"
>
<LinearLayout
android:id="@+id/custom_button_cn"
android:layout_width="400dp"
android:layout_height="400dp"
android:background="@drawable/china_button_drawable" >
<!-- Image (BG IMG) -->
<ImageView
android:layout_width="400dp"
android:layout_height="400dp"
android:src="@drawable/button_cn" />
</LinearLayout>
<LinearLayout
android:id="@+id/custom_button_uk"
android:layout_width="400dp"
android:layout_height="400dp"
android:background="@drawable/uk_button_drawable" >
<!-- Image UK -->
<ImageView
android:layout_width="400dp"
android:layout_height="400dp"
android:src="@drawable/button_en" />
</LinearLayout>
</RelativeLayout>
我已经检查了代码,没有发现任何错误,但我想我错过了某种Kotlin技巧。为了测试,我尝试删除图像,在图像中添加id,尝试逐层访问,例如,将id提供给相对布局,而不是线性布局,最后是图像,从而访问内部内容。尽管如此,它还是不起作用。请您解释一下,点击按钮并获取每个按钮对应的活动的正确方式是什么?在
RelativeLayout
中,子项按声明顺序排列,除非受到布局约束。您的CN和UK按钮布局没有布局约束,因此UK按钮布局位于CN布局之上,并且由于它完全覆盖了基础布局,因此只有它可以接收单击
通过向布局添加相应的约束,可以使按钮彼此相邻。例如,
android:layout\u-to-endof=“@id/custom\u-button\u-cn”
到custom\u-button\u-uk
,或者将父级RelativeLayout
更改为线性布局在RelativeLayout
中,子级按声明顺序排列,除非受到布局约束。您的CN和UK按钮布局没有布局约束,因此UK按钮布局位于CN布局之上,并且由于它完全覆盖了基础布局,因此只有它可以接收单击
通过向布局添加相应的约束,可以使按钮彼此相邻。例如,android:layout\u-to-endof=“@id/custom\u-button\u-cn”
到custom\u-button\u-uk
,或者将父级RelativeLayout
更改为线性布局假设布局正常(参考@laalto的答案),尝试使用如下匿名侦听器:
btnCN.setOnClickListener { println("CHINESE") }
btnUK.setOnClickListener { println("ENGLISH") }
假设布局正常(ref@laalto的答案),尝试使用匿名侦听器,如下所示:
btnCN.setOnClickListener { println("CHINESE") }
btnUK.setOnClickListener { println("ENGLISH") }
但是你在布局上设置了一个onClick
,而不是在按钮上,这就是你想要的吗?@lieforbanana他将布局用作按钮。。检查他的xml@Anna,我想有一个东西叫做图像按钮,顺便说一句。。为什么使用线性布局作为按钮?@Sam。我知道XML是什么样子,我问这个问题是为了澄清情况,而不是指出什么。但是你在布局上设置了一个onClick
,而不是在按钮上,这就是你想要的吗?@Lieforbanas他使用布局作为按钮。。检查他的xml@Anna,我想有一个东西叫做图像按钮,顺便说一句。。为什么使用线性布局作为按钮?@Sam。我知道XML是什么样子,我问这个问题是为了澄清情况,而不是指出什么。