Summary of the steps to create Activity jumps in Android

  • 2020-06-07 05:18:12
  • OfStack

1. Create the project

file- > new- > Fill in the application name, project name, and package name
Found in the project directory src/com example. helloworld MainActivity. java

2. Add code


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;
 @Override
 protected 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 and drop 1 Button control onto res/layout/ es25EN_main.xml. The Button message is automatically generated in ES29en_main.xml. Note that the id value is useful if you use this Button

3. Add jump Activity

1, right-click the src/com. example. helloworld, new - > class, fill in IntentActivity at name, don't add java
2. Add layout. Right click on res/layout, new- > android XML file, drag 1 TextView onto this xml. The address of TextView is automatically added to R.java
3. Add the following code to ES65en. xml: Complete the registration of Activity


<activity
   android:name="com.example.helloworld.IntentActivity"> </activity>

3. Implement IntentActivity. java


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
{
 @Override
 protected 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

Turn on the simulator or your phone and run.


Related articles: