首页 > 解决方案 > 计算器应用程序问题 - 开头为 0 和数字中的点

问题描述

我一直在研究 android-studio 计算器。我是一个非常初学者,我有两个问题。首先,当我在我的应用程序中按“c”时,它在第一次点击时工作正常。然后,当我单击它并单击任何数字时,它会显示“0x”(x 是那个数字)。

我的第二个问题是如何只添加一个点?我尝试使用 String.contains() 但它没有用。我怎样才能解决这个问题?这是我的 XML:

<android.support.constraint.ConstraintLayout 
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=".MainActivity">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000"
    android:orientation="vertical"
    android:weightSum="6"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:inputType="none"
        android:padding="10dp"
        android:text="0"
        android:textAlignment="viewEnd"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:textSize="32sp"
        android:textStyle="normal"
        android:ellipsize="end"
        android:maxLines="1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">

        <Button
            android:id="@+id/cc"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="C"
            android:textColor="#d75700"
            android:textSize="30dp"
            android:onClick="nrClear"
            />

        <Button
            android:id="@+id/back"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="undo"
            android:textSize="20dp"
            android:onClick="goBack"/>

        <Button
            android:id="@+id/division"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="/"
            android:textSize="30dp"
            android:onClick="pressDivision"/>

        <Button
            android:id="@+id/multip"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="*"
            android:textSize="30dp"
            android:onClick="pressMultiplication"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">


        <Button
            android:id="@+id/nr1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1"
            android:textSize="30dp"
            android:onClick="firstNumber"/>

        <Button
            android:id="@+id/nr2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"
            android:textSize="30dp"
            android:onClick="secondNumber"/>

        <Button
            android:id="@+id/nr3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3"
            android:textSize="30dp"
            android:onClick="thirdNumber"/>

        <Button
            android:id="@+id/minus"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="-"
            android:textSize="30dp"
            android:onClick="pressMinus"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">


        <Button
            android:id="@+id/nr4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"
            android:textSize="30dp"
            android:onClick="fourthNumber"/>

        <Button
            android:id="@+id/nr5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="5"
            android:textSize="30dp"
            android:onClick="fifthNumber"/>

        <Button
            android:id="@+id/nr6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="6"
            android:textSize="30dp"
            android:onClick="sixthNumber"/>

        <Button
            android:id="@+id/plus"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="+"
            android:textSize="30dp"
            android:onClick="pressPlus"/>
    </LinearLayout>


    <LinearLayout
        android:layout_marginTop="0dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4">


        <Button
            android:id="@+id/nr7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="7"
            android:textSize="30dp"
            android:onClick="seventhNumber"/>

        <Button
            android:id="@+id/nr8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="8"
            android:textSize="30dp"
            android:onClick="eightNumber"/>

        <Button
            android:id="@+id/nr9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="9"
            android:textSize="30dp"
            android:onClick="ninthNumber"/>

        <Button
            android:id="@+id/equall"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="="
            android:textSize="30dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_marginTop="0dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4"
        android:textSize="30dp">

        <Button
            android:id="@+id/procent"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="%"
            android:textSize="30dp"
            android:onClick="pressProcent"/>

        <Button
            android:id="@+id/nr0"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="0"
            android:textSize="30dp"
            android:onClick="pressZero"
            />

        <Button
            android:id="@+id/dot"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="."
            android:textSize="30dp"
            android:onClick="pressDot"/>

        <Button
            android:id="@+id/equal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="="
            android:textSize="30dp"
            android:onClick="pressEquals"/>

    </LinearLayout>

</LinearLayout>

和代码:

String operation;
CharSequence num1;
CharSequence num2;
TextView t;
String n1;
String n2;
String doot;
double number1;
double number2;
double output;
String out;
CharSequence temp;
CharSequence dooot;
int x;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    operation = "";
    num2 = "";
    n1 = "";
    n2 = "";
    doot = "";
    number1 = 1;
    number2 = 0;
    output = 0;
    out = "";
    temp = "";
    x=100;
    dooot=".";
}

public void firstNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.setText("1");
    } else t.append("1");
}

public void secondNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {

        t.setText("2");
    } else t.append("2");
}

public void thirdNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.clearComposingText();
        t.setText("3");
    } else t.append("3");
}

public void fourthNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.clearComposingText();
        t.setText("4");
    } else t.append("4");
}

public void fifthNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.clearComposingText();
        t.setText("5");
    } else t.append("5");
}

public void sixthNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.clearComposingText();
    num1 = "";
        t.setText("6");
    } else t.append("6");
}

public void seventhNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.clearComposingText();
        t.setText("7");
    } else t.append("7");
}

public void eightNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.clearComposingText();
        t.setText("8");
    } else t.append("8");
}

public void ninthNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (temp.equals("0")) {
        t.clearComposingText();
        t.setText("9");
    } else t.append("9");
}

public void pressZero(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    if (!temp.equals("0")) {
        t.append("0");
    }
}

public void goBack(View view){
    TextView t = (TextView)findViewById(R.id.textView);
    temp = t.getText();
    if(!temp.equals("0")) {
        if(n1.length()==1)
        {
            number1=0;
            t.setText("");
            out = String.valueOf(number1);
            t.append(out);
        }
        else {
            num1 = t.getText();
            n1 = num1.toString();
            n1 = n1.substring(0, n1.length() - 1);
            number1 = Integer.parseInt(n1);
            t.setText("");
            out = String.valueOf(number1);
            t.append(out);
        }
    }
}

public void pressPlus(View view)
{
    TextView t = (TextView) findViewById(R.id.textView);
    num1=t.getText();
    t.setText("0");
    operation="+";
}

public void pressMinus(View view)
{
    TextView t = (TextView) findViewById(R.id.textView);
    num1=t.getText();
    t.setText("0");
    operation="-";
}

public void pressMultiplication(View view)
{
    TextView t = (TextView) findViewById(R.id.textView);
    num1=t.getText();
    t.setText("0");
    operation="*";
}

public void pressDivision(View view)
{
    TextView t = (TextView) findViewById(R.id.textView);
    num1=t.getText();
    t.setText("0");
    operation="/";
}

public void pressProcent(View view)
{
    TextView t = (TextView) findViewById(R.id.textView);
    num1=t.getText();
    n1 = num1.toString();
    number1 = Integer.parseInt(n1);
    output = number1 / x;
    t.setText("");
    out= String.valueOf(output);
    out = out.indexOf(".")< 0 ? out : out.replaceAll("0*$", "").replaceAll("\\.$", "");
    t.append(out);
}

public void pressDot(View view)
{
    TextView t = (TextView) findViewById(R.id.textView);
    doot=t.toString();

    if (doot.contains(dooot))t.append(".");
    else t.append("");

}

public void nrClear(View view)
{
    TextView t = (TextView)findViewById(R.id.textView);
    t.setText("0");
}

public void pressEquals(View view)
{
    if(operation.equals("+"))
    {
        TextView t = (TextView)findViewById(R.id.textView);
        num2 = t.getText();
        n1 = num1.toString();
        n2 = num2.toString();
        number1 = Integer.parseInt(n1);
        number2 = Integer.parseInt(n2);
        output = number1 + number2;
    }

    if(operation.equals("-"))
    {
        TextView t = (TextView)findViewById(R.id.textView);
        num2 = t.getText();
        n1 = num1.toString();
        n2 = num2.toString();
        number1 = Integer.parseInt(n1);
        number2 = Integer.parseInt(n2);
        output = number1 - number2;
    }

    if(operation.equals("*"))
    {
        TextView t = (TextView)findViewById(R.id.textView);
        num2 = t.getText();
        n1 = num1.toString();
        n2 = num2.toString();
        number1 = Integer.parseInt(n1);
        number2 = Integer.parseInt(n2);
        output = number1 * number2;
    }

    if(operation.equals("/"))
    {
        TextView t = (TextView)findViewById(R.id.textView);
        num2 = t.getText();
        n1 = num1.toString();
        n2 = num2.toString();
        number1 = Integer.parseInt(n1);
        number2 = Integer.parseInt(n2);
        output = number1 / number2;
    }


    TextView t = (TextView)findViewById(R.id.textView);
    t.setText("");
    out= String.valueOf(output);
    out = out.indexOf(".")< 0 ? out : out.replaceAll("0*$", "").replaceAll("\\.$", "");
    t.append(out);
}

}

感谢您的任何提示。

标签: javaandroidcalculator

解决方案


对于您的第一个问题,请执行以下操作:

public void eightNumber(View view) {
    TextView t = (TextView) findViewById(R.id.textView);
    temp = t.getText();
    temp = temp + "8"

    //convert the input number to double to avoid extra zero before digits (05)
    double tempNumber = Double.parseDouble(temp);
    //convert our double number back to string, to use it in textView.setText method
    temp = String.valueOf(tempNumber);

    //to avoid redundant ".0" after integer numbers:
    //if 2 last digits of the number is .0, we delete it
    if(temp.lastIndexOf(".0") == temp.length() - 2){
        temp = temp.substring(0, temp.length() - 2);
    }

    t.setText(temp);
}

以此类推所有 9 位数字。但是你的第二个问题,你只需要反转 if 条件:

public void pressDot(View view)
{
    TextView t = (TextView) findViewById(R.id.textView);
    doot=t.toString();

    if (!doot.contains(dooot)) {
        t.append(".");
    }

}

并且不需要 else 块。如果您需要更多帮助,请告诉我。


推荐阅读