【问题标题】:Modify images on the buttons according to the spinner chosen根据选择的微调器修改按钮上的图像
【发布时间】:2013-11-08 07:02:50
【问题描述】:
我正在开发一个关于可访问性的 android 应用程序,并且需要根据选择的微调器动态加载按钮的图像。附件是一个照片应用程序。大多数照片都有一个空的微调器。单击它时,会出现一个列表,因此选择了微调器,按钮图像会改变。
示例:微调器 - 浴室(这个地方的可能性图片)
Spinner - 厨房(这个地方的可能性图片)。
OBS:我选择不使用imagebutton是为了更容易操作按钮!
代码按钮:
<Button
android:id="@+id/pic1"
android:layout_column="2"
android:layout_columnSpan="3"
android:layout_gravity="left"
android:layout_row="2"
android:drawableBottom="@drawable/pic1"
android:text="@string/pic1" />
Application
【问题讨论】:
标签:
android
image
button
spinner
【解决方案1】:
希望对你有帮助
public class myActivity extends Activity implements OnItemSelectedListener {
int[] images = {R.drawable.image01, R.drawable.image02, R.drawable.image03};
Spinner spinner;
Button button;
...
private void setView(){
button = (Button)findViewById(R.id.button);
spinner = (Spinner)findViewById(R.id.spinner);
// Register the listener
spinner.setOnClickListener(myActivity.this);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Set the background of the button
button.setBackgroundResource(images[pos]);
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
【解决方案2】:
实现一个 OnItemSelectedListener 并使用方法 spinner.setOnItemSelectedListener() 将其附加到您的微调器。
然后,您可以在监听器的 onItemSelected() 函数中设置图像。