您好,登录后才能下订单哦!
新建android工程:
之后再新建一个class:任意起名(我的是Acitvity2),但是要继承activity;
同时在layout里面新建一个布局文件:
代码实现如下:maniactivity
public class MainActivity extends Activity {
private Button btn_startActivity2;
private TextView tv_out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_out=(TextView) findViewById(R.id.textView1);
btn_startActivity2=(Button) findViewById(R.id.btn_startActivity2);
btn_startActivity2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,Activity2.class);
//intent.putExtra("hello", "hello world!!!");
Bundle data=new Bundle();
data.putString("hello", "hello yaowen613@github.com!!!");
//data.putString("hello", "15626102810");
//data.putString("hello", "794365939");
intent.putExtras(data);
//startActivity(intent);
startActivityForResult(intent, 0);
}
});
}
//接收并处理返回的data;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
String result = data.getStringExtra("result");
tv_out.setText(result);
super.onActivityResult(requestCode, resultCode, data);
}
}
新建类的代码实现:
public class Activity2 extends Activity {
private TextView tv_out;
private Button btn_close;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
init();
}
private void init() {
// TODO Auto-generated method stub
tv_out=(TextView) findViewById(R.id.tv_out);
//tv_out.setText(getIntent().getStringExtra("hello"));
Bundle data=getIntent().getExtras();
String hello=data.getString("hello");
tv_out.setText(hello);
btn_close=(Button) findViewById(R.id.btn_close);
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.putExtra("result", "hello MainActivity");
//startActivity(intent);
setResult(0, intent);
finish();
}
});
}
}
布局文件就不用我上代码了,随便你们自己任意设计;
另外,这样直接运行,程序还是会抛异常强制退出,是因为新建的activity2这个class没有在manifest里面注册:注册方法就是在application标签里加入“<activity android:name=".Activity2"></activity>”这么一段话;如下图::
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。