介绍
这篇文章主要介绍Android工作室怎么实现简单计算器应用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
<强>一、简介:强>用Android Studio 实现一个简单的计算器应用程序,并在蓝叠模拟器中运行。
该计算器只能实现两位数字的四则运算。
<强>二、代码强>
activity_main.xml ,- - -界面设计
& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; GridLayout  xmlns: android=癶ttp://schemas.android.com/apk/res/android" ,xmlns:工具=癶ttp://schemas.android.com/tools" ,android: layout_width=癿atch_parent" ,android: layout_height=癿atch_parent" ,android: layout_below=癅 + id/textView" ,android: layout_alignParentStart=皌rue" ,android: rowCount=?”; ,android: columnCount=?”; ,在& lt; !——设置网格为6行4列——比; ,& lt; !——显示文本组件,占1行4列——比; & lt; TextView ,android: id=癅 + id/text" 350年,android: layout_width=癲p" ,android: layout_height=皐rap_content" ,android: layout_columnSpan=?”; ,android: layout_marginLeft=? px" ,android:重力=發eft" ,android: textSize=?0 dp",/比; , ,& lt; !——清除按钮,占1行4列——比; & lt;按钮 ,android: id=癅 + id/btnClear" 353年,android: layout_width=癲p" ,android: layout_height=皐rap_content" ,android: layout_columnSpan=?”; ,android:文本=扒宄? ,android: textSize=?6 sp",/比; , ,& lt; !——以下按钮为数字按钮和函数按钮,每个占1行1列,比; & lt;按钮 ,android: id=癅 + id/btn1" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn2" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn3" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btnPlus" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn4" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn5" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn6" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btnSubtract" ,android:文本=安澄? ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn7" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn8" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn9" ,android:文本=?“; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btnMultiply" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btnPoint" ,android:文本=?” ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btn0" ,android:文本=?”; ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btnSum" ,android:文本=?? ,android: textSize=?6 sp",/比; & lt;按钮 ,android: id=癅 + id/btnDivide" ,android:文本=?? ,android: textSize=?6 sp",/比; , & lt;/GridLayout>
界面:
MainActivity。java——功能实现
public class MainActivity extends AppCompatActivity implements View.OnClickListener { , 这里,,Button btn0 btn1 btn3, btn4,为btn5, btn6, btn7, btn8, btn9, ,btnClear、btnPlus btnSubtract、btnMultiply btnDivide, btnSum, btnPoint; ,TextView 文本; ,String str =,““ , ,@Override ,protected void  onCreate (Bundle savedInstanceState), { ,super.onCreate (savedInstanceState); ,setContentView (R.layout.activity_main); , ,btn0 =,(按钮),findViewById (R.id.btn0); ,btn1 =,(按钮),findViewById (R.id.btn1); ,btn2 =,(按钮),findViewById (R.id.btn2); ,btn3 =,(按钮),findViewById (R.id.btn3); ,btn4 =,(按钮),findViewById (R.id.btn4); ,btn5 =,(按钮),findViewById (R.id.btn5); ,btn6 =,(按钮),findViewById (R.id.btn6); null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullAndroid工作室怎么实现简单计算器应用程序