主窗口(活动)
package com.myproject.testprojectonintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
/** the button that I've added in the xml*/
Button startBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
/** Get the id of the button from the xml*/
startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener( this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
/** make a refernce to store the intent when the view has been clicked*/
Intent intent;
/** Make cases according to the number of buttons you have in screen
* In this case, I've added one.*/
switch(view.getId()){
case R.id.startBtn :
Log.i("btnClick","Start button id is"+view.getId());
/** Intent should be fired from this activity to the other activity*/
intent = new Intent(MainActivity.this, ResultActivity.class);
/** This is useful when you want to get the button that is clicked here on the
* destination activity*/
intent.putExtra("button_click", "Start");
/** Start the intent*/
startActivity(intent);
/*** Use this only when you want to finish your current activity while opening an another one.
* if this is commented, then this activity will be running in the background.
***/
this.finish();
break;
}
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen, menu);
return true;
}*/
}
第二个窗口(活动)
package com.myproject.testprojectonintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ResultActivity extends Activity implements OnClickListener{
Button resultBtn;
private String _showText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result_screen);
/** Fetched from the MAIN ACTIVITY */
Bundle bundle = getIntent().getExtras();
if(bundle!=null){
_showText = bundle.getString("button_click");
}
Log.i("btnClick","button clicked is :"+_showText);
resultBtn = (Button) findViewById(R.id.resultBtn);
resultBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
/** make a refernce to store the intent when the view has been clicked*/
Intent intent;
/** Make cases according to the number of buttons you have in screen
* In this case, I've added one.*/
switch(view.getId()){
case R.id.resultBtn :
Log.i("btnClick","Result button is clicked whose id is :"+view.getId());
/** Intent should be fired from this activity to the other activity*/
intent = new Intent(ResultActivity.this, MainActivity.class);
/** Start the intent*/
startActivity(intent);
this.finish();
break;
}
}
}
主祭
<activity
android:name="com.myproject.testprojectonintent.ResultActivity"
></activity>
完成此工作后,我能够解决我的问题。