两个布局文件:
两个Activity:
package com.may.activity;
import android.app.Activity;
import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;/** * */public class Activity04 extends Activity {private EditText numOne = null;
private EditText numTwo = null; private TextView symbol = null; private Button calculate = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity04); numOne=(EditText)findViewById(R.id.numOne); numTwo=(EditText)findViewById(R.id.numTwo); symbol=(TextView)findViewById(R.id.symbol); calculate=(Button)findViewById(R.id.calculate); //为symbol symbol.setText("乘以"); calculate.setText("计算"); calculate.setOnClickListener(new CalculateListener()); }@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, R.string.exit); menu.add(0, 2, 2, R.string.about); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==1){ finish(); }else if(item.getItemId()==2){ } return super.onOptionsItemSelected(item); } class CalculateListener implements OnClickListener{
@Override
public void onClick(View v) { String numOneStr = numOne.getText().toString().trim(); String numTwoStr = numTwo.getText().toString().trim(); Intent inten = new Intent(); inten.putExtra("one", numOneStr); inten.putExtra("two", numTwoStr); inten.setClass(Activity04.this, Activity05.class); Activity04.this.startActivity(inten); } }}
package com.may.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class Activity05 extends Activity { private TextView resultTextView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity05); resultTextView = (TextView) findViewById(R.id.resultTextView); Intent intent = getIntent(); int numOne = Integer.valueOf(intent.getStringExtra("one")); int numTwo = Integer.valueOf(intent.getStringExtra("two")); int resultValue = numOne * numTwo; resultTextView.setText(resultValue+""); }}
在AndroidMainfest添加: