如何在Android中使用onItemSelected?

Jos*_*ble 36 android spinner

package org.example.mbtiapplication;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MBTITest extends Activity implements OnItemSelectedListener  {

    private Spinner firstSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mbtitest);

        Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        firstSpinner.setAdapter(adapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        // TODO Auto-generated method stub

    }

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

XML布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center_vertical"
            android:text="I like to go out more than staying home." />

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="120dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center_vertical"
            android:textSize="10.5dp"
            android:text="Sensing v Intuition" />

        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

我是一个新的Android程序员,使用Spinner时遇到了麻烦,我尝试了多个教程但仍感到困惑.我想知道我的下一步是什么,据我所知,我已经用XML设置了我的微调器,用Java我已经确定了微调器,为所述微调器创建了一个ArrayAdapter,并指定了一些选项.我不确定我是否已经填充了微调器,或者如何使用微调器对象进行操作.我希望能够使用微调器对象选择三个选项之一,然后将该值保留在微调器内的textview内.

bgs*_*gse 49

你快到了.如您所见,onItemSelected将为您提供一个position参数,您可以使用它来从适配器中检索对象,如getItemAtPosition(position).

例:

spinner.setOnItemSelectedListener(this);

...

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(parent.getContext(), 
        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
        Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)

这将在屏幕上显示一条消息,其中所选项目由其toString()方法打印.

  • 你能给我举个例子吗?在过去的几个小时里,这一直困扰着我。 (2认同)

Azu*_*pot 27

如果你不想实现监听器,你可以直接在你想要的地方设置它(你的适配器设置完成调用你的微调器):

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case 0:
                        Toast.makeText(parent.getContext(), "Spinner item 1!", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(parent.getContext(), "Spinner item 2!", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(parent.getContext(), "Spinner item 3!", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

               // sometimes you need nothing here
            }
        });
Run Code Online (Sandbox Code Playgroud)


小智 17

另一件事:当您的布局中有多个微调器时,您必须在onItemSlected()方法中实现一个开关选择,以了解单击了哪个窗口小部件.像这样的东西:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (parent.getId()){
        case R.id.sp_alarmSelection:
            //Do something
            Toast.makeText(this, "Alarm Selected: " + parent.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
            break;
        case R.id.sp_optionSelection:
            //Do another thing 
            Toast.makeText(this, "Option Selected: " + parent.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 11

对于 Kotlin 和绑定,代码是:

binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            }
        }
Run Code Online (Sandbox Code Playgroud)