从一个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();

                   }         

        });

    }

}