3、Android中Activity的跳转
1、创建工程
2、添加代码
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); } }); } }
3、添加跳转Activity
<activity android:name="com.example.helloworld.IntentActivity"> </activity>4、实现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、运行
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。