0

好的,我有一个微调器,这是我的微调器

List<String> list = new ArrayList<String>();
list.add("SPBU");
list.add("HOTEL");

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_spinner_item, list);
      dataAdapter
      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spiner.setAdapter(dataAdapter);

我想创建 OnItemSelectedListener 方法,所以我可以检测选择是否改变。问题是我不知道如何实现,我不明白在方法中使用哪些参数(arg0、View arg1、int arg2、long arg3)。这是我到目前为止所尝试的

public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // my code here

            }
4

2 回答 2

2

如果您不知道方法是如何工作的,那么检查文档总是一个好主意。无论如何,这将帮助您入门;)

import java.util.ArrayList;
import java.util.List;

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

public class MyActivity extends Activity implements OnItemSelectedListener {

    List<String> list = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);

        list.add("SPBU");
        list.add("HOTEL");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataAdapter);
        spinner.setOnItemSelectedListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }


    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        Toast.makeText(getApplicationContext(), "Clicked on: " + list.get(position), Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        //do something
    }

}

编辑:您还可以创建一个匿名类来处理事件:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

            }

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

            }
        });
于 2013-11-05T14:23:25.467 回答
2

您需要为微调器添加一个侦听器,该侦听器将在选择项目时调用“onItemSelected”方法。这是一些示例代码:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
         //stuff here to handle item selection      
     }

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

     }
});
于 2013-11-05T14:27:07.463 回答