0
我有2個兩個圖像(item1.png和item2.png),我想在單擊微調項目上的項目時在ImageView中顯示每個圖像。當選擇微調控制器中的「item1」時,我想顯示「item1.png」圖像和「item2」相同的圖像。更改ImageView基於點擊的微調項目它不工作
我試圖用下面的代碼做到這一點,但它沒有工作。你知道什麼是問題嗎?
在MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner2);
String string = String.valueOf(spinner.getSelectedItem());
final ImageView image = (ImageView)findViewById(R.id.image);
int image1 = R.drawable.item1;
int image2 = R.drawable.item2;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//spinner.
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2) {
if (arg2 == 0) {
image.setImageResource(R.drawable.item1);
} else {
image.setImageResource(R.drawable.item2);
}
}
}
}
}
活動的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="com.example.ricardorei.tpc.MainActivity">
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="48dp"
android:text="Button"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="199dp"
android:layout_marginRight="58dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginEnd="58dp" />
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="220dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:srcCompat="@drawable/item1" />
<TextView
android:id="@+id/textView2"
android:layout_width="321dp"
android:layout_height="28dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:text="Are Awesome!"
app:layout_constraintBottom_toTopOf="@+id/imageView2"
app:layout_constraintHorizontal_bias="0.517"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="295dp"
android:layout_height="28dp"
android:text="TextView"
app:layout_constraintBaseline_toBaselineOf="@+id/button3"
android:layout_marginLeft="32dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="32dp" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="134dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:entries="@array/spinner_values"
app:layout_constraintHorizontal_bias="0.522"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
你知道如何設置圖像invisiblity當的情況下0那就是該商品- 選擇一個商品
被選中?我使用image.setVisibility(View.INVISIBLE),但它不工作 –
JonD