MainActivity.java

 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 EditText EditTextOne;
private EditText EditTextTwo;
private Button myButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditTextOne = (EditText) findViewById(R.id.EditTextOne);
EditTextTwo = (EditText) findViewById(R.id.EditTextTwo);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new myButtonListener());
} class myButtonListener implements OnClickListener{
public void onClick(View v) {
Intent intent = new Intent();
String first = EditTextOne.getText().toString();
String second = EditTextTwo.getText().toString(); intent.putExtra("one",first );
intent.putExtra("two", second); intent.setClass(MainActivity.this, ResultActivity.class);
startActivity(intent);
} }
//当客户点击MENU按钮的时候,调用该方法
public boolean onCreateOptionsMenu(Menu menu) {
//一个add对应就产生一个选项
menu.add(0, 1, 1, R.string.exit);
menu.add(0, 2, 2, R.string.about);
return super.onCreateOptionsMenu(menu);
}
//当客户点击菜单当中的某一个选项时,会调用该方法
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == 1){
finish();
}
return super.onOptionsItemSelected(item);
} }

main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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=".MainActivity" > <EditText
android:id="@+id/EditTextOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>" <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/result"
/> <EditText
android:id="@+id/EditTextTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> <Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calculate"
/>
</LinearLayout>

ResultActivity.java

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class ResultActivity extends Activity{
private TextView myTextView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
//获取Intent对象
Intent intent = getIntent();
//得到Intent对象当中的值
String factorOneStr = intent.getStringExtra("one");
String factorTwoStr = intent.getStringExtra("two");
//将接收到的字符串转换成整数
int factorOneInt = Integer.parseInt(factorOneStr);
int factorOneTwo = Integer.parseInt(factorTwoStr);
//计算两个数的值
int result = factorOneInt*factorOneTwo; myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText(result); } }

result.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TexiView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> </LinearLayout>

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mars.widget"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mars.widget.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.mars.widget.ResultActivity" android:label="@string/result"/>"
</application> </manifest>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">EditText控件和menu控件</string>
<string name="action_settings">Settings</string>
<string name="result">乘以</string>
<string name="calculate">计算</string>
<string name="exit">退出</string>
<string name="about">关于</string> </resources>

计算器和Menu的更多相关文章

  1. AppleScript 快速入门

    AppleScript 快速入门 AppleScript 顾名思义是苹果开发的一套脚本语言,利用 AppleScript 在 macOS 系统上可以对其他程序进行操作,点击按钮.发送消息.模拟自动化执 ...

  2. Win10计算器在哪里?三种可以打开Win10计算器的方法图文介绍

    全新的windows10系统带来了不少新的特性和改变,其中win10的计算器位置就发生了很多的变化,导致很多网友们都以为win10计算器不见了,那么,win10计算器在哪里?如何打开?针对此问题,本文 ...

  3. 菜鸟学Android编程——简单计算器《一》

    菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...

  4. Android 简单计算器源码....

    PS:今天算是闲着没事做了一个小型的计算器...顺便熟悉一下Android的布局,组件,以及时间监听的方法...就当是做了一个小小的练习吧...     顺便去对比了一下别人写的代码...有的使用到了 ...

  5. andriod 带看括弧的计算器

    界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=& ...

  6. 每天2个android小例子----简单计算器源代码

    通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局 package com.android.xiong.gridlayoutTest; import java.mat ...

  7. android实现计算器功能

    设计一个简单的计算器. 第一个Activity的界面. 第二个Activity显示算式和计算结果. 第一个Activity代码: import android.app.Activity; import ...

  8. Android之一个简单计算器源代码

    通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局   源码如下(欢迎大家指导 批评 ) package com.android.xiong.gridlayoutTe ...

  9. 《汇编语言程序设计》——仿windows计算器

    <汇编语言程序设计> ——计算器程序设计 目录 一.     题目与目标 1.      题目 2.      学习目的 二.     分析与设计 1.      系统分析 2.      ...

随机推荐

  1. js实现全选/全不选、反选

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 移动APP开发框架盘点

    移动APP开发框架盘点 总体概述 现在比较流行的移动APP开发框架有以下六种:网页.混合.渐进.原生.桥接.自绘.前三种体验与Web的体验相似,后三种与原生APP的体验相似.这六种框架形式,都有自己适 ...

  3. 数据分析之numpy模块

    numpy(numerical python)是python语言的一个扩展程序库,支持大量的维度数组和矩阵运算,此外也针对数组提供大量的数学函数库. 一.创建数组 1 使用array()创建 impo ...

  4. 动态规划法(四)0-1背包问题(0-1 Knapsack Problem)

      继续讲故事~~   转眼我们的主人公丁丁就要离开自己的家乡,去大城市见世面了.这天晚上,妈妈正在耐心地帮丁丁收拾行李.家里有个最大能承受20kg的袋子,可是妈妈却有很多东西想装袋子里,已知行李的编 ...

  5. Nginx + 阿里云SSL + tomcat 实现https访问代理

    第一步:阿里云申请云盾证书服务 第二步:下载证书 第三步:修改Nginx配置 1. 证书文件214033834890360.pem,包含两段内容,请不要删除任何一段内容. 2. 如果是证书系统创建的C ...

  6. NIO学习笔记二

    Java NIO的通道channel 既可以从通道中读取数据,又可以写数据到通道.通道可以异步地读写.通道中的数据总是要先读到一个Buffer(缓冲区),或者总是要从一个Buffer(缓冲区)中写入. ...

  7. spring boot (1):初尝

    工具:intellij idea 自定义banner: 控制台console显示的banner 自定义方式,在src/main/resources 下创建banner.txt增加文本即可,生成字符网站 ...

  8. 【pygame游戏编程】第二篇-----移动图像

    Learning From Here import pygame import sys pygame.init() screen_width = 640 screen_high = 480 scree ...

  9. JqGrid: paging int asp.net

    https://www.codeproject.com/Articles/1118363/GridView-with-Server-Side-Filtering-Sorting-and-Pa http ...

  10. HTML常用标签及属性

    标签格式 格式: 双边:<标签名 属性1="值1" 属性2='值2' 属性3=值3>内容</标签名> 单边:<标签名 属性1="值1&quo ...