Intent -- open another Activity -- single pass value


In Android applications, we use the intent mechanism to jump between activities.

This example briefly introduces how to use intent to jump from MainActivity to another OtherActivity and realize one-way value transfer.

1, Design interface

1. MainActivity layout file

Open the RES / layout / activity main.xml file.
Enter the following code:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/open"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="Open another Activity" />  
  13.   
  14. </LinearLayout>  


2. OtherActivity layout file

Open the res/layout/otheractivity.xml file.
Enter the following code:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/close"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="Close current Activity(OtherActivity)" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/orderno"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="Show from MainActivity Pass value of!" />  
  18.   
  19. </LinearLayout>  


2, Procedure documents

1. Open the file "Src / com. Genwoxue. ContentProvider [B / mainactivity. Java".
Then enter the following code:

  1. package com.genwoxue.intent_a;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.   
  15. private Button btnOpen=null;  
  16. @Override  
  17. public void onCreate(Bundle savedInstanceState)     
  18.     {     
  19.         super.onCreate(savedInstanceState);                
  20.         setContentView(R.layout.activity_main);      
  21.         btnOpen=(Button)super.findViewById(R.id.open);  
  22.         btnOpen.setOnClickListener(new OnClickListener(){  
  23.         public void onClick(View v)  
  24.         {    
  25.         Intent intent=new Intent(MainActivity.this,OtherActivity.class);  
  26.         intent.putExtra("info""No66778899");  
  27.         MainActivity.this.startActivity(intent);  
  28.         }  
  29.         });  
  30.           
  31.     }  
  32. }         
  33.       
  34.       

2. Open the file "src/com.genwoxue.contentprovider_b/OtherActivity.java".
Then enter the following code:

  1. package com.genwoxue.intent_a;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12.   
  13. public class OtherActivity extends Activity {  
  14.   
  15.   
  16. private TextView tvOrderNo=null;  
  17. private Button btnClose=null;  
  18. @Override  
  19. public void onCreate(Bundle savedInstanceState)     
  20.     {     
  21.         super.onCreate(savedInstanceState);                
  22.         setContentView(R.layout.otheractivity);             
  23.         tvOrderNo=(TextView)super.findViewById(R.id.orderno);  
  24.         btnClose=(Button)super.findViewById(R.id.close);  
  25.           
  26.         Intent intent=super.getIntent();  
  27. String orderNo=intent.getStringExtra("info");  
  28. tvOrderNo.setText(orderNo);  
  29. btnClose.setOnClickListener(new OnClickListener(){  
  30.         public void onClick(View v)  
  31.         {    
  32.         finish();  
  33.         }  
  34.         });  
  35.     }  
  36. }            
  37.       

3, Profile

Open the "Android manifest. XML" file.
Then enter the following code:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.intent_a"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="15" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.genwoxue.intent_a.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.           
  26.        <strong><span style="color:#ff0000;"> <activity  
  27.             android:name="com.genwoxue.intent_a.OtherActivity">  
  28.         </activity>  
  29. </span></strong>    </application>  
  30.   
  31. </manifest>  


 

4, Operation results

   

Tags: Android xml encoding Java

Posted on Mon, 04 May 2020 17:21:27 -0400 by rbeschizza