2012-03-06 128 views
0

我有以下代碼,我需要填寫if(pos == 1){??????????},以便我可以在選擇微調控制器中的第一個位置時切換活動。我的新靈活性名稱是Route1.java。 如何在我的微調器中選擇位置1時編寫切換到Route1.java的代碼。使用微調器切換活動

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.Toast; 
import android.Manifest.permission; 

public class MyOnItemSelectedListener extends Activity 
     implements OnItemSelectedListener { 

    public void onItemSelected(AdapterView<?> parent, 
      View view, int pos, long id) { 
     if (pos == 1) { 
     } else { 
      Toast.makeText(parent.getContext(), 
        "Your route is " 
        + parent.getItemAtPosition(pos).toString(), 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void onNothingSelected(AdapterView parent) { 
     // Do nothing. 
    } 
} 

上述類此類用於

import java.lang.reflect.Array; 
import android.R.array; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.webkit.WebView; 

import android.widget.AbsSpinner; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.widget.Toast; 


public class cnycentro extends Activity {   

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.cnycentro); 

     Spinner spinner = (Spinner) findViewById(R.id.spinner); 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       this, R.array.routes, android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(adapter); 

     spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 
    } 
} 

回答

0

您應該意圖和更具體明確的意圖閱讀起來。

爲了明確地推出新的活動(比如一個創建)的語法是:

Intent newActivity = new Intent(this, Route1.class); 
startActivity(newActivity); 

還要確保新的活動添加到Android清單:

<activity android:name=".Route1"></activity> 

編輯:

+1在您的聽衆不延長活動,而不是簡單地寫

public class MyOnItemSelectedListener implements OnItemSelectedListener { 
    .... 
} 

如果您的活動或監聽器太大,並且會降低可讀性以將其內聯編寫,則可以通過此方式進行編寫。

0
  1. MyOnItemSelectedListener()不需要是單獨的類,它絕對不應該從Activity繼承。
  2. 更改您的代碼是這樣的:

    OnItemSelectedListener listener = new OnItemSelectedListener(){ 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, 
         int position, long id) { 
        //do you really want 1 here or 0? 
        if (position == 1) { 
         view.getContext().startActivity(new Intent(view.getContext().getApplicationContext(),Route1.class)); 
        }else{ 
         Toast.makeText(parent.getContext(), "Your route is " + 
            parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show(); 
    
        } 
    } 
    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 
    
    } 
    }; 
    spinner.setOnItemSelectedListener(listener);