Android计算器 Android实战教程第一篇之最简单的计算器

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android计算器 Android实战教程第一篇之最简单的计算器

杨道龙   2021-03-22 我要评论
想了解Android实战教程第一篇之最简单的计算器的相关内容吗,杨道龙在本文为您仔细讲解Android计算器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,计算器,下面大家一起来学习吧。

从今天开始,本专栏持续更新Android简易实战类博客文章。和以往专栏不同,此专栏只有实例。每个实例尽量按照知识点对应相应一章节的内容去写,循序渐进。有些实例可能会与另一个专栏有重复的文章。

开始本专栏的第一个简易案例:

首先设置两个布局文件,一个布局文件进行输入数据,获取加法运算;另一个布局文件进行显示最终结果。Activity1启动Activity2,并传递计算结果值给Activity2.

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
<EditText 
 android:id="@+id/factorOne" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
<TextView 
 android:id="@+id/symbol" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
<EditText 
 android:id="@+id/factorTwo" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
<Button 
 android:id="@+id/calculate" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 /> 
</LinearLayout> 

页面展示:

result.xml

activity03活动:

package mars.activity03; 
 
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; 
//1.在Activity03当中,要声明四个控件 
//2.要为其中的两个控件设置显示的值 
//3.创建一个监听器类,监听按钮按下的动作 
//4.将监听器类的对象,绑定在按钮对象上 
public class Activity03 extends Activity { 
 /** Called when the activity is first created. */ 
 private EditText factorOne ; 
 private EditText factorTwo; 
 private TextView symbol; 
 private Button calculate; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.main); 
 //根据控件的ID来取得代表控件的对象 
 factorOne = (EditText)findViewById(R.id.factorOne); 
 factorTwo = (EditText)findViewById(R.id.factorTwo); 
 symbol = (TextView)findViewById(R.id.symbol); 
 calculate = (Button)findViewById(R.id.calculate); 
 //为symbol和calculate设置显示的值 
// symbol.setText("乘以"); 
// calculate.setText("计算"); 
 symbol.setText(R.string.symbol);//这里通过引用的方式,去String文件中引用。保证了业务逻辑、视图、引用资源分开 
 calculate.setText(R.string.calculate); 
 //将监听器的对象绑定到按钮对象上面 
 calculate.setOnClickListener(new CalculateListener()); 
 } 
 //当客户点击MENU按钮的时候,调用该方法 
 @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(); 
 } 
 return super.onOptionsItemSelected(item); 
 } 
 class CalculateListener implements OnClickListener{ 
 
 @Override 
 public void onClick(View v) { 
  //取得两个EditText控件的值 
  String factorOneStr = factorOne.getText().toString(); 
  String factorTwoStr = factorTwo.getText().toString(); 
  //将这两个值存放到Intent对象当中 
  Intent intent = new Intent(); 
  intent.putExtra("one",factorOneStr); 
  intent.putExtra("two",factorTwoStr); 
  intent.setClass(Activity03.this, ResultActivity.class); 
  //使用这个Intent对象来启动ResultActivity 
  Activity03.this.startActivity(intent); 
 } 
 } 
} 

resultActivity:

package mars.activity03; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
//1.接受从Activity03当中所传递的值 
//2.计算两个值的积 
//3.将计算的结果显示在Activity上 
public class ResultActivity extends Activity{ 
 private TextView resultView; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 // TODO Auto-generated method stub 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.result); 
 resultView = (TextView)findViewById(R.id.result); 
 //得到Intent对象当中的值 
 Intent intent = getIntent(); 
 String factorOneStr = intent.getStringExtra("one"); 
 String factorTwoStr = intent.getStringExtra("two"); 
 int factorOneInt = Integer.parseInt(factorOneStr); 
 int factorTwoInt = Integer.parseInt(factorTwoStr); 
 //计算两个值的积 
 int result = factorOneInt * factorTwoInt; 
 resultView.setText(result + ""); 
 } 
 
} 

String.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <string name="hello">Hello World, Activity03!</string> 
 <string name="app_name">activity03</string> 
 <string name="resultLabel">result</string> 
 <string name="symbol">乘以</string> 
 <string name="calculate">计算</string> 
 <string name="exit">退出</string> 
 <string name="about">关于</string> 
</resources> 

最后再看一下配置文件:活动都要进行注册,并且设置Activity03为主活动

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="mars.activity03" 
 android:versionCode="1" 
 android:versionName="1.0"> 
 <application android:icon="@drawable/icon" android:label="@string/app_name"> 
 <activity android:name=".Activity03" 
   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=".ResultActivity" android:label="@string/resultLabel"/><!--这里使ResultActivity标题栏显示result--> 
 </application> 
 <uses-sdk android:minSdkVersion="4" /> 
 
</manifest> 

结果:

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们