欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器源碼

 更新時(shí)間:2022年05月20日 17:24:15   作者:大笨鐘的笨  
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

源碼:

布局樣式:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:orientation="horizontal"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:baselineAligned="false"
? ? android:background="@color/grey"
? ? android:columnCount="4"
? ? android:rowCount="6">
? ? <TextView
? ? ? ? android:id="@+id/textview_01"
? ? ? ? android:layout_columnSpan="4"
? ? ? ? android:layout_gravity="fill"
? ? ? ? android:gravity="right"
? ? ? ? android:layout_marginLeft="5dp"
? ? ? ? android:layout_marginRight="5dp"
? ? ? ? android:background="@color/grey"
? ? ? ? android:paddingRight="20dp"
? ? ? ? android:paddingTop="40dp"
? ? ? ? android:text="0"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:textSize="88sp">
? ? </TextView>
? ? <Button
? ? ? ? android:id="@+id/button_return"
? ? ? ? android:layout_columnSpan="2"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="2dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="30dp"
? ? ? ? android:textStyle="bold"
? ? ? ? android:text="回退" />
? ? <Button
? ? ? ? android:id="@+id/button_clear"
? ? ? ? android:layout_columnSpan="2"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="2dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="30dp"
? ? ? ? android:textStyle="bold"
? ? ? ? android:text="清空"/>
? ? <Button
? ? ? ? android:id="@+id/button_add"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="+"/>
? ? <Button
? ? ? ? android:id="@+id/button_01"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="1"/>
? ? <Button
? ? ? ? android:id="@+id/button_02"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="2"/>
? ? <Button
? ? ? ? android:id="@+id/button_03"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="3"/>
? ? <Button
? ? ? ? android:id="@+id/button_reduce"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="-"/>
? ? <Button
? ? ? ? android:id="@+id/button_04"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="4"/>
? ? <Button
? ? ? ? android:id="@+id/button_05"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="5"/>
? ? <Button
? ? ? ? android:id="@+id/button_06"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="6"/>
? ? <Button
? ? ? ? android:id="@+id/button_ride"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="*"/>
? ? <Button
? ? ? ? android:id="@+id/button_07"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="7"/>
? ? <Button
? ? ? ? android:id="@+id/button_08"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="8"/>
? ? <Button
? ? ? ? android:id="@+id/button_09"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="9"/>
? ? <Button
? ? ? ? android:id="@+id/button_except"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="/"/>
? ? <Button
? ? ? ? android:id="@+id/button_spot"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="."/>
? ? <Button
? ? ? ? android:id="@+id/button_00"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="0"/>
? ? <Button
? ? ? ? android:id="@+id/button_equal"
? ? ? ? android:layout_columnSpan="1"
? ? ? ? android:gravity="center"
? ? ? ? android:layout_columnWeight="1"
? ? ? ? android:layout_rowWeight="1"
? ? ? ? android:layout_margin="1dp"
? ? ? ? android:background="@color/white"
? ? ? ? android:textSize="50dp"
? ? ? ? android:text="="/>
</GridLayout>

Java部分源碼:

package com.example.pyfcalculator;
?
import androidx.appcompat.app.AppCompatActivity;
?
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
?
? ? TextView textView;
? ? Button button_return,button_00,button_01,button_02,button_03,
? ? ? ? ? ? button_04,button_05,button_06,button_07,button_08,button_09,button_add,
? ? ? ? ? ? button_clear,button_equal,button_except,button_reduce,button_ride,button_spot;
?
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? initViewAndListener();
? ? }
?
?
? ? //注冊(cè)監(jiān)聽(tīng)方法
? ? protected void initViewAndListener(){
? ? ? ? button_return=findViewById(R.id.button_return);
? ? ? ? button_00=findViewById(R.id.button_00);
? ? ? ? button_01=findViewById(R.id.button_01);
? ? ? ? button_02=findViewById(R.id.button_02);
? ? ? ? button_03=findViewById(R.id.button_03);
? ? ? ? button_04=findViewById(R.id.button_04);
? ? ? ? button_05=findViewById(R.id.button_05);
? ? ? ? button_06=findViewById(R.id.button_06);
? ? ? ? button_07=findViewById(R.id.button_07);
? ? ? ? button_08=findViewById(R.id.button_08);
? ? ? ? button_09=findViewById(R.id.button_09);
? ? ? ? button_add=findViewById(R.id.button_add);
? ? ? ? button_clear=findViewById(R.id.button_clear);
? ? ? ? button_equal=findViewById(R.id.button_equal);
? ? ? ? button_except=findViewById(R.id.button_except);
? ? ? ? button_reduce=findViewById(R.id.button_reduce);
? ? ? ? button_ride=findViewById(R.id.button_ride);
? ? ? ? button_spot=findViewById(R.id.button_spot);
?
? ? ? ? button_return.setOnClickListener(this);
? ? ? ? button_00.setOnClickListener(this);
? ? ? ? button_01.setOnClickListener(this);
? ? ? ? button_02.setOnClickListener(this);
? ? ? ? button_03.setOnClickListener(this);
? ? ? ? button_04.setOnClickListener(this);
? ? ? ? button_05.setOnClickListener(this);
? ? ? ? button_06.setOnClickListener(this);
? ? ? ? button_07.setOnClickListener(this);
? ? ? ? button_08.setOnClickListener(this);
? ? ? ? button_09.setOnClickListener(this);
? ? ? ? button_add.setOnClickListener(this);
? ? ? ? button_clear.setOnClickListener(this);
? ? ? ? button_equal.setOnClickListener(this);
? ? ? ? button_except.setOnClickListener(this);
? ? ? ? button_reduce.setOnClickListener(this);
? ? ? ? button_ride.setOnClickListener(this);
? ? ? ? button_spot.setOnClickListener(this);
?
? ? ? ? textView=findViewById(R.id.textview_01);
? ? }
?
? ? private String num_in_now;//記錄當(dāng)前結(jié)果
? ? private boolean flag=false;//記錄計(jì)算器狀態(tài)
? ? int now_num=0;
?
? ? @Override
? ? public void onClick(View view) {
?
? ? ? ? //獲取文本框內(nèi)容
? ? ? ? String currentText=textView.getText().toString();
?
? ? ? ? switch (view.getId()){
? ? ? ? ? ? case R.id.button_00:
? ? ? ? ? ? ? ? if ((textView.getText().toString().equals("0")))
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? isflag("0");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_01:
? ? ? ? ? ? ? ? isflag("1");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_02:
? ? ? ? ? ? ? ? isflag("2");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_03:
? ? ? ? ? ? ? ? isflag("3");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_04:
? ? ? ? ? ? ? ? isflag("4");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_05:
? ? ? ? ? ? ? ? isflag("5");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_06:
? ? ? ? ? ? ? ? isflag("6");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_07:
? ? ? ? ? ? ? ? isflag("7");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_08:
? ? ? ? ? ? ? ? isflag("8");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_09:
? ? ? ? ? ? ? ? isflag("9");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_add:
// ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"+");
? ? ? ? ? ? ? ? now_num++;
? ? ? ? ? ? ? ? if(now_num>1){
? ? ? ? ? ? ? ? ? ? equal();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? isflag("+");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_except:
// ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"/");
? ? ? ? ? ? ? ? now_num++;
? ? ? ? ? ? ? ? if(now_num>1){
? ? ? ? ? ? ? ? ? ? equal();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? isflag("/");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_spot:
// ? ? ? ? ? ? ? ?textView.setText(textView.getText()+".");
? ? ? ? ? ? ? ? isflag(".");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_reduce:
// ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"-");
? ? ? ? ? ? ? ? now_num++;
? ? ? ? ? ? ? ? if(now_num>1){
? ? ? ? ? ? ? ? ? ? equal();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? isflag("-");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_ride:
// ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"*");
? ? ? ? ? ? ? ? now_num++;
? ? ? ? ? ? ? ? if(now_num>1){
? ? ? ? ? ? ? ? ? ? equal();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? isflag("*");
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_clear:
? ? ? ? ? ? ? ? textView.setText("0");
? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? ? ? now_num=0;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_return:
? ? ? ? ? ? ? ? if(TextUtils.isEmpty(textView.getText())){
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (currentText.substring(0,currentText.length()-1).length()>0){
? ? ? ? ? ? ? ? ? ? textView.setText(currentText.substring(0,currentText.length()-1));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else{
? ? ? ? ? ? ? ? ? ? textView.setText("0");
? ? ? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? ? ? ? ? now_num=0;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.button_equal:
? ? ? ? ? ? ? ? if(TextUtils.isEmpty(textView.getText())){
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? equal();
? ? ? ? ? ? ? ? flag=false;
? ? ? ? ? ? ? ? now_num=0;
? ? ? ? ? ? ? ? break;
? ? ? ? }
?
? ? }
?
? ? private void equal(){
? ? ? ? textView.setText(operation(textView.getText().toString()));
? ? }
?
? ? //運(yùn)算方法
? ? private String operation(String string){
? ? ? ? int index;
? ? ? ? double num_1,num_2;
? ? ? ? if (string.contains("+")){
? ? ? ? ? ? index=string.indexOf("+");
? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index));
? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length()));
? ? ? ? ? ? return num_1+num_2+"";
? ? ? ? }
? ? ? ? if (string.contains("-")){
? ? ? ? ? ? index=string.indexOf("-");
? ? ? ? ? ? if(index==0){
? ? ? ? ? ? ? ? String string_2=string.substring(1,string.length());
? ? ? ? ? ? ? ? int index_2=string_2.indexOf("-");
? ? ? ? ? ? ? ? num_1= Double.parseDouble(string_2.substring(0,index_2));
? ? ? ? ? ? ? ? num_2= Double.parseDouble(string_2.substring(index_2+1,string_2.length()));
? ? ? ? ? ? ? ? return (-(num_1+num_2))+"";
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index));
? ? ? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length()));
? ? ? ? ? ? ? ? return num_1-num_2+"";
? ? ? ? ? ? }
?
? ? ? ? }
? ? ? ? if (string.contains("*")){
? ? ? ? ? ? index=string.indexOf("*");
? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index));
? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length()));
? ? ? ? ? ? return num_1*num_2+"";
? ? ? ? }
? ? ? ? if (string.contains("/")){
? ? ? ? ? ? index=string.indexOf("/");
? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index));
? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length()));
? ? ? ? ? ? return num_1/num_2+"";
? ? ? ? }
? ? ? ? return "error!!!";
? ? }
?
? ? //判斷0要不要被替換 看flag的狀態(tài) 輸入數(shù)字
? ? private void isflag(String info){
? ? ? ? if (flag){
? ? ? ? ? ? textView.setText(textView.getText()+info);
? ? ? ? }else{
? ? ? ? ? ? if(info.equals(".")||info.equals("+")||info.equals("-")||info.equals("*")||info.equals("/")){
? ? ? ? ? ? ? ? textView.setText(textView.getText()+info);
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? textView.setText(info);
? ? ? ? ? ? }
? ? ? ? ? ? flag=true;
? ? ? ? }
? ? }
?
}

其余資源沒(méi)有太大難度,就不一展現(xiàn)了!

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

相關(guān)文章

最新評(píng)論