1、建立项目MyApplication

2、右击“res/layout”目录,创建一个新的空白Activity 新的activity,名称为MyActivity2。

4c3b49224466517046ba9018cd2b6f04.png

3、修改布局文件

activity_my.xml内容修改为:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context=".MyActivity">

android:id="@+id/button01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="进入MyActivity2" />

activity_my_2.xml内容修改为:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context="com.example.letian.myapplication.MyActivity2">

android:id="@+id/textview01"

android:text=""

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

4、修改java文件 MyActivity.java修改为:

package com.example.letian.myapplication;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MyActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my);

final Button btn01 = (Button) this.findViewById(R.id.button01);

btn01.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent it = new Intent(MyActivity.this, MyActivity2.class);

Bundle bundle=new Bundle();

bundle.putString("value", "This is from MyActivity!");

it.putExtras(bundle);

startActivity(it);

}

});

}

}

MyActivity2.java修改为:

package com.example.letian.myapplication;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class MyActivity2 extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my_2);

final TextView tv01 = (TextView) findViewById(R.id.textview01);

Bundle bundle=getIntent().getExtras();

String value=bundle.getString("value");

tv01.setText(value);

}

}

5、运行效果图

62a0933a197be32174e5eccbddf5e054.png

点击按钮“进入MyActivity2”,

b0b788cf53fe3e1520fdaaff1c972d7f.png

更多

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐