Jump of Activity in Android
Directory (?) [+]
- 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]
- 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 );}
- });}
- }
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]
-
4. Implement IntentActivity. java[Java]View plaincopyprint?
- 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 );}
- }
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.