Android studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器功能
本文實(shí)例為大家分享了android studio簡(jiǎn)易運(yùn)算器,供大家參考,具體內(nèi)容如下
JAVA語(yǔ)句代碼塊:
package com.example.douyingming; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; import android.view.View; import android.view.View.OnClickListener; public class CompuActivity extends AppCompatActivity implements OnClickListener { ? ? private ?String opr="+"; ?//記錄當(dāng)前運(yùn)算符,最初運(yùn)算符為+,可以更改 ? ? private EditText et1,et2; ? ? private TextView tv; ? ? private Button bt; ? ? private RadioGroup rg; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.samplecompu); ? ? ? ? ? ?//設(shè)置匹配頁(yè)面為samplecompu ? ? ? ? rg=(RadioGroup)findViewById(R.id.radioGroup1); //單選按鈕組的按鈕匹配 ? ? ? ? et1=(EditText)findViewById(R.id.editText1);//匹配第一個(gè)編輯框的按鈕 ? ? ? ? et2=(EditText)findViewById(R.id.editText2);//匹配第二個(gè)編輯框的按鈕 ? ? ? ? tv=(TextView)findViewById(R.id.textView1);//匹配顯示文本 ? ? ? ? bt=(Button)findViewById(R.id.button1);//獲得按鈕 ? ? ? ? bt.setOnClickListener(this); ? //設(shè)置計(jì)算按鈕的監(jiān)聽(tīng)器 ? ? ? ? rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){ ? ?//設(shè)置單選按鈕監(jiān)聽(tīng)器,獲得單擊時(shí)執(zhí)行 ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onCheckedChanged(RadioGroup group,int checkedId){ ? ? ? ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ? ? ? RadioButton rb=(RadioButton)findViewById(checkedId); //設(shè)定RadioButton類(lèi)rb,獲得checkedId ? ? ? ? ? ? ? ? opr=rb.getText().toString(); ?//把rb強(qiáng)轉(zhuǎn)為String類(lèi)型,賦給opr ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? @Override ? ? public void onClick(View v){ ? ? //定義點(diǎn)擊事件方法 ? ? ? ? int sum,num1,num2; ? ? ? ? ? ? //定義三個(gè)變量 ? ? ? ? num1 = Integer.parseInt(et1.getText().toString());//接收et1文本框中的數(shù)字(強(qiáng)轉(zhuǎn)為數(shù)字類(lèi)型) ? ? ? ? num2=Integer.parseInt(et2.getText().toString());//接收et2文本框中的數(shù)字(強(qiáng)轉(zhuǎn)為數(shù)字類(lèi)型) ? ? ? ? if (opr.equals("+")){ ? ?//+法 ? ? ? ? ? ? sum=num1+num2; ? ? ? ? }else if (opr.equals("-")){//減法 ? ? ? ? ? ? sum=num1-num2; ? ? ? ? }else if(opr.equals("*")){//乘法 ? ? ? ? ? ? sum=num1*num2; ? ? ? ? }else{//如果不是加減乘,就執(zhí)行除法 ? ? ? ? ? ? sum=num1/num2; ? ? ? ? } ? ? ? ? tv.setText(String.valueOf(sum)); ?//顯示setText文本 ? ? } }
xml代碼塊
<?xml version="1.0" encoding="utf-8"?> <!-- 設(shè)置布局為垂直 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical"> ? ? <!-- ? ? 增加id為editText1 ? ? 寬度剛度匹配自己 ? ? 文本顯示寬度為8 ? ? * ? ? 輸入的類(lèi)型為數(shù)字 ? ? 設(shè)置該文本框焦點(diǎn) ? ? --> ? ? <EditText ? ? ? ? android:id="@+id/editText1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:ems="8" ? ? ? ? android:hint="請(qǐng)輸入數(shù)字" ? ? ? ? android:inputType="number" ? ? ? ? android:text=""> ? ? ? ? <requestFocus/> ? ? </EditText> ? ? <!-- 設(shè)置一個(gè)RadioGroup組 ? ? 增加一個(gè)id ? ? 寬度匹配父類(lèi) ? ? 高度等于自己 ? ? 該組水平排列 ? ? --> ? ? <RadioGroup ? ? ? ? android:id="@+id/radioGroup1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <!-- ? ? ? ? 增加id ? ? ? ? 寬和高自己匹配 ? ? ? ? 選擇為選中狀態(tài) ? ? ? ? 文本內(nèi)容為+ ? ? ? ? (四個(gè)RadioButton內(nèi)容相同) ? ? ? ? --> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio0" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:checked="true" ? ? ? ? ? ? android:text="+"/> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio1" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:checked="true" ? ? ? ? ? ? android:text="-"/> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio2" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:checked="true" ? ? ? ? ? ? android:text="*"/> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio3" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:checked="true" ? ? ? ? ? ? android:text="/"/> ? ? </RadioGroup> ? ? <!-- ? ? 增加id為editText2 ? ? 寬度剛度匹配自己 ? ? 文本顯示寬度為8 ? ? * ? ? 輸入的類(lèi)型為數(shù)字 ? ? --> ? ? <EditText ? ? ? ? android:id="@+id/editText2" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:ems="8" ? ? ? ? android:hint="請(qǐng)輸入數(shù)2" ? ? ? ? android:inputType="number" ? ? ? ? android:text=""/> ? ? <!-- ? ? * ? ? 文本為= ? ? --> ? ? <Button ? ? ? ? android:id="@+id/button1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="="/> ? ? <TextView ? ? ? ? android:id="@+id/textView1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text=""/> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- android studio實(shí)現(xiàn)計(jì)算器
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)
- Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
- Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器APP
- Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)
- Android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器
相關(guān)文章
Android中系統(tǒng)默認(rèn)輸入法設(shè)置的方法(輸入法的顯示和隱藏)
這篇文章主要介紹了Android中系統(tǒng)默認(rèn)輸入法設(shè)置的方法(輸入法的顯示和隱藏)的相關(guān)資料,需要的朋友可以參考下2016-01-01Android studio 生成帶Kotlin文檔的實(shí)現(xiàn)方式
這篇文章主要介紹了Android studio 生成帶Kotlin文檔的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android 圓角 ImageView類(lèi)可設(shè)置弧度(代碼簡(jiǎn)單)
這篇文章主要介紹了Android 圓角 ImageView類(lèi)可設(shè)置弧度 的相關(guān)資料,需要的朋友可以參考下2016-03-03Android編程實(shí)現(xiàn)顯示在標(biāo)題上的進(jìn)度條功能【附源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)顯示在標(biāo)題上的進(jìn)度條功能,涉及Android界面布局及相關(guān)組件屬性設(shè)置技巧,并附帶完整實(shí)例源碼供讀者下載參考,需要的朋友可以參考下2018-01-01Android編程之匿名內(nèi)部類(lèi)與回調(diào)函數(shù)用法分析
這篇文章主要介紹了Android編程之匿名內(nèi)部類(lèi)與回調(diào)函數(shù)用法,結(jié)合實(shí)例形式分析了Android編程中所涉及的java匿名內(nèi)部類(lèi)與回調(diào)函數(shù)的概念、定義、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-10-10Android中加載網(wǎng)絡(luò)資源時(shí)的優(yōu)化可使用(線程+緩存)解決
Android 中加載網(wǎng)絡(luò)資源時(shí)的優(yōu)化;基本的思路是線程+緩存來(lái)解決,具體解決思路如下,有類(lèi)似情況的朋友可以參考下哈2013-06-06Android ZxingPlus精簡(jiǎn)的二維碼框架示例代碼
下面小編就為大家分享一篇Android ZxingPlus精簡(jiǎn)的二維碼框架示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Android Canvas自定義實(shí)現(xiàn)時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了Android Canvas自定義實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12