简体   繁体   English

Android 工作室按钮 onClickListener 不工作

[英]Android studio button onClickListener not working

I am relatively new to Android Studio, and I wanted to get a button to simply display a Toast when I tap it, but nothing's happening when I followed this tutorial exactly: https://youtu.be/312RhjfetP8我是 Android Studio 的新手,我想获得一个按钮来在我点击它时简单地显示 Toast,但是当我完全按照本教程进行操作时没有任何反应: https://youtu.be/312RhjfetP8

The onClickListener was greyed out before it became a lambda (whatever that is) onClickListener 在变成 lambda(不管是什么)之前变灰了

here is the main activity这是主要活动



-- MainActivity.java --

package com.treehouse.sqldemo2;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    //references to buttons and other controls in the layout
    //known as "member variables" to the class: they're accessible through all other methods in the class

    //referencing the two buttons
    Button btn_add,btn_viewAll;
    EditText et_name,et_age;
    Switch sw_activeCustomer;
    ListView lv_customerList;
    //these must be placed before the OnCreate method so they can be accessible throughout the page

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //finding the assigned values of the set variables (from the computer)
        btn_add = findViewById(R.id.btn_add);
        btn_viewAll = findViewById(R.id.btn_viewAll);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        sw_activeCustomer = findViewById(R.id.sw_active);
        lv_customerList = findViewById(R.id.lv_customerList);

        //assigning click listeners for each button
        btn_add.setOnClickListener(v -> {
            Toast.makeText(MainActivity.this, "add button", Toast.LENGTH_SHORT).show();
        });
        btn_viewAll.setOnClickListener(v -> {
            Toast.makeText(MainActivity.this, "view all", Toast.LENGTH_SHORT).show();
        });




    }
}


this is the xml file这是 xml 文件




-- activity_main.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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="Customer name"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/et_age"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="Age"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_name" />

    <Switch
        android:id="@+id/sw_active"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="8dp"
        android:text="@string/ActiveCustomer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_age" />

    <Button
        android:id="@+id/btn_viewAll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="View all"
        app:layout_constraintEnd_toStartOf="@+id/btn_add"
        app:layout_constraintHorizontal_bias="0.116"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/sw_active" />

    //setting the button attributes and constraints

    <Button
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/btn_viewAll"
        app:layout_constraintTop_toTopOf="@+id/btn_viewAll" />

    <ListView
        android:id="@+id/lv_customerList"
        android:layout_width="409dp"
        android:layout_height="503dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn_viewAll"/>

</androidx.constraintlayout.widget.ConstraintLayout>

I Have Copy You Code And Its Working Fine.我已经复制了你的代码并且它工作正常。 在此处输入图像描述

在此处输入图像描述

Please Sync Your Gradle Files/Project Goto.请同步您的 Gradle 文件/项目转到。 and Build And Clean And Rebuild Project.和建立、清理和重建项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM