Android static parameter passing

Source: Internet
Author: User

Android static parameter passing

1. Create OtherActivity. java

 

public class OtherActivity extends Activity {public static String name;public static int age;private TextView textview;protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.other);                textview = (TextView)this.findViewById(R.id.msg);        textview.setText("name:"+name+",age:"+age);    }}

Defines the static variable name, age.

 

 

Ii. In MainActivity. java, when you jump to the page, call the static variable name and age of class OtherActivity to pass in

 

public class MainActivity extends Activity {Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button)this.findViewById(R.id.button1);        button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setClass(MainActivity.this,OtherActivity.class);OtherActivity.age = 23;OtherActivity.name ="deng";startActivity(intent);}});    }}

 

 

3. Attach xml

1) activity_main.xml

 

 
  
 

 

 

2) other. xml

 

 
     
      
  
 
 
3)AndroidManifest.xml
 
 
  
     
     
     
    
       
  
  


 

 




 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.