ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用

一、代码

1.xml
(1)activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.s01_original_e06_caculate.MainActivity" >
10 
11     <EditText 
12         android:id="@+id/m_multiplier"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:inputType="text"/>
16     
17     <EditText
18         android:id="@+id/m_multiplicand"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:layout_below="@id/m_multiplier"
22         android:inputType="number"/>
23        
24     <Button
25         android:id="@+id/caculateBtn" 
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="@string/caculate"
29         android:layout_below="@id/m_multiplicand"/>
30 
31 </RelativeLayout>

 

(2)resultactivity.xml

 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin">
 9     
10     <TextView 
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:id="@+id/accumulateText"
14         android:text="@string/app_name"/>
15 
16 </RelativeLayout>

 

 

 

2.java
(1)MainActivity.java

 1 package com.example.s01_original_e06_caculate;
 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 MainActivity extends Activity {
14 
15     private Button caculateBtn = null;
16     private EditText multiplierView = null;
17     private EditText multiplicandViwe = null;
18     
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         multiplierView = (EditText) findViewById(R.id.m_multiplier);
24         multiplicandViwe = (EditText) findViewById(R.id.m_multiplicand);
25         caculateBtn = (Button) findViewById(R.id.caculateBtn);
26         caculateBtn.setOnClickListener(new OnClickListener() {
27             @Override
28             public void onClick(View v) {
29                 Intent intent = new Intent();
30                 intent.putExtra("multiplier", multiplierView.getText().toString());
31                 intent.putExtra("multiplicand", multiplicandViwe.getText().toString());
32                 intent.setClass(MainActivity.this, ResultActivity.class);
33                 MainActivity.this.startActivity(intent);
34             }
35         });
36     }
37 
38     @Override
39     public boolean onCreateOptionsMenu(Menu menu) {
40         menu.add(0, 1, 1, R.string.exit);
41         menu.add(0, 2, 2, R.string.about);
42         return super.onCreateOptionsMenu(menu);
43     }
44 
45     @Override
46     public boolean onOptionsItemSelected(MenuItem item) {
47         switch (item.getItemId()) {
48         case 1:
49             finish();
50             return true;
51         default:
52             System.out.println("关于程序");
53             break;
54         }
55         return super.onOptionsItemSelected(item);
56     }
57 }

 

(2)ResultActivity.java

 

 1 package com.example.s01_original_e06_caculate;
 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 accumulateTextView = null;
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.resultactivity);
15         
16         accumulateTextView = (TextView) findViewById(R.id.accumulateText);
17         Intent intent = getIntent();
18         int multiplier = Integer.parseInt(intent.getStringExtra("multiplier"));
19         int multiplicand = Integer.parseInt(intent.getStringExtra("multiplicand"));
20         accumulateTextView.setText((multiplier * multiplicand) + "");
21     }
22 }

 

posted @ 2016-02-13 11:54  shamgod  阅读(210)  评论(0编辑  收藏  举报
haha