Jump of Activity in Android

Source: Internet
Author: User

Jump of Activity in Android
Directory (?) [+]

  1. Create a project Add code add jump Activity run 1, create a project file-> new-> android application fill in the application name, project name, package name in the project directory find src/com. example. helloworld MainActivity. java2, add code[Java]
    1. Package com. example. helloworld;
    2. Import android. app. Activity; import android. content. Intent;
    3. Import android. OS. Bundle; import android. view. View;
    4. Import android. view. View. OnClickListener; import android. widget. Button;
    5.  
    6. Public class MainActivity extends Activity {
    7. Private Button button; @ Override
    8. Protected void onCreate (Bundle savedInstanceState ){
    9. Super. onCreate (savedInstanceState); setContentView (R. layout. activity_main );
    10. This. button = (Button) this. findViewById (R. id. button1 );
    11. This. button. setOnClickListener (new OnClickListener () {@ Override
    12. Public void onClick (View v) {Intent intent = new Intent ();
    13. Intent. setClass (MainActivity. this, IntentActivity. class); intent. putExtra (name, Hello World );
    14. StartActivity (intent );}
    15. });}
    16. }
      package com.example.helloworld;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {    private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);   this.button = (Button) this.findViewById(R.id.button1);        this.button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setClass(MainActivity.this, IntentActivity.class);                intent.putExtra(name, Hello World);                startActivity(intent);            }        });}}

      Before adding code, drag a Button control in res/layout/activity_main.xml. In this case, the Button information is automatically generated in activity_main.xml. Note the id value. If this Button is used, 3. Add link Activity1 and right-click src/com. example. helloworld, new-> class, fill in IntentActivity in name, do not add java 2, add layout. Right-click res/layout, new-> android XML file, and drag a TextView on the xml file. In this case, the TextView address is automatically added to R. java. 3. Add the following code to AndroidManifest. xml: complete Activity registration.[Java] 
      1.  
        4. Implement IntentActivity. java[Java]View plaincopyprint?
        1. Package com. example. helloworld;
        2. Import android. app. Activity; import android. OS. Bundle;
        3. Import android. widget. TextView; import android. content. Intent;
        4. Public class IntentActivity extends Activity {
        5. @ Override protected void onCreate (Bundle savedInstanceState)
        6. {Super. onCreate (savedInstanceState );
        7. SetContentView (R. layout. activity_intent); Intent intent = getIntent ();
        8. String string = intent. getStringExtra (name );
        9. TextView textView = new TextView (this); textView. setTextSize (40 );
        10. TextView. setText (string );
        11. SetContentView (textView );}
        12. }
          package com.example.helloworld;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import android.content.Intent;public class IntentActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_intent);Intent intent = getIntent();String string = intent.getStringExtra(name);TextView textView = new TextView(this);textView.setTextSize(40);textView.setText(string);setContentView(textView);}}

          4. Run and open the simulator or mobile phone.

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.