我的按钮在点击时在Kotlin Android Studio中不起作用。

huangapple go评论63阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;@color/bgColor&quot;
    android:orientation=&quot;vertical&quot;
    android:gravity=&quot;center&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;TextView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:gravity=&quot;center&quot;
        android:layout_marginBottom=&quot;20dp&quot;
        android:text=&quot;Mobile Quiz Application&quot;
        android:textColor=&quot;@color/white&quot;
        android:textSize=&quot;30sp&quot;
        android:textStyle=&quot;bold&quot; /&gt;

    &lt;com.google.android.material.card.MaterialCardView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;20dp&quot;
        android:layout_marginEnd=&quot;20dp&quot;
        app:cardCornerRadius=&quot;10dp&quot;
        android:background=&quot;@color/white&quot;&gt;

        &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_margin=&quot;5dp&quot;
            android:padding=&quot;10dp&quot;
            android:orientation=&quot;vertical&quot;&gt;

            &lt;TextView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;Welcome To The Quiz App!&quot;
                android:textColor=&quot;@color/txtTitle&quot;
                android:textSize=&quot;25sp&quot;
                android:textStyle=&quot;bold&quot; /&gt;

            &lt;TextView
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:gravity=&quot;center&quot;
                android:text=&quot;Please enter your name before starting the quiz.&quot;
                android:textColor=&quot;@color/txtDesc&quot;
                android:textSize=&quot;15sp&quot; /&gt;

            &lt;com.google.android.material.textfield.TextInputLayout
                style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginTop=&quot;16dp&quot;&gt;

                &lt;androidx.appcompat.widget.AppCompatEditText
                    android:id=&quot;@+id/etName&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:hint=&quot;Your name......&quot;
                    android:inputType=&quot;textCapWords&quot;
                    android:textColor=&quot;@color/txtTitle&quot;
                    android:textColorHint=&quot;@color/txtDesc&quot; /&gt;
            &lt;/com.google.android.material.textfield.TextInputLayout&gt;

            &lt;Button
                android:id=&quot;@+id/btn_Start&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_marginTop=&quot;15dp&quot;
                android:text=&quot;Continue&quot;
                android:textStyle=&quot;bold&quot;&gt;
            &lt;/Button&gt;
        &lt;/LinearLayout&gt;
    &lt;/com.google.android.material.card.MaterialCardView&gt;
&lt;/LinearLayout&gt;

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&lt;Button&gt;(R.id.btn_Start)
        val nameInput = findViewById&lt;EditText&gt;(R.id.etName)
        btnStart.setOnClickListener {
            Toast.makeText(this@MainActivity, &quot;You clicked me.&quot;, 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, &quot;You clicked me.&quot;, Toast.LENGTH_SHORT).show()
    }
}

huangapple
  • 本文由 发表于 2023年2月6日 14:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定