1

嗨,我需要在微调器上单击一下。这是我的微调器:

Spinner spinner = (Spinner) findViewById(R.id.spnTarghe);
final String targhe[]=risposta.split("/");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this,
        android.R.layout.simple_spinner_item,
        targhe
        );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

我该怎么做才能使用执行此简单指令的 onclicklistener:

        sTarga=targhe[position];
4

2 回答 2

7

通过使用 onitemselected 侦听器,您可以在微调器中选择项目

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            String  mselection=spinner.getSelectedItem().toString();    
            Toast.makeText(getApplicationContext(), "selected "+ mselection, 30).show();              
            /**** do your code*****/
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        //  
        }
    });
于 2012-05-30T13:23:00.007 回答
2

使用 onItemSelected

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

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

});
于 2012-05-30T13:22:27.947 回答