0

我尝试使用微调器控件结果将其指向应用程序上的另一个屏幕。例如,在微调器控件中,如果用户选择 chevy,它将带您进入另一个屏幕,该屏幕在 chevy.xml 和 Chevy.class 中编码。这就是我目前所拥有的并且需要一些帮助,因为我们的书只使用了 getSelectedItem 并且该示例仅用于输出语句。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;          

 public class Mainpage extends Activity implements OnItemSelectedListener {
    String carChoice, chevy, ford, dodge, toyota;

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

  /*  carChoice = group.getSelectedItem().toString();
    }

     if (carChoice.compareTo(chevy)==0)
     {
     startActivity(new Intent(Mainpage.this, Chevy.class));
  */ }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        final Spinner group = (Spinner) findViewById(R.id.carGroup);
        group.setOnItemSelectedListener(this);
        // TODO Auto-generated method stub
         String selected = group.getItemAtPosition(1).toString();           
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
 }
4

3 回答 3

1

为了

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
      long arg3)

arg2 是选中项的索引,arg3 是选中项的 id。您可以调用 getItem() 方法来获取选定的项目对象。

于 2012-12-06T04:44:41.540 回答
0
public class SpinnerExample extends Activity
       {
                Spinner sp;
                ArrayAdapter<String> adapter;
                String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
                                                "EIGHT", "NINE", "TEN" };

                public void onCreate(Bundle savedInstanceState)
                      {

                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                sp = (Spinner) findViewById(R.id.spinner1);

                                adapter = new ArrayAdapter<String>(this,
                                                                android.R.layout.simple_spinner_item, numbers);
                                sp.setAdapter(adapter);

        sp.setOnItemSelectedListener(new OnItemSelectedListener()
        {
      public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)                                                    
                    {
       Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(),
        Toast.LENGTH_LONG).show();                                                 
                        }
                 public void onNothingSelected(AdapterView<?> arg0)
                    {
                      // TODO Auto-generated method stub                 
                    }
       });
     }
}

sp.getSelectedItem().toString() 获取项目保存在这里

于 2012-12-06T04:55:06.147 回答
0

请参阅下面的代码以获取 Android 中微调器的完整示例。

public class ArrayAdapterDemo2 extends Activity implements
        AdapterView.OnItemSelectedListener {
    TextView selection;
    String[] items = { "Dipak", "Keshariya", "Software", "Deepak", 
            "Android Example", "Android Book",
            "Android", "i-phone", "Developer", "Tutorial" };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        selection = (TextView) findViewById(R.id.selection);

        Spinner spin = (Spinner) findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);

        ArrayAdapter aa = new ArrayAdapter(
                this,
                android.R.layout.simple_spinner_item, 
                items);

        aa.setDropDownViewResource(
           android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(aa);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        // Here Position is Item Index
        selection.setText(spin.getSelectedItem().toString());
    }

    public void onNothingSelected(AdapterView<?> parent) {
        selection.setText("");
    }
}

有关更多信息,请参见下面的链接。

Android 微调器

于 2012-12-06T05:08:17.673 回答