gpt4 book ai didi

java - 单击复选框时显示图像数组列表

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

我有一个整数数组列表,可以从单击的复选框中获取可绘制对象。单击该复选框时无法显示图像。

我尝试使用 example.get 函数来显示图像,但没有成功。

public class MainActivity extends AppCompatActivity {
ImageView rib;
ArrayList<Integer> example = new ArrayList<>();


private CheckBox ribbon1, ribbon2, ribbon3, ribbon4, ribbon5, ribbon6;
private Button button;
private ArrayList<String> selection = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rib = findViewById(R.id.imageView);
button = findViewById(R.id.button);
ribbon1 = findViewById(R.id.checkBox);
ribbon2 = findViewById(R.id.checkBox2);
ribbon3 = findViewById(R.id.checkBox3);
ribbon4 = findViewById(R.id.checkBox4);
ribbon5 = findViewById(R.id.checkBox5);
ribbon6 = findViewById(R.id.checkBox6);
selectItem();
}

public void selectItem(){
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(ribbon1.isChecked()) example.add(R.drawable.ribbon1);
else example.remove(R.drawable.ribbon1);

if(ribbon2.isChecked()) example.add(R.drawable.ribbon2);
else example.remove(R.drawable.ribbon2);

}
});
if(example.size() == 1)
{
rib.setImageResource(example.get(1));
}

}

此处为 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=".MainActivity">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="395dp"
android:layout_height="340dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="8dp">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox"
tools:text="Ribbon 1"
android:onClick="selectItem"/>

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ribbon 2" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ribbon 3" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ribbon 4" />

<CheckBox
android:id="@+id/checkBox5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RIbbon 5" />

<CheckBox
android:id="@+id/checkBox6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ribbon 6" />

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yes"

/>
</LinearLayout>

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout"
app:srcCompat="@drawable/ribbon6" />

</android.support.constraint.ConstraintLayout>

我希望单击该复选框后就会显示图像。

收到错误消息

 2019-06-05 00:57:05.765 15015-15015/com.example.dennis.checkboxribbons E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dennis.checkboxribbons, PID: 15015
java.lang.IllegalStateException: Could not find method selectItem(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatCheckBox with id 'checkBox'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
at android.view.View.performClick(View.java:6256)

最佳答案

在复选框更改监听器上编写代码(setOnCheckedChangeListener),为此请实现 OnCheckedChangeListener

 ribbon1.setOnCheckedChangeListener(this);
ribbon2.setOnCheckedChangeListener(this);

关于java - 单击复选框时显示图像数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56454498/

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