gpt4 book ai didi

android - GridView 第一项 onClick 不起作用 - 为什么?

转载 作者:行者123 更新时间:2023-11-29 14:12:18 25 4
gpt4 key购买 nike

enter image description here

这应该是一个匹配每个按钮的数字的游戏。将有 12 个不同的数字,数字将设置为随机按钮(2 个按钮将具有相同的数字)。玩家必须点击第一个按钮,按钮的颜色会变成红色,如果玩家再次点击另一个按钮,它会检查它是否有相同的数字,如果是,那么两个按钮都会保持红色。对于按钮,我使用按钮的 GridView 。我将所有按钮设置为黑色,如果我单击该按钮,它会将其颜色更改为红色。按钮显示正确,所有按钮工作正常,除了第一个按钮(在左上角),它没有变红,但它仍然会检查它是否具有相同的数字。

我在 BaseAdapter 上的 getView 代码:

gridView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return buttons.length;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
if(convertView==null) buttons[position] = new Button(activity3.this);
else buttons[position] = (Button)convertView;

if(position==12) Collections.shuffle(numbers);
buttons[position].setText(String.valueOf(numbers.get(position%12)));

buttons[position].setTextSize(0);
buttons[position].setBackgroundColor(Color.BLACK);

buttons[position].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClick && position!=indexClick) {
onClick = false;
String _strClicked = buttons[indexClick].getText().toString();
String _strNow = buttons[position].getText().toString();
if (_strClicked.equals(_strNow)) {
countSuccess++;
buttons[indexClick].setEnabled(false);
buttons[position].setEnabled(false);
buttons[position].setBackgroundColor(Color.RED);
buttons[position].setTextSize(15);
}else {
countFail++;
buttons[indexClick].setBackgroundColor(Color.BLACK);
}

if(countSuccess==11){
Intent intent = new Intent(activity3.this, activity2.class);
intent.putExtra("countFail", String.valueOf(countFail));
startActivity(intent);
finish();
}
} else {
buttons[position].setBackgroundColor(Color.RED);
indexClick = position;
onClick = true;
buttons[position].setTextSize(15);
}
}
});

return buttons[position];
}
});

对于 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".activity3"
android:id="@+id/act3_ConstraintLayout"
>

<GridView
android:id="@+id/gridview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView

android:id="@+id/act3_textViewNICKNAME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toStartOf="parent" />

<TextView
android:id="@+id/act3_textViewNilaiMin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="HOME 26416058"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

我尝试在约束布局上使用 android:descendantFocusability="blocksDescendants",但当我单击它时,左上角的第一个按钮仍然没有变成红色。

我已经搜索了一些引用文献,但它仍然不起作用: Android GridView first button not working OnClickListener not working for first item in GridView

最佳答案

请注意这一行:

 if(position==12) Collections.shuffle(numbers); 
//if the length of buttons array is 12,the max index is 11, not 12.
//position==12 always equals to false.

关于android - GridView 第一项 onClick 不起作用 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53019312/

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