3. Redirect of Activity in Android
1. Create a project file-> new-> android application. Enter the application name, project name, and package name in the project directory, find src/com. example. helloworld MainActivity. java2, and 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;@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.
4. 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{@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.