android里如何让我写的计算器完美运行


//想做一个win10  那样的计算器,有几个功能不会做,诸位看看能不能帮我解决
//(Button) b1,b2,b3,b4,b5功能不会实现,还有除法有问题
//android java


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="70sp"
        android:hint="请输入" 
        android:textSize="30sp"
        android:layout_gravity="center"        
        android:background="@drawable/bg" 
        android:id="@+id/t"
        //输入输出框
       />
        
    <TableLayout
        android:layout_marginTop="40sp" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        > 
            <TableLayout
         
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        > 
    <TableRow
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="5sp"
        >
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="根号"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b1"
    //开平方根
    />
                          <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="平方"   
     android:layout_marginRight="5sp"
     android:id="@+id/b2"
//数的的平方
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="1/x"   
     android:layout_marginRight="5sp"
     android:id="@+id/b3"
//变分数
    />
    </TableRow>
    
    
    <TableRow
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="5sp"
        >
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="%"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b4"
//百分号
    />
                          <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="清除"   
     android:layout_marginRight="5sp"
     android:id="@+id/b5"
//相当于CE
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="退格"   
     android:layout_marginRight="5sp"
     android:id="@+id/b6"
//相当于C
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="/"
     android:layout_marginRight="5sp"
     android:id="@+id/b7"
    />
      </TableRow>   
     

       <TableRow
        android:layout_width="wrap_content"
      android:layout_height="wrap_content"
        android:layout_marginBottom="5sp"
        >
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="7" 
     android:textSize="20sp"  
     android:layout_marginRight="5sp"
     android:id="@+id/b8"
    />
                          <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="8"
     android:textSize="20sp"     
     android:layout_marginRight="5sp"
     android:id="@+id/b9"
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="9"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b10"
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="×"
     android:textSize="25sp"    
     android:layout_marginRight="5sp"
     android:id="@+id/b11"
    />
             </TableRow >
                
       <TableRow
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="5sp"
        >
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="4"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b12"
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="5"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b13"
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="6"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b14"
    />
                          <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="-" 
     android:textSize="25sp"  
     android:layout_marginRight="5sp"
     android:id="@+id/b15"
    />
      </TableRow>
      
       
       <TableRow
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="5sp"
        >
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="1"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b16"
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="2" 
     android:textSize="20sp"   
     android:layout_marginRight="5sp"
     android:id="@+id/b17"
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="3"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b18"
    />
                          <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="+"
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b19"
    />
             </TableRow>
     <TableRow
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="5sp"
        >
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="0"
     android:textSize="20sp"   
     android:layout_marginRight="5sp"
     android:id="@+id/b20"
    />
             <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="."
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b21"
    />
           <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="="
     android:textSize="20sp"
     android:layout_marginRight="5sp"
     android:id="@+id/b22"
    />
             </TableRow>         
      </TableLayout >
</TableLayout>

</LinearLayout>







///java 文件
package com.example.test1;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
 
 
public class MainActivity extends Activity implements View.OnClickListener{
    //数字0-9
    private Button n_0;
    private Button n_1;
    private Button n_2;
    private Button n_3;
    private Button n_4;
    private Button n_5;
    private Button n_6;
    private Button n_7;
    private Button n_8;
    private Button n_9;
 
    //运算符
    private Button y_j;//+
    private Button y_jj;//-
    private Button y_c;//*
    private Button y_cc;//除
    private Button y_d;//=
    private Button y_x;//小数点
 
    //清除
    private  Button det;
 
     boolean clean;//清空标识
 
 
    //结果显示集
    private EditText editText;
 
 
    EditText t;
    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //数字0——9实例化
        n_0=(Button)findViewById(R.id.b20);
        n_1=(Button)findViewById(R.id.b16);
        n_2=(Button)findViewById(R.id.b17);
        n_3=(Button)findViewById(R.id.b18);
        n_4=(Button)findViewById(R.id.b12);
        n_5=(Button)findViewById(R.id.b13);
        n_6=(Button)findViewById(R.id.b14);
        n_7=(Button)findViewById(R.id.b8);
        n_8=(Button)findViewById(R.id.b9);
        n_9=(Button)findViewById(R.id.b10);
 
        //运算符实例化
        y_j=(Button)findViewById(R.id.b19);//加
        y_jj=(Button)findViewById(R.id.b15);//减
        y_c=(Button)findViewById(R.id.b11);//乘
        y_cc=(Button)findViewById(R.id.b7);//除
        y_d=(Button)findViewById(R.id.b22);//等
        y_x=(Button)findViewById(R.id.b21);//小数点
        det=(Button)findViewById(R.id.b5);//清除
 
        //结果显示集
        editText= (EditText)findViewById(R.id.t);
 
        //添加时间点击时间
        n_0.setOnClickListener( this);
        n_1.setOnClickListener(this);
        n_2.setOnClickListener( this);
        n_3.setOnClickListener( this);
        n_4.setOnClickListener(this);
        n_5.setOnClickListener( this);
        n_6.setOnClickListener( this);
        n_7.setOnClickListener(this);
        n_8.setOnClickListener( this);
        n_9.setOnClickListener( this);
 
        y_j.setOnClickListener( this);
        y_jj.setOnClickListener( this);
        y_c.setOnClickListener( this);
        y_cc.setOnClickListener( this);
        y_x.setOnClickListener( this);
        y_d.setOnClickListener( this);
        det.setOnClickListener( this);
 
    }
    //读取每个按钮内容
    public void onClick(View view){
        //获取文本内容
        String  input=editText.getText().toString();
       switch (view.getId()){
           case R.id.b20:
           case R.id.b16:
           case R.id.b17:
           case R.id.b18:
           case R.id.b12:
           case R.id.b13:
           case R.id.b14:
           case R.id.b8:
           case R.id.b9:
           case R.id.b10:
           case R.id.b21://小数点
               if(clean){
                    clean=false;
                   editText.setText("");//赋值为空
               }
               editText.setText(input+((Button)view).getText()+"");//结果集就是本身
               break;
           /*
           case R.id.b1:Math.sqrt(double );
           case R.id.b2:
           case R.id.b3:
           case R.id.b4:
           case R.id.b5:
           */
           case R.id.b19:
           case R.id.b15:
           case R.id.b11:
           case R.id.b7://除
               if(clean){
                   clean=false;
                   input="";
                   editText.setText("");
               }
               editText.setText(input+" "+((Button)view).getText()+" ");
                break;
           case R.id.b5://清除
               if(clean){
                   clean=false;
                   input="";
                   editText.setText("");
               }else  if(input!=null || !input.equals("")){
                   //如果获取内容为空
                   editText.setText(input.substring(0,input.length() - 1 ));//结果集为空
                   break;
               }
                break;
           case  R.id.b22://运算结果=
               getResult();//调用处理结果方法
               break;
           
       }
    }
    //运算结果方法
    private void getResult(){
        String exp=editText.getText().toString();//获取文本框内容
        if(exp==null||exp.equals("")){
            return;
        }
        if(!exp.contains("")){
            return;
        }
        if(clean){
            clean=false;
            return;
        }
        clean=true;
        double result=0;
 
        //进行截取
        //运算符前的数字
        String s1=exp.substring(0,exp.indexOf(" "));
        //运算符
        String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
        //运算符后的数字
        String s2=exp.substring(exp.indexOf(" ")+3);
 
        if(!s1.equals("")&&!s2.equals("")){
            //如果包含小数点的运算
            double d1=Double.parseDouble(s1);//则数字都是double类型
            double d2=Double.parseDouble(s2);
 
            if(op.equals("+")){
                //如果是+
                result=d1+d2;
            }else if(op.equals("-")){
                    //如果是-
                    result=d1-d2;
            }else if(op.equals("*")){
                //如果是*
                result=d1*d2;
            }else if(op.equals("/")){
               if(d2==0){
                   //如果被除数是0
                   result=0;//则结果为0
               }/*else if(op.equals(""))*/
               else {
                   //否则执行正常运算
                   result=d1/d2;
               }
            }
            if(!s1.contains(".") &&!s2.contains(".")&&!op.equals("/")){
                //如果是整数类型
                int r=(int)result;//都是整形
                editText.setText(r+"");
            }else {
                editText.setText(result+"");
            }
        }else  if(!s1.equals("")&& s2.equals("")){
            //如果只输入运算符前的数字
            editText.setText(exp);//直接返回当前输入内容
        }else if (s1.equals("")&& !s2.equals("")){
            //如果是只输入运算符后面的数
            double d2 =Double.parseDouble(s2);
 
            //运算符当前没有输入数字
            if(op.equals("+")){
                result= 0 + d2;
            }else  if(op.equals("-")){
                result= 0 - d2;
            }else if (op.equals("*")){
                result= 0;
            }else  if(op.equals("/")){
                result= 0;
            }
            if(!s1.contains(".")&&!s2.contains(".")){
                int r=(int) result;
                editText.setText(r+"");
            }else {
                editText.setText(result+"");
            }
        }else {
            editText.setText("");
        }
    }
}


你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。