android数据传递
短信预约 -IT技能 免费直播动态提醒
将MainActivity页面的数据:姓名、年龄、性别,传递给新的Activity页面显示出来
MainActivity.java页面package com.example.dell.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView2;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView2=(TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//String text =textView2.getText().toString();
String name=textView2.getText().toString();
Intent intent =new Intent(MainActivity.this,OpenActivity.class);
// intent.setClassName(MainActivity.this,"OpenActivity.class");
intent.putExtra("name","姓名:gongjuan");
intent.putExtra("age","年龄:20");
intent.putExtra("sex","性别:女");
startActivity(intent);
startActivityForResult(intent,1);
}
});
}
protected void onActivityResukt(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == 1) {
String string = data.getStringExtra("extra_data");
}
}
}
}
在新activity页面
package com.example.dell.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class OpenActivity extends AppCompatActivity {
private TextView textView1;
private TextView textView5;
private TextView textView6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
textView1= (TextView) findViewById(R.id.textView1);
textView5= (TextView) findViewById(R.id.textView5);
textView6=(TextView) findViewById(R.id.textView6);
Intent intent = new Intent();
String name=getIntent().getStringExtra("name");
String age=getIntent().getStringExtra("age");
String sex=getIntent().getStringExtra("sex");
textView1.setText(name);
textView5.setText(age);
textView6.setText(sex);
setResult(1,intent);
}
}
布局就不发了
作者:是独角兽咩
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341