0

スピナーから値を選択したときに画面遷移を取得しようとしています。私のスピナーには2つの値しかありません。デフォルトでは 1 番目が選択されています。私が望むのは、スピナーの 2 番目の値をクリックすると、新しい画面に移動することです。

助けてください!

前もって感謝します!

4

3 に答える 3

1

スピナーのonItemSelectedListenerを使用します。これがデモです、

public class AndroidSpinner extends Activity implements OnItemSelectedListener {

 TextView selection;
 Spinner spin;
 String[] items = { "bangladesh", "bangla", "bd", "australia", "japan",
   "china", "indiaA", "indiaC" };

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);


  Spinner spin = new Spinner(this);
  setContentView(spin);

  spin.setOnItemSelectedListener(this);

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

  spin.setAdapter(aa);
 }

 @Override
 public void onItemSelected(AdapterView<?> parent, View v, int position,
   long id) {
  // Do your Stuff Here
   Intent intent = new Intent(MyActivity.this, NextActivity.class);
        startActivity(intent);

 }

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

 }
}
于 2012-09-08T11:00:32.083 に答える
0

スピナーのitemselectedイベントで新しいアクティビティまたは新しい画面を開くには、以下のコードを使用します。

public class SpinnerExample extends Activity implements OnItemSelectedListener { 

    String[] items = { "Dipak", "Aadi", "Bharat", "Pratik", "Usha", "Jayesh", "Deep", "Imran" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Spinner mSpn1 = (Spinner) findViewById(R.id.mSpn1);
        mSpn1.setOnItemSelectedListener(this);
        ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
        mSpn1.setAdapter(adpt);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        // Do your Stuff Here
        // For Open New Activity
        Intent mInNewAccount = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(mInNewAccount);
        finish();

        // For Open New Screen
        setContentView(R.layout.second_screen);
    }

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

    }
}
于 2012-09-08T11:07:59.307 に答える
0

スピナーアダプターとすべてのセットアップ方法を知っていると仮定すると、スピナーで他のアイテムが選択されたときに次の画面に進むためのコードを追加するだけです。

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) 
{
  if(position ==0)
         //do nothing (assuming that you would want to stay in the same screen)
  else if (position ==1)
     {
         //go to the next screen, probably by using an INTENT to the next activity or using setContentView(theLayoutXmlFile)
     }
 }
于 2012-09-08T11:08:21.090 に答える