網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了android studio簡易運算器,供大家參考,具體內(nèi)容如下
JAVA語句代碼塊:
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)前運算符,最初運算符為+,可以更改
? ? 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è)置匹配頁面為samplecompu
? ? ? ? rg=(RadioGroup)findViewById(R.id.radioGroup1); //單選按鈕組的按鈕匹配
? ? ? ? et1=(EditText)findViewById(R.id.editText1);//匹配第一個編輯框的按鈕
? ? ? ? et2=(EditText)findViewById(R.id.editText2);//匹配第二個編輯框的按鈕
? ? ? ? tv=(TextView)findViewById(R.id.textView1);//匹配顯示文本
? ? ? ? bt=(Button)findViewById(R.id.button1);//獲得按鈕
? ? ? ? bt.setOnClickListener(this); ? //設(shè)置計算按鈕的監(jiān)聽器
? ? ? ? rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){ ? ?//設(shè)置單選按鈕監(jiān)聽器,獲得單擊時執(zhí)行
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onCheckedChanged(RadioGroup group,int checkedId){
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? RadioButton rb=(RadioButton)findViewById(checkedId); //設(shè)定RadioButton類rb,獲得checkedId
? ? ? ? ? ? ? ? opr=rb.getText().toString(); ?//把rb強(qiáng)轉(zhuǎn)為String類型,賦給opr
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? @Override
? ? public void onClick(View v){ ? ? //定義點擊事件方法
? ? ? ? int sum,num1,num2; ? ? ? ? ? ? //定義三個變量
? ? ? ? num1 = Integer.parseInt(et1.getText().toString());//接收et1文本框中的數(shù)字(強(qiáng)轉(zhuǎn)為數(shù)字類型)
? ? ? ? num2=Integer.parseInt(et2.getText().toString());//接收et2文本框中的數(shù)字(強(qiáng)轉(zhuǎn)為數(shù)字類型)
? ? ? ? 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 ? ? * ? ? 輸入的類型為數(shù)字 ? ? 設(shè)置該文本框焦點 ? ? --> ? ? <EditText ? ? ? ? android:id="@+id/editText1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:ems="8" ? ? ? ? android:hint="請輸入數(shù)字" ? ? ? ? android:inputType="number" ? ? ? ? android:text=""> ? ? ? ? <requestFocus/> ? ? </EditText> ? ? <!-- 設(shè)置一個RadioGroup組 ? ? 增加一個id ? ? 寬度匹配父類 ? ? 高度等于自己 ? ? 該組水平排列 ? ? --> ? ? <RadioGroup ? ? ? ? android:id="@+id/radioGroup1" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:orientation="horizontal"> ? ? ? ? <!-- ? ? ? ? 增加id ? ? ? ? 寬和高自己匹配 ? ? ? ? 選擇為選中狀態(tài) ? ? ? ? 文本內(nèi)容為+ ? ? ? ? (四個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 ? ? * ? ? 輸入的類型為數(shù)字 ? ? --> ? ? <EditText ? ? ? ? android:id="@+id/editText2" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:ems="8" ? ? ? ? android:hint="請輸入數(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>
原文鏈接:https://blog.csdn.net/weixin_48524820/article/details/115009058
相關(guān)推薦
- 2022-09-15 教你如何將應(yīng)用從docker-compose遷移到k8s中_docker
- 2022-10-20 C++分析如何用虛析構(gòu)與純虛析構(gòu)處理內(nèi)存泄漏_C 語言
- 2022-03-14 @Builder 實際參數(shù)列表和形式參數(shù)列表長度不同
- 2023-07-28 async await 寫法
- 2022-03-23 詳解C#異步多線程使用中的常見問題_C#教程
- 2022-07-01 python讀取nc數(shù)據(jù)并繪圖的方法實例_python
- 2022-09-16 Pandas數(shù)據(jù)連接pd.concat的實現(xiàn)_python
- 2022-03-03 解決Typescript報錯:Property 'style' does not exist on
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支