((Button) findViewById(R.id.Notepadv1)).setOnClickListener( new OnClickListener() { public void onClick(View v) { startActivity(new Intent (MyAndroidAppActivity.this, Notepadv1.class) ); } });
static final int SEND_SMS_REQUEST = 0; static final int CALL_REQUEST = 1; ((Button) findViewById(R.id.sms)).setOnClickListener( new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyAndroidAppActivity.this, SendSMSActivity.class); startActivityForResult(intent, SEND_SMS_REQUEST); } }); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SEND_SMS_REQUEST) { if (resultCode == RESULT_OK) { Toast.makeText(this, "Send SMS RESULT_OK", Toast.LENGTH_SHORT).show(); }else if (resultCode == RESULT_CANCELED) { Bundle bundle = data.getExtras(); String phoneno = bundle.getString("phoneNO"); Toast.makeText(this, "Send SMS RESULT_CANCELED "+phoneno, Toast.LENGTH_SHORT).show(); } }else if (requestCode == CALL_REQUEST) { if (resultCode == RESULT_CANCELED) { Toast.makeText(this, "Call RESULT_CANCELED", Toast.LENGTH_SHORT).show(); } } }
SendSMSActivity:
((Button) findViewById(R.id.send)).setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { SendSMSActivity.this.setResult(RESULT_OK); SendSMSActivity.this.finish(); } }):
** 注意,在setResult后,要调用finish()销毁当前的Activity,否则无法返回到原来的Activity,就无法执行原来Activity的onActivityResult函数,看到当前的Activity没反应。
((Button) findViewById(R.id.cancel)).setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { // 实例化 Bundle,设置需要传递的参数 Bundle bundle = new Bundle(); bundle.putString("phoneNO", "020-123"); SendSMSActivity.this.setResult(RESULT_CANCELED, SendSMSActivity.this.getIntent().putExtras(bundle)); SendSMSActivity.this.finish(); } }); @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // 是否触发按键为back键 if (keyCode == KeyEvent.KEYCODE_BACK) { // 实例化 Bundle,设置需要传递的参数 Bundle bundle = new Bundle(); bundle.putString("phoneNO", "020-123"); setResult(RESULT_CANCELED, this.getIntent().putExtras(bundle)); this.finish(); return true; }else { return super.onKeyDown(keyCode, event); } }
// 在某个按钮响应事件里 Intent intent = new Intent(this, TextInputActivity.class); intent.putExtra("Text", mText); intent.putExtra("TextColor", mTextColor); intent.putExtra("TextSize", mTextSize); intent.putExtra("TextBold", mTextBold); startActivityForResult(intent, REQUEST_TEXT);
In Activity B:
// in onCreate(Bundle savedInstanceState) Bundle extras = getIntent().getExtras(); mText = extras.getString("Text"); mTextColor = extras.getInt("TextColor"); mTextSize = extras.getFloat("TextSize"); mTextBold = extras.getBoolean("TextBold");
Android课程---Activity的跳转与传值(转自网上)
原文:http://www.cnblogs.com/0927wyj/p/5304364.html