Android屏幕间的跳转和事件的传递


在一般情况下,Android的每一个屏幕基本上就是一个Activity,屏幕间的切换实际上就是在互相调用的过程,Android使用Intent完成这个动作。包括两种跳转:无返回值和有返回值。

一、无返回值跳转

  1. public void onClick(View v) {  
  2.                 Intent intent=new Intent(TestAndroidActivity.this,t2.class);  
  3.                 startActivity(intent);  
  4.             }  

二、带有返回值的跳转

ReceiveResult.java片段

  1. static final private GET_CODE = 0;  
  2. <pre class="java" name="code">public void onClick(View v) {  
  3.             // Start the activity whose result we want to retrieve.  The  
  4.             // result will come back with request code GET_CODE.  
  5.             Intent intent = new Intent(ReceiveResult.this, SendResult.class);  
  6.             startActivityForResult(intent, GET_CODE);  
  7.         }  

SendResult.java片段

  1. public class SendResult extends Activity  
  2. {  
  3.     /** 
  4.      * Initialization of the Activity after it is first created.  Must at least 
  5.      * call {@link android.app.Activity#setContentView setContentView()} to 
  6.      * describe what is to be displayed in the screen. 
  7.      */  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         // Be sure to call the super class.   
  12.         super.onCreate(savedInstanceState);  
  13.   
  14.         // See assets/res/any/layout/hello_world.xml for this   
  15.         // view layout definition, which is being set here as   
  16.         // the content of our screen.   
  17.         setContentView(R.layout.send_result);  
  18.   
  19.         // Watch for button clicks.   
  20.         Button button = (Button)findViewById(R.id.corky);  
  21.         button.setOnClickListener(mCorkyListener);  
  22.         button = (Button)findViewById(R.id.violet);  
  23.         button.setOnClickListener(mVioletListener);  
  24.     }  
  25.   
  26.     private OnClickListener mCorkyListener = new OnClickListener()  
  27.     {  
  28.         public void onClick(View v)  
  29.         {  
  30.             // To send a result, simply call setResult() before your   
  31.             // activity is finished.   
  32.             setResult(RESULT_OK, (new Intent()).setAction("Corky!"));  
  33.             finish();  
  34.         }  
  35.     };  
  36.   
  37.     private OnClickListener mVioletListener = new OnClickListener()  
  38.     {  
  39.         public void onClick(View v)  
  40.         {  
  41.             // To send a result, simply call setResult() before your   
  42.             // activity is finished.   
  43.             setResult(RESULT_OK, (new Intent()).setAction("Violet!"));  
  44.             finish();  
  45.         }  
  46.     };  
  47. }  

ReceiveResult.java 片段

  1. protected void onActivityResult(int requestCode, int resultCode,  
  2.         Intent data) {  
  3.         // You can use the requestCode to select between multiple child   
  4.         // activities you may have started.  Here there is only one thing   
  5.         // we launch.   
  6.         if (requestCode == GET_CODE) {  
  7.   
  8.             // We will be adding to our text.   
  9.             Editable text = (Editable)mResults.getText();  
  10.   
  11.             // This is a standard resultCode that is sent back if the   
  12.             // activity doesn't supply an explicit result.  It will also   
  13.             // be returned if the activity failed to launch.   
  14.             if (resultCode == RESULT_CANCELED) {  
  15.                 text.append("(cancelled)");  
  16.   
  17.             // Our protocol with the sending activity is that it will send   
  18.             // text in 'data' as its result.   
  19.             } else {  
  20.                 text.append("(okay ");  
  21.                 text.append(Integer.toString(resultCode));  
  22.                 text.append(") ");  
  23.                 if (data != null) {  
  24.                     text.append(data.getAction());  
  25.                 }  
  26.             }  
  27.   
  28.             text.append("\n");  
  29.         }  

相关内容