Android studio實(shí)現(xiàn)加法軟件
本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)加法軟件的具體代碼,供大家參考,具體內(nèi)容如下
布局為簡(jiǎn)單的線(xiàn)性布局,用一個(gè)EditText來(lái)接收輸入的結(jié)果
用Random來(lái)獲得兩個(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以?xún)?nèi)的加法" android:textSize="30sp" android:textColor="#000"/> <EditText android:id="@+id/et_1" 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:id="@+id/et_2" 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:id="@+id/et_3" 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:id="@+id/btn_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="運(yùn)算結(jié)果" android:textSize="30sp"/> <Button android:id="@+id/btn_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一題" android:textSize="30sp" android:layout_marginLeft="30dp"/> </LinearLayout> </LinearLayout>

總代碼
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;
@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);
//通過(guò)View.OnClickListener接口來(lái)實(shí)現(xiàn)給按鈕添加監(jiān)聽(tīng)事件
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)算正確
//重寫(xiě)View.OnClickListener中的onClick方法
@Override
public void onClick(View v) {
//定義一個(gè)String屬性的變量來(lái)接收mEdit3文本框中輸入的元素
String dite3=mEdit3.getText().toString();
//通過(guò)正則表達(dá)式來(lái)判斷輸入的數(shù)值是否為數(shù)值類(lèi)型
Pattern pattern=Pattern.compile("[0-9]*");
Matcher matcher=pattern.matcher(dite3);
//通過(guò)switch方法判斷點(diǎn)擊的時(shí)哪個(gè)按鈕
switch (v.getId()) {
case R.id.btn_1:
//用equals方法來(lái)判斷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)擊專(zhuān)題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小程序
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- android計(jì)算器簡(jiǎn)單實(shí)現(xiàn)代碼
- 簡(jiǎn)單實(shí)現(xiàn)Android計(jì)算器功能
- Android實(shí)戰(zhàn)教程第一篇之最簡(jiǎn)單的計(jì)算器
- Android計(jì)算器編寫(xiě)代碼
- 從零開(kāi)始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
相關(guān)文章
一分鐘實(shí)現(xiàn)Android遮罩引導(dǎo)視圖
本文通過(guò)一分鐘的時(shí)間教大家實(shí)現(xiàn)Android遮罩引導(dǎo)視圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Android實(shí)現(xiàn)動(dòng)態(tài)曲線(xiàn)繪制
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)曲線(xiàn)繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Android入門(mén)之onTouchEvent觸碰事件的示例詳解
今天給大家?guī)?lái)的是TouchListener與OnTouchEvent的比較,以及多點(diǎn)觸碰的知識(shí)點(diǎn)!?文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12
android Animation監(jiān)聽(tīng)器AnimationListener的使用方法)
AnimaitonListener的使用方法主要是在Animation上設(shè)置一個(gè)監(jiān)聽(tīng)器,下面通過(guò)一個(gè)實(shí)例說(shuō)明它的使用方法2013-11-11
Android 判斷是否能真正上網(wǎng)的實(shí)例詳解
這篇文章主要介紹了Android 判斷是否能真正上網(wǎng)的實(shí)例詳解相關(guān)資料,希望通過(guò)本文大家能夠掌握判斷是否上網(wǎng)的方法,需要的朋友可以參考下2017-10-10
Kotlin對(duì)象的懶加載方式by?lazy?與?lateinit?異同詳解
這篇文章主要為大家介紹了Kotlin對(duì)象的懶加載方式by?lazy?與?lateinit?異同詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android實(shí)現(xiàn)橫豎屏切換的實(shí)例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)橫豎屏切換的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Android通過(guò)BLE傳輸文件遇到問(wèn)題解決
這篇文章主要為大家介紹了Android通過(guò)BLE傳輸文件遇到問(wèn)題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
android事件分發(fā)機(jī)制的實(shí)現(xiàn)原理
本篇文章主要介紹了android事件分發(fā)機(jī)制的實(shí)現(xiàn)原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

