1、创建工程
file->new->android application 依次填入应用名称、工程名、包名
在工程目录下找到src/com.example.helloworld MainActivity.java
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);
- }
- });
- }
- }
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);
}
});
}
}
在添加代码之前,先在res/layout/activity_main.xml中,拖拽一个Button控件上去。此时会自动在activity_main.xml中生成Button的信息,注意id值,如果使用此Button,有用
3、添加跳转Activity
1、右击src/com.example.helloworld,new->class,在name处填写IntentActivity,不用加java即可
2、添加layout。在res/layout处右击,new->android XML file,在此xml上拖拽一个TextView上去。此时在R.java中自动添加了TextView的地址
3、在AndroidManifest.xml中添加如下代码:完成Activity的注册
- <activity
- android:name="com.example.helloworld.IntentActivity"> </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);
- }
- }
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、运行
打开模拟器或者手机,运行。