英文:
My button on clicked is not working in Kotlin Android Studio
问题
以下是您要的翻译部分:
I am facing a problem which is my button ID is well-configured and in my main activity, I am calling the button using the correct ID too, but after I run the project on the emulator, the button is not working after I clicked on it for multiple times.
Can anyone help me to solve the problem?
This is my xml file:
我遇到一个问题,我的按钮ID已经配置正确,在我的主活动中,我也使用了正确的ID来调用按钮,但在模拟器上运行项目后,按钮在多次点击后不起作用。
有人可以帮助我解决这个问题吗?
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/bgColor"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="20dp"
android:text="Mobile Quiz Application"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:cardCornerRadius="10dp"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Welcome To The Quiz App!"
android:textColor="@color/txtTitle"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Please enter your name before starting the quiz."
android:textColor="@color/txtDesc"
android:textSize="15sp" />
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name......"
android:inputType="textCapWords"
android:textColor="@color/txtTitle"
android:textColorHint="@color/txtDesc" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_Start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Continue"
android:textStyle="bold">
</Button>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
这是我的主文件:
package com.example.quizapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnStart = findViewById<Button>(R.id.btn_Start)
val nameInput = findViewById<EditText>(R.id.etName)
btnStart.setOnClickListener {
Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
}
英文:
I am facing a problem which is my button ID is well-configured and in my main activity, I am calling the button using the correct ID too, but after I run the project on the emulator, the button is not working after I clicked on it for multiple times.
Can anyone help me to solve the problem?
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/bgColor"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="20dp"
android:text="Mobile Quiz Application"
android:textColor="@color/white"
android:textSize="30sp"
android:textStyle="bold" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
app:cardCornerRadius="10dp"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Welcome To The Quiz App!"
android:textColor="@color/txtTitle"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Please enter your name before starting the quiz."
android:textColor="@color/txtDesc"
android:textSize="15sp" />
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your name......"
android:inputType="textCapWords"
android:textColor="@color/txtTitle"
android:textColorHint="@color/txtDesc" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_Start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="Continue"
android:textStyle="bold">
</Button>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
This is my main file:
package com.example.quizapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnStart = findViewById<Button>(R.id.btn_Start)
val nameInput = findViewById<EditText>(R.id.etName)
btnStart.setOnClickListener {
Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
}
答案1
得分: 1
findViewById是一个老的方法。
我建议您使用ViewBinding。
点击这里查看文档
英文:
findViewById is an old method.
i would recommend you to use with ViewBinding.
click here to documentation
答案2
得分: 1
你可以使用 viewBinding
在 build.gradle 中添加以下行:
buildFeatures{
viewBinding true
}
然后更新你的活动(activity)如下:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnStart.setOnClickListener{
Toast.makeText(this@MainActivity, "你点击了我。", Toast.LENGTH_SHORT).show()
}
}
}
英文:
You can use viewBinding
add line to build.gradle
buildFeatures{
viewBinding true
}
and update your activity like this
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.btnStart.setOnClickListener{
Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论