これはここでの私の最初の投稿なので、あまりうまくいっていないかもしれません...私はソフトウェア開発の学生で、現在Androidアプリの開発を学んでいます。
4つのチェックボックスを備えたプログラムを作成するように依頼されたので、それらのいずれかをチェックすると、アプリは特定の画像を表示します。例えば。
チェックボックス1:人。
チェックボックス2:車。
チェックボックス3:通り。
チェックボックス4:音楽。
1(人)と2(車)をチェックすると、同じ写真に人と車が表示されているはずです...これについて調べていたところ、この投稿を見つけました。そして、このプログラムを作るのに良い方法だと思いましたが、正しく動作させる方法がわかりません。私はこれをやってみました:
MainActivity.Java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
CheckBox cb1, cb2, cb3, cb4;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = findViewById(R.id.persona);
cb2 = findViewById(R.id.car);
cb3 = findViewById(R.id.calle);
cb4 = findViewById(R.id.music);
img = findViewById(R.id.imagen);
int pattern = (cb1.isSelected() ? 0b0001 : 0)
| (cb2.isSelected() ? 0b0010 : 0)
| (cb3.isSelected() ? 0b0100 : 0)
| (cb4.isSelected() ? 0b1000 : 0);
switch (pattern) {
// No selection
case 0b0000:
img.setImageResource(R.drawable.def);
break;
//Person
case 0b0001:
img.setImageResource(R.drawable.wick);
break;
//Car
case 0b0010:
img.setImageResource(R.drawable.car);
break;
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:id="@+id/rl">
<CheckBox
android:id="@+id/persona"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/persona"
android:layout_marginTop="25dp"
android:checked="false"
android:textSize="30sp"
/>
<CheckBox
android:id="@+id/car"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/car"
android:layout_below="@id/persona"
android:checked="false"
android:textSize="30sp"
/>
<CheckBox
android:id="@+id/calle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calle"
android:layout_below="@id/car"
android:checked="false"
android:textSize="30sp"
/>
<CheckBox
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/music"
android:layout_below="@id/calle"
android:checked="false"
android:textSize="30sp"
/>
<ImageView
android:id="@+id/imagen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/music"
android:layout_centerHorizontal="true"
android:contentDescription="@string/imagen" />
</RelativeLayout>
しかし、アプリケーションを実行すると、xmlで特定のチェックボックスchecked = "true"を作成しても、デフォルトの画像しか表示されません(ケース0b0000のみが機能するようです?)...それぞれに対してonClickイベントを作成してみましたチェックボックスをオンにしますが、パターン変数を正しく使用していないようです。
助けてもらえたらとてもありがたいです... Ifsでできると思いますが、個人的にはその方法に興味があります。
チェックボックスにリスナーをアタッチし、そのパターンチェックをonCheckedChanged()
メソッド内に移動する必要があります。このようにして、チェックボックスはアクティビティを通知し、pattern
次の値を再計算できます。
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox cb1, cb2, cb3, cb4;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = findViewById(R.id.persona);
cb2 = findViewById(R.id.car);
cb3 = findViewById(R.id.calle);
cb4 = findViewById(R.id.music);
img = findViewById(R.id.imagen);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
int pattern = (cb1.isChecked() ? 0b0001 : 0)
| (cb2.isChecked() ? 0b0010 : 0)
| (cb3.isChecked() ? 0b0100 : 0)
| (cb4.isChecked() ? 0b1000 : 0);
switch (pattern) {
// No selection
case 0b0000:
img.setImageResource(R.drawable.def);
break;
//Person
case 0b0001:
img.setImageResource(R.drawable.wick);
break;
//Car
case 0b0010:
img.setImageResource(R.drawable.car);
break;
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加