2

私は 8 つの Screenns を持っています。そのために 8 つのアクティビティを用意しました。最初のアクティビティで、IstアクティビティからIInd On Image Buttonに切り替えるには、このコードを指定しました

public void onClick(View v) { 
Intent myIntent = new Intent(v.getContext(), Activity2.class);
     v.getContext().startActivity(myIntent);
});
2 番目のアクティビティから 3 番目のアクティビティに、3 番目のアクティビティから 4 番目のアクティビティに、というように切り替えるにはどうすればよいですか。

Plsは私を助けてくれます。

4

2 に答える 2

7

以下にそれを行う1つの方法を示します。この例では、画面に 3 つのボタンを配置します。これらは、私が定義して XML ファイルに配置したボタンです。3 つの異なるボタンのいずれかをクリックすると、対応するアクティビティに移動します。

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

      // Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such.     
    Button b1 =(Button)findViewById(R.id.b1);
    Button b2 =(Button)findViewById(R.id.b2);
    Button b3 =(Button)findViewById(R.id.b3);

// Setup the listeners for the buttons, and the button handler      
    b1.setOnClickListener(buttonhandler);
    b2.setOnClickListener(buttonhandler);   
    b3.setOnClickListener(buttonhandler);
}           
    View.OnClickListener buttonhandler=new View.OnClickListener() { 

   // Now I need to determine which button was clicked, and which intent or activity to launch.         
      public void onClick(View v) {
   switch(v.getId()) { 

 // Now, which button did they press, and take me to that class/activity

       case R.id.b1:    //<<---- notice end line with colon, not a semicolon
          Intent myIntent1 = new Intent(yourAppNamehere.this, theNextActivtyIwant.class);
    YourAppNameHere.this.startActivity(myIntent1);
      break;

       case R.id.b2:    //<<---- notice end line with colon, not a semicolon
          Intent myIntent2 = new Intent(yourMainAppNamehere.this, AnotherActivtyIwant.class);
    YourAppNameHere.this.startActivity(myIntent2);
      break;  

       case R.id.b3:  
                Intent myIntent3 = new Intent(yourMainAppNamehere.this, a3rdActivtyIwant.class);
    YourAppNameHere.this.startActivity(myIntent3);
      break;   

       } 
    } 
};
   }

基本的に、私たちはそれを設定するためにいくつかのことを行っています。ボタンを特定し、XML レイアウトから取得します。それぞれにどのように ID 名が割り当てられているかを確認してください。例による r.id.b1 は私の最初のボタンです。

次に、ボタンのクリックをリッスンするハンドラを設定します。次に、どのボタンが押されたかを知る必要があります。スイッチ/ケースは「if then」のようなものです。ボタン b1 を押すと、コードはそのボタンのクリックに割り当てたものに移動します。b1 (ボタン 1) を押すと、割り当てた「インテント」またはアクティビティに移動します。

これが少し役立つことを願っています。役に立つ場合は、答えに投票することを忘れないでください。私は自分自身でこのことを始めたばかりです。

ありがとう、

于 2010-11-13T03:31:15.023 に答える
0

以下のURLのコードスニペットを使用して、開発者ガイドのフラグを確認してみましょう。

アンドロイド; あるアクティビティで状態を初期化してから、別のアクティビティで更新するにはどうすればよいですか?

于 2010-10-04T15:09:46.333 に答える