Android Kotlin:Mobile:setOnClickListener必须双击所有按钮,而不是单击一次
我目前正在学习如何结合一些基本java教程编写代码,然后将该过程转换为Kotlin。我已经做了一个应用程序,它可以做3件事,它发送一个字符串到第二个屏幕上,在一个句子中使用,最后它关闭应用程序。然而,按钮需要两次按下,我不知道为什么 下面是我的xml和kt文件。任何意见都会有帮助 Manifest.xmlAndroid Kotlin:Mobile:setOnClickListener必须双击所有按钮,而不是单击一次,android,kotlin,Android,Kotlin,我目前正在学习如何结合一些基本java教程编写代码,然后将该过程转换为Kotlin。我已经做了一个应用程序,它可以做3件事,它发送一个字符串到第二个屏幕上,在一个句子中使用,最后它关闭应用程序。然而,按钮需要两次按下,我不知道为什么 下面是我的xml和kt文件。任何意见都会有帮助 Manifest.xml 主要活动.kt package com.gmail.launch 导入android.content.Intent 导入android.net.Uri 导入androidx.appcomp
主要活动.kt
package com.gmail.launch
导入android.content.Intent
导入android.net.Uri
导入androidx.appcompat.app.appcompat活动
导入android.os.Bundle
导入android.view.view
导入android.widget.Button
导入android.widget.EditText
导入android.widget.TextView
导入kotlinx.android.synthetic.main.activity\u main*
导入org.w3c.dom.Text
类MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage(视图:视图){
val nameb=findviewbyd(R.id.namebtn)
val namet=findviewbyd(R.id.nametext)
nameb.setOnClickListener{
val intent=intent(这是launch2::class.java){
putExtra(“com.gmail.launch.name”,namet.text.toString())
}
星触觉(意图)
}
}
趣味openGoogle(查看:查看){
val gglB=findViewById(R.id.gglbtn)
gglB.setOnClickListener{
val googleURL=Intent(android.content.Intent.ACTION\u视图)
googleURL.data=Uri.parse(“https://www.google.com/")
startActivity(谷歌网址)
}
}
趣味关闭按钮(视图:视图){
val closeB=findViewById(R.id.closeB)
closeB.setOnClickListener{
有限性
}
}
}
MainActivity.xml文件
启动2.kt
package com.gmail.launch
导入androidx.appcompat.app.appcompat活动
导入android.os.Bundle
导入android.widget.TextView
导入org.w3c.dom.Text
类启动2:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_launch2)
val nameE=intent.getStringExtra(“com.gmail.launch.name”)
val txtV=findviewbyd(R.id.txtV)
txtV.text=nameE.plus(“是个超级书呆子!”)
}
}
Launch2.xml
迈克·M.在上面找到了答案,但看起来他的答案不可标记为答案
您混合了两种不同的方法来设置OnClickListeners。在布局XML中使用android:onClick属性,或者在setContentView()之后的onCreate()中的代码中使用setOnClickListener()。不要两者都做-Mike M.您混合了两种不同的方法来设置
OnClickListener
s。在布局XML中使用android:onClick
属性,或者在onCreate()
中的setContentView()之后的代码中使用setOnClickListener()
。不要两者兼而有之,上面所说的是完全正确的。我现在已经注释掉了“onClickListener”,它立即解决了这个问题。非常感谢你。奇怪的是,我发现了一种设置双击要求的复杂方法,这种方法永远不应该被使用。这就是答案,我只是希望我能把它标记成这样。谢谢你,迈克M。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gmail.launch">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".launch2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.gmail.launch
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import org.w3c.dom.Text
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun sendMessage (view: View){
val nameb = findViewById<Button>(R.id.namebtn)
val namet = findViewById<EditText>(R.id.nametext)
nameb.setOnClickListener {
val intent = Intent(this, launch2::class.java).apply {
putExtra("com.gmail.launch.name",namet.text.toString())
}
startActivity(intent)
}
}
fun openGoogle (view: View) {
val gglB = findViewById<Button>(R.id.gglbtn)
gglB.setOnClickListener {
val googleURL = Intent(android.content.Intent.ACTION_VIEW)
googleURL.data = Uri.parse("https://www.google.com/")
startActivity(googleURL)
}
}
fun closeButton (view: View) {
val closeB = findViewById<Button>(R.id.CloseB)
closeB.setOnClickListener {
finishAffinity()
}
}
}
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/gglbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="openGoogle"
android:text="Open Google"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/namebtn" />
<Button
android:id="@+id/namebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nametext" />
<EditText
android:id="@+id/nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/namebtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/CloseB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="157dp"
android:layout_marginLeft="157dp"
android:layout_marginTop="11dp"
android:layout_marginEnd="156dp"
android:layout_marginRight="156dp"
android:onClick="closeButton"
android:text="Close App"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/gglbtn" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.gmail.launch
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import org.w3c.dom.Text
class launch2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_launch2)
val nameE = intent.getStringExtra("com.gmail.launch.name")
val txtV = findViewById<TextView>(R.id.txtV)
txtV.text = nameE.plus(" is a giant nerd!")
}
}
<?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"
tools:context=".launch2">
<TextView
android:id="@+id/txtV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>