- Intent——切换activity
intent.setClass(first.this,second.class);startActivity(intent);
- Bundle——切换时数据传递
//跳转Intent intent = new Intent();//跳转时数据传输Bundle bundle = new Bundle();intent.setClass(MainActivity.this, SecondActivity.class);bundle.putString("message", str);intent.putExtras(bundle);startActivity(intent);
Bundle bundle = this.getIntent().getExtras();String message = bundle.getString("message");
Source Code:
package com.example.intent;import android.os.Bundle;import android.R.string;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity { private EditText myText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button)findViewById(R.id.button1); myText = (EditText)findViewById(R.id.editText1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String str = myText.getText().toString(); //跳转 Intent intent = new Intent(); //跳转时数据传输 Bundle bundle = new Bundle(); intent.setClass(MainActivity.this, SecondActivity.class); bundle.putString("message", str); intent.putExtras(bundle); startActivity(intent); } }); } }
package com.example.intent;import android.app.Activity;import android.os.Bundle;import android.view.TextureView;import android.widget.TextView;public class SecondActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); Bundle bundle = this.getIntent().getExtras(); String message = bundle.getString("message"); TextView myText = (TextView)findViewById(R.id.textView2); myText.setText(message); } }
Intent Settings Hello world! TreeDream