0

我在 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 打开一个新活动。因此,如果我单击位置 2 的项目,我需要进入活动One。是否可以?

当我单击一个项目但不起作用时,我尝试创建一个 toast:

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
      }

我该怎么办?

4

5 回答 5

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);
      }
      }

等等...

于 2014-02-18T16:23:17.800 回答
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
    }

});
于 2014-02-18T16:23:58.667 回答
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
            }

});
于 2014-02-18T16:24:03.267 回答
0

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

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1,
        // your code here
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        // your code here
    }
}); 
于 2014-02-18T16:22:45.660 回答
0

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

于 2014-02-18T16:25:05.393 回答