我是Android開發新手,我嘗試創建包括微調控件(3項)的simple application
。在不同的微調選項上顯示不同圖像
我的目標是,to show a different image for each spinner that is selected
。
在一些教程的幫助下,我設法得到下面的代碼,但我不知道如何將圖像綁定到微調項。
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/Stockwerk"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Plan der 8. Etage"
/>
</LinearLayout>
</RelativeLayout>
main_activity.java
package com.example.raumplan;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Spinner;
public class MainActivity extends Activity {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection() {
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
CustomOnItemSelectedListener.java
package com.example.raumplan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Toast;
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString()+" ausgewählt",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
我真的很感謝你們的幫助:D 謝謝
編輯: 感謝您的幫助。 我試圖添加以下內容,但我不知道,用什麼來替換「urImageView」。
switch (pos) {
case 0:
urImageView.setImageResource(R.drawable.x);
break;
case 1:
urImageView.setImageResource(R.drawable.y);
break;
case 2:
urImageView.setImageResource(R.drawable.z);
break;
default:
break;
}
在我activity_main.xml中我
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Plan der 8. Etage"
/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Plan der 7. Etage"
/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="Plan der 6. Etage"
/>