【问题标题】:Percentage Calculator isn't Working in Android Studio百分比计算器在 Android Studio 中不起作用
【发布时间】:2019-05-23 04:07:37
【问题描述】:

我的应用程序包含 3 个 EditText 属性

第一个属性从用户那里获取百分比值,第二个属性应该获取用户想要计算百分比的数字,第三个属性应该显示结果。

我附上了下面的java和xml代码!谢谢。

我有一个逻辑错误。我期望获得具有用户输入的特定百分比的特定数字的值。

例如。用户想要 200 的 25%,然后在第一个 EditText 中,用户将输入 25%,然后在第二个 Edittext 中他将输入 200,该值应自动显示在第三个 Edittext 中,即 50。但是这里第三个 Edittext 没有更新它值并显示 0.0。

    <EditText
    android:id="@+id/txtbx1"
    android:layout_width="179dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.462" />

    <EditText
    android:id="@+id/txtbx2"
    android:layout_width="179dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.585" />

    <EditText
    android:id="@+id/txtbx3"
    android:layout_width="179dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.708" />

    <TextView
    android:id="@+id/txtvw1"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="%"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.144"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.462" />

    <TextView
    android:id="@+id/txtvw3"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="is"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.144"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.708" />

    <TextView
    android:id="@+id/txtvw2"
    android:layout_width="33dp"
    android:layout_height="45dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="of"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.133"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.585" />

    <TextView
    android:id="@+id/PC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:fontFamily="@font/sansationbold"
    android:text="Percentage Calculator"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.139" />

    <TextView
    android:id="@+id/acl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="320dp"
    android:layout_marginLeft="320dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:fontFamily="@font/casanova"
    android:text="Arcis Computer Labs"
    android:textSize="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.988" />

    setContentView(R.layout.activity_mainscreen);
    EditText txtbx1 = (EditText)findViewById(R.id.txtbx1);
    EditText txtbx2 = (EditText)findViewById(R.id.txtbx2);
    EditText txtbx3 = (EditText)findViewById(R.id.txtbx3);
    TextView txtvw1 = (TextView)findViewById(R.id.txtvw1);
    TextView txtvw2 = (TextView)findViewById(R.id.txtvw2);
    TextView txtvw3 = (TextView)findViewById(R.id.txtvw3);

    float txt;
    try{
        txt = Float.parseFloat(txtbx1.getText().toString());
    }
    catch(NumberFormatException x){
        txt = (float) 0.0;
    }
    float txt2;
    try{
        txt2 = Float.parseFloat(txtbx2.getText().toString());
    }
    catch(NumberFormatException ex){
        txt2 = (float) 0.0;
    }
    Percent = (txt2 * txt) / 100;
    txtbx3.setText(String.valueOf(Percent));
}

【问题讨论】:

  • 您遇到了什么错误?你期望得到什么?你在放什么?
  • 我没有收到任何错误应用程序正在运行没有任何错误。我期望以用户输入的特定百分比获得特定数字的值。例如。用户想要 200 的 25%,然后在第一个 Edittext 中用户将输入 25%,然后在第二个 Edittext 中他将输入 200,该值应自动显示在第三个 Edittext 中,即 50。但是这里第三个 Edittext 没有更新它的值并显示 0.0。
  • edit问题包含额外信息
  • 您需要在 EditText 上添加更改监听器。检查这个答案:stackoverflow.com/questions/20824634/…
  • 添加更改监听器会做什么?

标签: java android textview android-edittext


【解决方案1】:

在您的代码中,EditText 的文本不会更新,因为只有在应用程序启动时才会调用 onCreate 方法。所以需要在EditText 上添加更改监听器。试试下面的代码:

float txt1 = 0, txt2 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText txtbx1 = (EditText) findViewById(R.id.txtbx1);
    EditText txtbx2 = (EditText) findViewById(R.id.txtbx2);
    final EditText txtbx3 = (EditText) findViewById(R.id.txtbx3);
    TextView txtvw1 = (TextView) findViewById(R.id.txtvw1);
    TextView txtvw2 = (TextView) findViewById(R.id.txtvw2);
    TextView txtvw3 = (TextView) findViewById(R.id.txtvw3);


    txtbx1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0)
                txt1 = Float.parseFloat(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
            changeValue(txt1, txt2, txtbx3);
        }
    });

    txtbx2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0)
                txt2 = Float.parseFloat(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
            changeValue(txt1, txt2, txtbx3);
        }
    });


}

private void changeValue(float txt1, float txt2, EditText txtbx3) {
    float Percent = (txt2 * txt1) / 100;
    txtbx3.setText(String.valueOf(Percent));
}

【讨论】:

  • 感谢您的回答。它就像魅力一样,而且我学得很好。
【解决方案2】:

如果Percent 是您定义的浮点数,则应通过以下方式对其进行修复:

Percent = (txt2 * txt) / 100f;

【讨论】:

  • 你能在问题中解释更多吗
【解决方案3】:

首先,您需要确保第一个和第二个 EditText 已填充,然后更改第三个 EditText 的值。
https://developer.android.com/reference/android/text/TextWatcher
为此,您必须向第一个和第二个 EditText 添加一个 TextWatcher;我们称它们为 edtPercentage、edtOriginalValue 和 edtFinalValue:

edtPercentage.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    //Here you can define code to happen before the change
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after{
    //Here you can define code to happen on change
    }

    @Override
    public void afterTextChanged(Editable s) {
    //Here you can define code to happen after the change

    if(edtPercentage.getText().length()>0 && edtOriginalValue.getText().length()>0)
    //edtFinalValue.setText([metod to do the calculation and parse to string]);
    }
});


对 EditText edtOriginalValue 执行相同的操作,因此当您更改百分比或值以获取百分比时,最终的 EditText 将更新

【讨论】:

  • @karanugale 然后请接受答案,以便其他人可以更轻松地找到解决方案
【解决方案4】:

假设百分比被定义为浮点数,您应该将第二条到最后一条指令更改为:

Percent = (txt2 * txt) / 100.00;

如果您在包含浮点数的方程式中使用整数字面数字,例如 100,则结果可能会转换为整数,并且可能会发生舍入或截断。

我认为您的问题是由于用户在文本框 1 中输入“25%”这一事实引起的。当您尝试将其解析为浮点数时,您会收到格式错误,因为“%”不是数值.在尝试将该值解析为浮点数之前,您需要以某种方式将 25% 转换为 .25。否则,它将产生错误并将第一个值替换为 0.0,这将导致百分比计算结果为 0.0

float txt;
try{
    txt = Float.parseFloat(txtbx1.getText().toString()); // this will generate an error on a value of '25%'
}
catch(NumberFormatException x){
    txt = (float) 0.0; // so, this will execute.
}

您可以通过在异常中的该行的代码中放置一个断点来验证这一点,并证明实际上异常发生在这里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2021-03-27
    • 2023-02-23
    • 2019-09-20
    • 2011-06-01
    • 2021-01-10
    相关资源
    最近更新 更多