代码之家  ›  专栏  ›  技术社区  ›  Anshu Verma

下一次输入后,Android文本从文本视图中消失

  •  0
  • Anshu Verma  · 技术社区  · 7 年前

    我是android studio的新手,我正在开发一个计算器应用程序,我的问题是每当我输入一个数字,然后选择一个运算符,文本视图中的数字就会消失,就像我想要的那样 2+4 2 + , 2. 从文本视图中消失,然后当我按下 4 + 从文本视图中消失,但结果是正确的

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                if (edt1 == null){
                    edt1.setText("");
                }else {
                    mValueOne = Float.parseFloat(edt1.getText() + "");
                    mAddition = true;
                    edt1.setText(null);
                }
            }
        });
    
        buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueOne = Float.parseFloat(edt1.getText() + "");
                mSubtract = true ;
                edt1.setText(null);
            }
        });
    
        buttonMul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueOne = Float.parseFloat(edt1.getText() + "");
                mMultiplication = true ;
                edt1.setText(null);
            }
        });
    
        buttonDivision.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueOne = Float.parseFloat(edt1.getText()+"");
                mDivision = true ;
                edt1.setText(null);
            }
        });
    
        buttonEqual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mValueTwo = Float.parseFloat(edt1.getText() + "");
    
                if (mAddition == true){
    
                    edt1.setText(mValueOne + mValueTwo +"");
                    mAddition=false;
                }
    
    
                if (mSubtract == true){
                    edt1.setText(mValueOne - mValueTwo+"");
                    mSubtract=false;
                }
    
                if (mMultiplication == true){
                    edt1.setText(mValueOne * mValueTwo+"");
                    mMultiplication=false;
                }
    
                if (mDivision == true){
                    edt1.setText(mValueOne / mValueTwo+"");
                    mDivision=false;
                }
            }
        });
    
        buttonC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText("");
            }
        });
    
        button10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt1.setText(edt1.getText()+".");
            }
        });
    }
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Martin Lund Andreas Engedal    7 年前

    您正在正确收集数据,但您一直将edt1设置为null,然后当您点击+edt1时,它将始终为null

     if (edt1 == null){
                edt1.setText("");
            }
    

    我建议使用2个edittext字段。

    然后在文本视图中显示结果?

    使用下面的代码可以实现类似的功能 enter image description here

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
    
        <EditText
            android:id="@+id/first_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="0.40"/>
    
        <EditText
            android:id="@+id/second_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="0.40"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" = "
            android:layout_weight="0.10"/>
    
        <TextView
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="0.20"/>
    
    </LinearLayout>
    

        2
  •  0
  •   Evgeniy    7 年前

    edt1.append(someString);
    
        3
  •  0
  •   Fabio Piunti    7 年前

    每次单击按钮时,您都将编辑文本设置为null。这将清除其中的所有文本。如果要添加文本,请使用append方法。