《android简单计算器.doc》由会员分享,可在线阅读,更多相关《android简单计算器.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;imp
2、ort android.view.LayoutInflater;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 Mai
3、nActivity extends ActionBarActivity 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(
4、);Overridepublic boolean 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 wil
5、l/ automatically handle 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
6、PlaceholderFragment extends 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;p
7、rivate Button button0;private 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(L
8、ayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) View rootView = inflater.inflate(R.layout.fragment_main, container,false);findbutton(rootView);/定义一个函数用于找组件ViewEvent(rootView);/定义一个函数。用于初始化监听器(将组件与监听器相连接)/注意,设置监听器的时候必须保证找到了组件。否则程序用易出错。特别提醒return rootView;/使用fragment,此处的返回值必须为ro
9、otView,上面有函数可以看一下、private 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.findViewByI
10、d(R.id.button4);button5=(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
11、.findViewById(R.id.button0);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.findViewByI
12、d(R.id.qingchu);buttondengyu=(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.setOnCl
13、ickListener(l);button4.setOnClickListener(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);button
14、chu.setOnClickListener(l);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.g
15、etText().toString();/上面找到了文本编辑框,/接上句。此处为将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.ge
16、tId()=R.id.button7|btn.getId()=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;i
17、f(btn.getId()=R.id.buttoncheng)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)/此处
18、的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;