1 package com.zachary.multiplication; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.EditText; 12 13 public class EnterActivity extends Activity { 14 15 private Button myButton = null; 16 private EditText factorOne = null; 17 private EditText factorTwo = null; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_enter); 22 factorOne = (EditText)findViewById(R.id.factorOne); 23 factorTwo = (EditText)findViewById(R.id.factorTwo); 24 myButton = (Button)findViewById(R.id.resultButton); 25 myButton.setOnClickListener(new MyButtonListener()); 26 } 27 28 @Override 29 public boolean onCreateOptionsMenu(Menu menu) { 30 // Inflate the menu; this adds items to the action bar if it is present. 31 getMenuInflater().inflate(R.menu.enter, menu); 32 menu.add(0,1,1,R.string.exit); 33 menu.add(0,2,2,R.string.about); 34 return true; 35 } 36 37 @Override 38 public boolean onOptionsItemSelected(MenuItem item) { 39 // TODO Auto-generated method stub 40 if(item.getItemId()==1) 41 finish(); 42 return super.onOptionsItemSelected(item); 43 } 44 45 class MyButtonListener implements OnClickListener { 46 47 @Override 48 public void onClick(View arg0) { 49 // TODO Auto-generated method stub 50 String factorOneStr = factorOne.getText().toString(); 51 String factorTwoStr = factorTwo.getText().toString(); 52 Intent intent = new Intent(); 53 intent.putExtra("num1",factorOneStr); 54 intent.putExtra("num2",factorTwoStr); 55 intent.setClass(EnterActivity.this, ResultActivity.class); 56 EnterActivity.this.startActivity(intent); 57 } 58 } 59 }
1 package com.zachary.multiplication; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.widget.TextView; 7 8 public class ResultActivity extends Activity{ 9 10 private TextView resultTextView = null; 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 // TODO Auto-generated method stub 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_result); 16 Intent intent = getIntent(); 17 resultTextView = (TextView)findViewById(R.id.resultTextView); 18 int factorOneNum = Integer.parseInt(intent.getStringExtra("num1")); 19 int factorTwoNum = Integer.parseInt(intent.getStringExtra("num2")); 20 int resultNum = factorOneNum * factorTwoNum; 21 resultTextView.setText(resultNum+""); 22 } 23 24 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" android:layout_width="fill_parent" 3 android:layout_height="fill_parent"> 4 <EditText android:id="@+id/factorOne" 5 android:layout_width="fill_parent" 6 android:layout_height="wrap_content" 7 android:hint="@string/editText" 8 android:numeric="integer" 9 /> 10 <TextView android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="@string/mult" 13 /> 14 <EditText android:id="@+id/factorTwo" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:hint="@string/editText" 18 android:numeric="integer" 19 /> 20 <Button android:id="@+id/resultButton" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" 23 android:text="@string/result" 24 />" 25 </LinearLayout>
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" android:layout_width="fill_parent" 3 android:layout_height="fill_parent"> 4 <TextView android:id="@+id/resultTextView" 5 android:layout_width="wrap_content" 6 android:layout_height="wrap_content" 7 /> 8 </LinearLayout>