一、代码
1.xml
(1)activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.s01_original_e06_caculate.MainActivity" > <EditText
android:id="@+id/m_multiplier"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/> <EditText
android:id="@+id/m_multiplicand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/m_multiplier"
android:inputType="number"/> <Button
android:id="@+id/caculateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/caculate"
android:layout_below="@id/m_multiplicand"/> </RelativeLayout>
(2)resultactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/accumulateText"
android:text="@string/app_name"/> </RelativeLayout>
2.java
(1)MainActivity.java
package com.example.s01_original_e06_caculate; 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; public class MainActivity extends Activity { private Button caculateBtn = null;
private EditText multiplierView = null;
private EditText multiplicandViwe = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
multiplierView = (EditText) findViewById(R.id.m_multiplier);
multiplicandViwe = (EditText) findViewById(R.id.m_multiplicand);
caculateBtn = (Button) findViewById(R.id.caculateBtn);
caculateBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("multiplier", multiplierView.getText().toString());
intent.putExtra("multiplicand", multiplicandViwe.getText().toString());
intent.setClass(MainActivity.this, ResultActivity.class);
MainActivity.this.startActivity(intent);
}
});
} @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) {
switch (item.getItemId()) {
case 1:
finish();
return true;
default:
System.out.println("关于程序");
break;
}
return super.onOptionsItemSelected(item);
}
}
(2)ResultActivity.java
package com.example.s01_original_e06_caculate; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class ResultActivity extends Activity { private TextView accumulateTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultactivity); accumulateTextView = (TextView) findViewById(R.id.accumulateText);
Intent intent = getIntent();
int multiplier = Integer.parseInt(intent.getStringExtra("multiplier"));
int multiplicand = Integer.parseInt(intent.getStringExtra("multiplicand"));
accumulateTextView.setText((multiplier * multiplicand) + "");
}
}