《android简单计算器(5页).doc》由会员分享,可在线阅读,更多相关《android简单计算器(5页).doc(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-android简单计算器-第 5 页欢迎大家补充package com.example.zzz;/此文使用了fragment的模块。/此文使用了fragment的模块。/此文使用了fragment的模块。/此文使用了fragment的模块。import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutI
2、nflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import android.os.Build;public class MainActivity extends Action
3、BarActivity Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (savedInstanceState = null) getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment().commit();Overridepublic boolean
4、 onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;Overridepublic boolean onOptionsItemSelected(MenuItem item) / Handle action bar item clicks here. The action bar will/ automatically handle
5、clicks on the Home/Up button, so long/ as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id = R.id.action_settings) return true;return super.onOptionsItemSelected(item); * A placeholder fragment containing a simple view.public static class PlaceholderFragment exte
6、nds Fragment /此文使用了fragment的模块。private double a,b;private int num=0;private Button button1;private Button button2;private Button button3;private Button button4;private Button button5;private Button button6;private Button button7;private Button button8;private Button button9;private Button button0;pr
7、ivate Button buttonjia;private Button buttonjian;private Button buttoncheng;private Button buttonchu;private Button buttonxiaoshudian;private Button buttonqingchu;private Button buttondengyu;private TextView text;public PlaceholderFragment() Overridepublic View onCreateView(LayoutInflater inflater,
8、ViewGroup container,Bundle savedInstanceState) View rootView = inflater.inflate(R.layout.fragment_main, container,false);findbutton(rootView);/定义一个函数用于找组件ViewEvent(rootView);/定义一个函数。用于初始化监听器(将组件与监听器相连接)/注意,设置监听器的时候必须保证找到了组件。否则程序用易出错。特别提醒return rootView;/使用fragment,此处的返回值必须为rootView,上面有函数可以看一下、privat
9、e void findbutton(View rootView)button0=(Button)rootView.findViewById(R.id.button0);button1=(Button)rootView.findViewById(R.id.button1);button2=(Button)rootView.findViewById(R.id.button2);button3=(Button)rootView.findViewById(R.id.button3);button4=(Button)rootView.findViewById(R.id.button4);button5=
10、(Button)rootView.findViewById(R.id.button5);button6=(Button)rootView.findViewById(R.id.button6);button7=(Button)rootView.findViewById(R.id.button7);button8=(Button)rootView.findViewById(R.id.button8);button9=(Button)rootView.findViewById(R.id.button9);button0=(Button)rootView.findViewById(R.id.butto
11、n0);buttonjia=(Button)rootView.findViewById(R.id.buttonjia);buttonjian=(Button)rootView.findViewById(R.id.buttonjian);buttoncheng=(Button)rootView.findViewById(R.id.buttoncheng);buttonchu=(Button)rootView.findViewById(R.id.buttonchu);buttonqingchu=(Button)rootView.findViewById(R.id.qingchu);buttonde
12、ngyu=(Button)rootView.findViewById(R.id.buttondengyu);/text,找到文本编辑框组件,在下面需要传输值。text = (TextView)rootView.findViewById(R.id.editText1);private void ViewEvent(View rootView)button0.setOnClickListener(l);button1.setOnClickListener(l);button2.setOnClickListener(l);button3.setOnClickListener(l);button4.s
13、etOnClickListener(l);button5.setOnClickListener(l);button6.setOnClickListener(l);button7.setOnClickListener(l);button8.setOnClickListener(l);button9.setOnClickListener(l);buttonjia.setOnClickListener(l);buttonjian.setOnClickListener(l);buttoncheng.setOnClickListener(l);buttonchu.setOnClickListener(l
14、);buttondengyu.setOnClickListener(l);buttonqingchu.setOnClickListener(l);/实例化监听器:这一步必须的,相当于把监听器给物理化了。 View.OnClickListener l=new View.OnClickListener() Override/监听器实例化后会自动生成此方法,onClick函数中编辑简单计算器的方法。public void onClick(View v) / TODO Auto-generated method stubString s = text.getText().toString();/上面找
15、到了文本编辑框,/接上句。此处为将text中的内容传给s变量Button btn =(Button)v;String g=(String) btn.getText();/获取按钮的值。if(btn.getId()=R.id.button0|btn.getId()=R.id.button1|btn.getId()=R.id.button2|btn.getId()=R.id.button3|btn.getId()=R.id.button4|btn.getId()=R.id.button5|btn.getId()=R.id.button6|btn.getId()=R.id.button7|btn.g
16、etId()=R.id.button8|btn.getId()=R.id.button9)/此处含义:若按钮的id等于layout中的id,则text的值等于g(按钮的值);text.setText(g);return;if(btn.getId()=R.id.buttonjia)a=Double.parseDouble(s);num=1;text.setText();return;if(btn.getId()=R.id.buttonjian)a=Double.parseDouble(s);num=2;text.setText();return;if(btn.getId()=R.id.butto
17、ncheng)a=Double.parseDouble(s);num=3;text.setText();return;if(btn.getId()=R.id.buttonchu)a=Double.parseDouble(s);num=4;text.setText();return;if(btn.getId()=R.id.qingchu)a=0;b=0;num=0;text.setText();return;if(btn.getId()=R.id.buttondengyu)b=Double.parseDouble(s);switch(num)/此处的num纯粹是为了switch存在。因为需要用if语句,所以用/case num 作为+,-,*,/,的执行。case 1:text.setText(String.valueOf(a+b);break;case 2:text.setText(String.valueOf(a-b);break;case 3:text.setText(String.valueOf(a*b);break;case 4:if(b!=0)text.setText(String.valueOf(a/b);elseToast.makeText(getActivity(), 除数不能为零, Toast.LENGTH_SHORT).show();break;return;