【问题标题】:Show different images on different spinner selections在不同的微调器选择上显示不同的图像
【发布时间】:2016-09-06 01:22:27
【问题描述】:

我是 Android 开发新手,我正在尝试创建 simple application,包括微调器(3 个条目)。

我的目标是,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>

ma​​in_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"
    />

【问题讨论】:

    标签: java android android-layout imageview spinner


    【解决方案1】:

    您可以简单地在onItemSelected() 中添加switch 语句

    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();
    
        switch (pos) {
            case 0:
               break;
            default:
               break;
        }
    }
    

    【讨论】:

      【解决方案2】:

      创建带有文本和图像的微调器有一些步骤。

      Steps :
        1.  Create Model (SpinnerModel.java) to store data for each spinner row.
        2.  Create a ArrayList to store Model (SpinnerModel.java) objects.
        3.  Store data in Models and Store Model objects in Arraylist.
        4.  Pass Model object Arraylist to custom adapter.
        5.  Custom Adapter use Arraylist data (Model Objects) and create rows for Spinner.
        6.  Create listener for Spinner and show spinner item selected values on activity.
      
      
      This link given below will help you.
      

      Custom Spinner With Image And Text

      【讨论】:

        【解决方案3】:

        您也可以使用 if else 条件来使用 show multiple image on selction。像这样的。

        spinner = (Spinner) findViewById(R.id.spinner1);
        imageview = (ImageView) findViewById(R.id.imageView1);
        
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            //spinner.
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                if(arg2==0)
                {
                    imageview.setImageResource(R.drawable.apple);
                }
                else if(arg2==1)
                {
                    imageview.setImageResource(R.drawable.microsoft);
                }
                else
                {
                    imageview.setImageResource(R.drawable.google);
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多