国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android studio實(shí)現(xiàn)加法軟件

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

本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)加法軟件的具體代碼,供大家參考,具體內(nèi)容如下

布局為簡(jiǎn)單的線性布局,用一個(gè)EditText來接收輸入的結(jié)果用Random來獲得兩個(gè)隨機(jī)數(shù)

布局文件:

<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以內(nèi)的加法' 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='運(yùn)算結(jié)果' 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實(shí)現(xiàn)加法軟件

總代碼

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,'請(qǐng)輸入答案',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, '回答錯(cuò)誤', Toast.LENGTH_SHORT).show();mEdit3.setText(''); } } }else{ Toast.makeText(jiafa_2_28Activity.this,'輸入的是非整數(shù)',Toast.LENGTH_SHORT).show(); mEdit3.requestFocus(); } break; case R.id.btn_2: mEdit3.setText(''); myRandom(); break; } }}

代碼文件①

定義屬性,再依次獲取個(gè)控件的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接口來實(shí)現(xiàn)給按鈕添加監(jiān)聽事件 mBtn1.setOnClickListener(this); mBtn2.setOnClickListener(this); mRandom=new Random(); myRandom(); // 默認(rèn)讓焦點(diǎn)定位到mEdit3空間上 mEdit3.requestFocus();}

代碼文件②

定義一個(gè)獲得隨機(jī)數(shù)的方法,給mEdit1和mEdit2賦予1~30之間的一個(gè)隨機(jī)整數(shù)

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

代碼文件③

設(shè)置點(diǎn)擊事件,并判斷是否運(yùn)算正確

//重寫View.OnClickListener中的onClick方法@Overridepublic void onClick(View v) {//定義一個(gè)String屬性的變量來接收mEdit3文本框中輸入的元素 String dite3=mEdit3.getText().toString(); //通過正則表達(dá)式來判斷輸入的數(shù)值是否為數(shù)值類型 Pattern pattern=Pattern.compile('[0-9]*'); Matcher matcher=pattern.matcher(dite3); //通過switch方法判斷點(diǎn)擊的時(shí)哪個(gè)按鈕 switch (v.getId()) { case R.id.btn_1: //用equals方法來判斷mEdit3中的內(nèi)容是否為空,若為空則彈出Toast if(matcher.matches()){ if(''.equals(dite3)){ Toast.makeText(jiafa_2_28Activity.this,'請(qǐng)輸入答案',Toast.LENGTH_SHORT).show(); mEdit3.requestFocus(); }else { //將dite3獲取到的mEdit3的值qiang’zhu強(qiáng)轉(zhuǎn)為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, '回答錯(cuò)誤', Toast.LENGTH_SHORT).show(); mEdit3.setText(''); } } }else{ Toast.makeText(jiafa_2_28Activity.this,'輸入的是非整數(shù)',Toast.LENGTH_SHORT).show(); mEdit3.requestFocus(); } break; case R.id.btn_2: //若點(diǎn)擊下一題則清空mEdit3中的內(nèi)容,并再調(diào)用myRandom獲取隨機(jī)數(shù) mEdit3.setText(''); myRandom(); break; }}

更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 汝城县| 郁南县| 洪洞县| 武宣县| 铜山县| 黎平县| 无为县| 张家港市| 蕉岭县| 上杭县| 广饶县| 交口县| 西吉县| 临泉县| 永丰县| 新源县| 桃园市| 来安县| 冕宁县| 牟定县| 嘉禾县| 岑巩县| 延津县| 忻城县| 乐陵市| 阿拉善左旗| 轮台县| 孟津县| 黄山市| 车致| 吴川市| 焉耆| 德兴市| 东宁县| 广西| 陕西省| 水城县| 三台县| 刚察县| 嘉善县| 盐城市|