從一個Activity跳到另外一個Activity或是Layout

摘要:從一個Activity跳到另外一個Activity或是Layout

一個Activity裡面有兩個Button,這兩個Button按下去均會跳出一個AlertDialog。

 

按下第一個Button跳出的畫面。

按下Cancel會關閉Dialog,不會有任何動作。按下OK會跳到另一個Layout。

按下返回鍵會跳回原本的Activity。再按下第二個Button,會跳出Dialog。

按下OK會跳到另一個Activity。

按下返回鍵會跳回原本的Activity。

 

《程式碼》

架構:有兩個Activity(java檔)與三個.xml檔(包含main本身)。

 

AndroidManifest.xml檔裡面多一個Activity。

 

第一個Activity的程式碼:

package tw.nkfust.jason;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class AlertDialogSampleActivity extends Activity {

      /** Called when the activity is first created. */

      Button btn1;

      Button btn2;

 

      @Override

      public void onCreate(Bundle savedInstanceState) {

             super.onCreate(savedInstanceState);

             setContentView(R.layout.main);

             setMainControl();

      }

 

      private void setMainControl() {

             // TODO Auto-generated method stub

             btn1 = (Button) findViewById(R.id.button1);

             btn1.setOnClickListener(new Button.OnClickListener() {

                   public void onClick(View v) {

                          // TODO Auto-generated method stub

                          show(1);

                   }

             });

             btn2 = (Button) findViewById(R.id.button2);

             btn2.setOnClickListener(new Button.OnClickListener() {

                   public void onClick(View v) {

                          // TODO Auto-generated method stub

                          show(2);

                   }

             });

      }

 

      private void show(int i) {

             // TODO Auto-generated method stub

             switch (i) {

             case 1:

                   new AlertDialog.Builder(AlertDialogSampleActivity.this)

                                .setTitle("陳偉殷")

                                .setMessage("確定要投下您神聖的一票?")

                                .setPositiveButton("OK",

                                             new DialogInterface.OnClickListener() {

                                                   public void onClick(DialogInterface dialog,

                                                                int which) {

                                                          // TODO Auto-generated method stub

                                                          //跳到另一個layout

                                                          jumptochen();

                                                   }

                                             })

                                .setNegativeButton("Cancel",

                                             new DialogInterface.OnClickListener() {

                                                   public void onClick(DialogInterface dialog,

                                                                int which) {

                                                          // TODO Auto-generated method stub

                                                   }

                                             }).show();

                   break;

             case 2:

                   new AlertDialog.Builder(AlertDialogSampleActivity.this)

                                .setTitle("達比修有")

                                .setMessage("確定要投下您神聖的一票?")

                                .setPositiveButton("OK",

                                             new DialogInterface.OnClickListener() {

                                                   public void onClick(DialogInterface dialog,

                                                                int which) {

                                                          // TODO Auto-generated method stub

                                                          //跳到另一個activity

                                                          jumptodarvish();

                                                   }

                                             })

                                .setNegativeButton("Cancel",

                                             new DialogInterface.OnClickListener() {

                                                   public void onClick(DialogInterface dialog,

                                                                int which) {

                                                          // TODO Auto-generated method stub

                                                   }

                                             }).show();

                   break;

             default:

                   break;

             }

      }

 

      protected void jumptodarvish() {

             // TODO Auto-generated method stub

             Intent intent = new Intent();

             intent.setClass(AlertDialogSampleActivity.this, darvish.class);

             startActivity(intent);

             AlertDialogSampleActivity.this.finish();

      }

 

      protected void jumptochen() {

             // TODO Auto-generated method stub

             setContentView(R.layout.chen);

             Button btnchen = (Button) findViewById(R.id.button1);

             btnchen.setOnClickListener(new Button.OnClickListener() {

                   public void onClick(View v) {

                          // TODO Auto-generated method stub

                          setContentView(R.layout.main);

                          setMainControl();

                   }

             });

      }

}

 

 

 

 

 

 

第二個Activity的程式碼:

package tw.nkfust.jason;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class darvish extends Activity {

    /** Called when the activity is first created. */

      Button btn1;

     

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.darvish);

       

        btn1 = (Button) findViewById(R.id.button1);

        btn1.setOnClickListener(new Button.OnClickListener(){

                   public void onClick(View v) {

                          // TODO Auto-generated method stub

                          //跳回主activity

                          Intent intent = new Intent();

                          intent.setClass(darvish.this, AlertDialogSampleActivity.class);

                          startActivity(intent);

                          darvish.this.finish();

                   }          

        });

    }