单击spinner元素android

时间:2014-02-18 16:18:08

标签: java android android-spinner onitemclick

我在main.xml中创建了一个微调器:

<Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/categoria_arrays"
        android:prompt="@string/categoria_prompt" />

在strings.xml中,值为:

<string name="categoria_prompt">Choose</string>
   <string-array name="categoria_arrays">
        <item>All</item>
        <item>One</item>
        <item>Two</item>
        <item>Three</item>
    </string-array>

我可以正常显示但实际上没有交互。我需要onClick over a item打开一个新活动,例如。因此,如果我点击位置2处的项目,我需要进入活动One。有可能吗?

当我点击某个项目但没有效果时,我试图创建一个祝酒词:

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        Toast.makeText(parent.getContext(), 
            "OnItemSelectedListener : " + 
                                   parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
      //HERE CHANGE ACTIVITY
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
      }

我怎么办?

5 个答案:

答案 0 :(得分:1)

是的,这是可能的。这样做

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        if (pos == 1){
                Intent i = new Intent(currentActivity.this, One.class);
                startActivity(i);

        }else if (pos == 2)
        {
                Intent i = new Intent(currentActivity.this, Two.class);
                startActivity(i);

        }else if (pos == 3){
                Intent i = new Intent(currentActivity.this, Three.class);
                startActivity(i);
      }
      }

依旧......

答案 1 :(得分:1)

您需要像这样设置项目选择的侦听器:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // your code here
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

答案 2 :(得分:1)

在setContentView:

之后,只需在你的onCreate中调用类似的东西
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
                Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_SHORT).show();
                //HERE CHANGE ACTIVITY

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

});

答案 3 :(得分:0)

您需要为微调器显式设置侦听器。

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1,
        // your code here
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        // your code here
    }
}); 

答案 4 :(得分:0)

您刚刚添加了onItemSelected方法,还是设置了onItemSelectListener? onItemSelected和onNothingSelected只是方法,你需要使用onItemSelectListener。