久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Android studio實現加法軟件

瀏覽:6日期:2022-09-25 10:22:48

本文實例為大家分享了Android studio實現加法軟件的具體代碼,供大家參考,具體內容如下

布局為簡單的線性布局,用一個EditText來接收輸入的結果用Random來獲得兩個隨機數

布局文件:

<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' tools:context='.jiafa_2_28Activity' android:orientation='vertical' android:gravity='center_horizontal'> <TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='30以內的加法' android:textSize='30sp' android:textColor='#000'/> <EditText android: android:layout_width='200dp' android:layout_height='wrap_content' android:textSize='25sp' android:layout_marginTop='10dp' android:enabled='false' android:textColor='#000' android:gravity='center'/> <EditText android: android:layout_width='200dp' android:layout_height='wrap_content' android:textSize='25sp' android:enabled='false' android:layout_marginTop='10dp' android:textColor='#000' android:gravity='center'/> <EditText android: android:layout_width='150dp' android:layout_height='wrap_content' android:textSize='25sp' android:textColor='#000' android:text='' android:gravity='center'/> <LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='horizontal' android:gravity='center_horizontal'> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='運算結果' android:textSize='30sp'/> <Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='下一題' android:textSize='30sp' android:layout_marginLeft='30dp'/> </LinearLayout></LinearLayout>

Android studio實現加法軟件

總代碼

public class jiafa_2_28Activity extends AppCompatActivity implements View.OnClickListener { private Button mBtn1,mBtn2; private EditText mEdit1,mEdit2,mEdit3; private Random mRandom; private int x,y; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jiafa_2_28); mBtn1=findViewById(R.id.btn_1); mBtn2=findViewById(R.id.btn_2); mEdit1=findViewById(R.id.et_1); mEdit2=findViewById(R.id.et_2); mEdit3=findViewById(R.id.et_3); mBtn1.setOnClickListener(this); mBtn2.setOnClickListener(this); mRandom=new Random(); myRandom(); mEdit3.requestFocus(); } private void myRandom(){ x=mRandom.nextInt(30)+1; y=mRandom.nextInt(30)+1; mEdit1.setText(String.valueOf(x)); mEdit2.setText(String.valueOf(y)); } @Override public void onClick(View v) { String dite3=mEdit3.getText().toString(); Pattern pattern=Pattern.compile('[0-9]*'); Matcher matcher=pattern.matcher(dite3); switch (v.getId()) { case R.id.btn_1: if(matcher.matches()){ if(''.equals(dite3)){Toast.makeText(jiafa_2_28Activity.this,'請輸入答案',Toast.LENGTH_SHORT).show();mEdit3.requestFocus(); }else { int result = Integer.parseInt(dite3); if (result == x + y) {Toast.makeText(jiafa_2_28Activity.this, '恭喜你,回答正確', Toast.LENGTH_SHORT).show(); } else {Toast.makeText(jiafa_2_28Activity.this, '回答錯誤', Toast.LENGTH_SHORT).show();mEdit3.setText(''); } } }else{ Toast.makeText(jiafa_2_28Activity.this,'輸入的是非整數',Toast.LENGTH_SHORT).show(); mEdit3.requestFocus(); } break; case R.id.btn_2: mEdit3.setText(''); myRandom(); break; } }}

代碼文件①

定義屬性,再依次獲取個控件的Id

private Button mBtn1,mBtn2;private EditText mEdit1,mEdit2,mEdit3;private Random mRandom;private int x,y;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jiafa_2_28); mBtn1=findViewById(R.id.btn_1); mBtn2=findViewById(R.id.btn_2); mEdit1=findViewById(R.id.et_1); mEdit2=findViewById(R.id.et_2); mEdit3=findViewById(R.id.et_3);//通過View.OnClickListener接口來實現給按鈕添加監聽事件 mBtn1.setOnClickListener(this); mBtn2.setOnClickListener(this); mRandom=new Random(); myRandom(); // 默認讓焦點定位到mEdit3空間上 mEdit3.requestFocus();}

代碼文件②

定義一個獲得隨機數的方法,給mEdit1和mEdit2賦予1~30之間的一個隨機整數

private void myRandom(){ x=mRandom.nextInt(30)+1; y=mRandom.nextInt(30)+1; mEdit1.setText(String.valueOf(x)); mEdit2.setText(String.valueOf(y));}

代碼文件③

設置點擊事件,并判斷是否運算正確

//重寫View.OnClickListener中的onClick方法@Overridepublic void onClick(View v) {//定義一個String屬性的變量來接收mEdit3文本框中輸入的元素 String dite3=mEdit3.getText().toString(); //通過正則表達式來判斷輸入的數值是否為數值類型 Pattern pattern=Pattern.compile('[0-9]*'); Matcher matcher=pattern.matcher(dite3); //通過switch方法判斷點擊的時哪個按鈕 switch (v.getId()) { case R.id.btn_1: //用equals方法來判斷mEdit3中的內容是否為空,若為空則彈出Toast if(matcher.matches()){ if(''.equals(dite3)){ Toast.makeText(jiafa_2_28Activity.this,'請輸入答案',Toast.LENGTH_SHORT).show(); mEdit3.requestFocus(); }else { //將dite3獲取到的mEdit3的值qiang’zhu強轉為int型 int result = Integer.parseInt(dite3); if (result == x + y) { Toast.makeText(jiafa_2_28Activity.this, '恭喜你,回答正確', Toast.LENGTH_SHORT).show(); } else { Toast.makeText(jiafa_2_28Activity.this, '回答錯誤', Toast.LENGTH_SHORT).show(); mEdit3.setText(''); } } }else{ Toast.makeText(jiafa_2_28Activity.this,'輸入的是非整數',Toast.LENGTH_SHORT).show(); mEdit3.requestFocus(); } break; case R.id.btn_2: //若點擊下一題則清空mEdit3中的內容,并再調用myRandom獲取隨機數 mEdit3.setText(''); myRandom(); break; }}

更多計算器功能實現,請點擊專題: 計算器功能匯總 進行學習

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 涪陵区| 敦煌市| 天津市| 尚义县| 麻栗坡县| 卓资县| 和田市| 北海市| 博野县| 清原| 湛江市| 江川县| 上饶市| 和平区| 荆门市| 大石桥市| 日土县| 丹江口市| 白沙| 长沙县| 德保县| 晋中市| 浙江省| 汉源县| 阿勒泰市| 五大连池市| 商城县| 砚山县| 社会| 静海县| 方山县| 内丘县| 正蓝旗| 泽普县| 宁陕县| 仪陇县| 苏州市| 丹东市| 温宿县| 鄄城县| 望都县|