马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
先在activity_second.xml里加个文本控件<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/text1"/>
然后把MainActivity.java文件里的button3按钮修改为:button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("extra_data","hello activity1");
startActivity(intent);
}
});
把secondactivity.java修改为:package com.example.xinwei.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button btn =(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
startActivity(intent);
}
});
TextView text=(TextView)findViewById(R.id.text1);
Intent intent1=getIntent();
String string1 = intent1.getStringExtra("extra_data");
text.setText(string1);
}
}
就可以通过intent传值了,效果图为:
|