gpt4 book ai didi

android - 按钮不引起 onClick 事件

转载 作者:行者123 更新时间:2023-12-04 17:12:24 25 4
gpt4 key购买 nike

我正在开发一个应用程序(学校项目的一部分),我有一个 Activity ,它由一个 ImageView、一个 SearchView、一个 ListView 和一个 Button 组成。

我试图将按钮放在 ListView 的前面,在我真正让按钮做某事之前,它一直在工作。设置 onClick 方法后,我注意到该按钮没有执行任何操作。我尝试使用按钮打印一些东西,但没有任何效果。还尝试在 ListView 之外创建一个新按钮,但这也失败了(也没有导致 onClick 事件)。

我有点无能,想不出任何可能导致这个问题的原因......

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/main_color"
android:orientation="vertical"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:layout_gravity="center"
tools:context=".SetupActivity">

<ImageView
android:layout_width="300dp"
android:layout_gravity="center"
android:layout_height="130dp"
android:layout_marginTop="13dp"
android:background="@drawable/setup_text"
android:id="@+id/setupText"
></ImageView>

<androidx.appcompat.widget.SearchView
android:layout_width="match_parent"
android:layout_height="65dp"
android:id="@+id/searchViewSetup"
android:theme="@style/AppTheme.Toolbar"
app:iconifiedByDefault="false"
></androidx.appcompat.widget.SearchView>

<RelativeLayout
android:layout_width="wrap_content"
android:paddingHorizontal="8dp"
android:layout_height="wrap_content">
<ListView
android:id="@+id/list_item"
android:layout_gravity="center"
android:layout_width="395dp"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:layout_height="wrap_content"
tools:listitem="@layout/activity_ingredients_layout"
android:paddingBottom="10.0sp"
android:clipToPadding="false"
android:scrollbars="none"
android:paddingTop="10.0sp">
</ListView>

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/setup_btn_shadow"
android:layout_marginLeft="287dp"
android:layout_marginTop="450dp"
android:alpha="0.5"
></ImageView>

<android.widget.Button
android:layout_width="73dp"
android:id="@+id/setup_floating_btn"
android:layout_height="73dp"
android:layout_marginLeft="300dp"
android:layout_marginTop="460dp"
android:foregroundGravity="bottom"
android:src="@drawable/ic_baseline_arrow_forward_ios_24"
android:background="@drawable/setup_btn"
></android.widget.Button>
</RelativeLayout>



</LinearLayout>

Java:

package com.example.yummilyproject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;

import com.example.yummilyproject.databinding.ActivitySetupFridgeBinding;

import java.util.ArrayList;

public class SetupActivity extends AppCompatActivity implements View.OnClickListener {

ActivitySetupFridgeBinding binding;

SearchView searchView;
ListView listView;
Button btn;

String[] ingredients = {"Avocado", "Tomato", "Pasta", "Cauliflower", "Egg", "Salmon", "Chicken", "Beef", "Broccoli", "Cheese", "Zucchini", "Lemon", "Sweet Potato", "Kale", "Carrot", "Black Beans", "Asparagus",
"Spinach", "Rice", "Potato", "Yoyo Beans"};
Boolean[] checkboxes = {false, false, false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
int[] images = {R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_setup_fridge);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
listView = findViewById(R.id.list_item);
searchView = findViewById(R.id.searchViewSetup);
btn = (Button) findViewById(R.id.setup_floating_btn);
btn.bringToFront();
btn.setOnClickListener(this);


binding = ActivitySetupFridgeBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());



ArrayList<Ingredient> ingredientArrayList = new ArrayList<>();
for (int i = 0; i < images.length; i++) {
Ingredient ingredient = new Ingredient(ingredients[i], images[i], checkboxes[i]);
ingredientArrayList.add(ingredient);
}

ListAdapter listAdapter = new ListAdapter(SetupActivity.this, ingredientArrayList);
binding.listItem.setTextFilterEnabled(true);
binding.listItem.setAdapter(listAdapter);
binding.listItem.setClickable(true);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
listAdapter.getFilter().filter(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
listAdapter.getFilter().filter(newText);
return false;
}
});


binding.listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Ingredient ig = (Ingredient) parent.getItemAtPosition(position);
ig.setIngredientCheckBox(!ig.isIngredientCheckBox());
CheckBox cb = view.findViewById(R.id.ingredientChekcBox);
cb.setChecked(!cb.isChecked());
//cb.setChecked(!cb.isChecked());
}
});


}

@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
if (v == findViewById(R.id.setup_floating_btn))
{
System.out.println("press btn");
String[] ingredientsList = new String[ingredients.length];
int cnt = 0;
for (int i = 0; i<ingredients.length; i++)
{
if (checkboxes[i] == true)
{
ingredientsList[cnt] = ingredients[i];
cnt++;
}
}
System.out.println(ingredientsList);
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("ingredientsList", ingredientsList);

}
}

private AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.8F);


public class Ingredient
{
private String ingredientTitle;
private int ingredientPhoto;
private boolean ingredientCheckBox;

public Ingredient(String ingredientTitle, int ingredientPhoto, boolean ingredientCheckBox) {
this.ingredientTitle = ingredientTitle;
this.ingredientPhoto = ingredientPhoto;
this.ingredientCheckBox = ingredientCheckBox;
}

public String getIngredientTitle() {
return ingredientTitle;
}

public void setIngredientTitle(String ingredientTitle) {
this.ingredientTitle = ingredientTitle;
}

public int getIngredientPhoto() {
return ingredientPhoto;
}

public void setIngredientPhoto(int ingredientPhoto) {
this.ingredientPhoto = ingredientPhoto;
}

public boolean isIngredientCheckBox() {
return ingredientCheckBox;
}

public void setIngredientCheckBox(boolean ingredientCheckBox) {
this.ingredientCheckBox = ingredientCheckBox;
}
}

public class ListAdapter extends ArrayAdapter<Ingredient>
{
public ListAdapter (Context context, ArrayList<Ingredient> ingredientList)
{
super(context, R.layout.activity_ingredients_layout, ingredientList);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Ingredient ingredient = getItem(position);
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_ingredients_layout, parent, false);
}

ImageView imageView = convertView.findViewById(R.id.ingredientPhoto);
TextView title = convertView.findViewById(R.id.ingredientTitle);
CheckBox cb = convertView.findViewById(R.id.ingredientChekcBox);
imageView.setImageResource(ingredient.ingredientPhoto);
title.setText(ingredient.ingredientTitle);
cb.setActivated(ingredient.ingredientCheckBox);


return convertView;
}
}

public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}


}

最佳答案

你使用平等的方式是错误的。您不能使用 == 来查看两个对象是否彼此相等。

如果你真的想使用 equals 来验证它是同一个对象,你应该这样做:

if (v.equals(findViewById(R.id.setup_floating_btn)) {
// Do whatever you need to do when the button is clicked here.
}

虽然这可行,但它不是验证是否点击了 View 的推荐方法。相反,您应该像这样检查被点击的 ViewID:

if (v.getId() == R.id.setup_floating_btn) {
// Do whatever you need to do when the button is clicked.
}

看看 equals== 之间的区别 here .

旁注:您在这里将东西混合在一起 - ViewBindings 与正常的 findViewById 这真的很糟糕并且让事情变得非常难以阅读。 ViewBindings 旨在更轻松地膨胀屏幕中的所有 View ,以及使对 View 的所有引用都是类型安全的,并确保没有 View 为 null。您可能也应该阅读此内容。

同样在您的 XML 中,您使用 sp 作为 dividerHeightpadding 的单位,这是完全错误的。 sp 单位仅用于文本,不用于 View 的高度或填充。

另外看看 bringToFront()View 上做了什么,看看这是否真的应该应用/用于您的 Button .

关于android - 按钮不引起 onClick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69210010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com