当前位置: 首页 > 知识库问答 >
问题:

Android中edittext中添加decimal而不是number时应用程序崩溃

冯茂实
2023-03-14
public class MainActivity extends ActionBarActivity {

    EditText editText1;
    EditText editText2;
    EditText editText4;
    EditText editText3;
    EditText editText5;
    EditText editText6;
    EditText editText7;
    EditText editText8;
    TextView textViewtotal;
    TextView textViewnet;
    TextView textViewproceeds;
    TextView textViewprof;

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

        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);
        editText6 = (EditText) findViewById(R.id.editText6);
        editText7 = (EditText) findViewById(R.id.editText7);
        editText8 = (EditText) findViewById(R.id.editText8);
        textViewtotal = (TextView) findViewById(R.id.editText6);
        textViewnet = (TextView) findViewById(R.id.editText8);
        textViewproceeds = (TextView) findViewById(R.id.editText7);
        textViewprof = (TextView) findViewById(R.id.editText9);

        editText1.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewproceeds.setText(addProceeds());
                textViewtotal.setText(addTotal());

            }
        });
        editText2.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewtotal.setText(addTotal());
            }
        });
        editText3.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewproceeds.setText(addProceeds());

            }
        });
        editText4.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewtotal.setText(addTotal());
            }
        });
        editText5.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewtotal.setText(addTotal());
            }
        });
        editText6.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewnet.setText(addNet());
                textViewprof.setText(addProfpercent());
            }
        });
        editText7.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewnet.setText(addNet());
            }
        });
        editText8.addTextChangedListener(new TextWatcher() {

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {


            }

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                textViewprof.setText(addProfpercent());
            }
        });
    }

    private String addProceeds() {
        Double num1;
        Double num2;

        if(editText1.getText().toString() != "" && editText1.getText().length() > 0) {
            num1 = Double.parseDouble(editText1.getText().toString());
        } else {
            num1 = Double.valueOf(0);
        }
        if(editText3.getText().toString() != "" && editText3.getText().length() > 0) {
            num2 = Double.parseDouble(editText3.getText().toString());
        } else {
            num2 = Double.valueOf(0);
        }

        return Double.toString(num1 * num2);
    }

    private String addTotal() {
        Double num1;
        Double num2;
        Double num3;
        Double num4;

        if (editText1.getText().toString() != "" && editText1.getText().length() > 0) {
            num1 = Double.parseDouble(editText1.getText().toString());
        } else {
            num1 = Double.valueOf(0);
        }
        if (editText2.getText().toString() != "" && editText2.getText().length() > 0) {
            num2 = Double.parseDouble(editText2.getText().toString());
        } else {
            num2 = Double.valueOf(0);
        }
        if (editText4.getText().toString() != "" && editText4.getText().length() > 0) {
            num3 = Double.parseDouble(editText4.getText().toString());
        } else {
            num3 = Double.valueOf(0);
        }
        if (editText5.getText().toString() != "" && editText5.getText().length() > 0) {
            num4 = Double.parseDouble(editText5.getText().toString());
        } else {
            num4 = Double.valueOf(0);
        }

        return Double.toString((num1 * num2) + num3 + num4);
    }

    private String addNet() {
        Double num1;
        Double num2;

        if(editText6.getText().toString() != "" && editText6.getText().length() > 0) {
            num1 = Double.parseDouble(editText6.getText().toString());
            } else {
            num1 = Double.valueOf(0);
            }
        if(editText7.getText().toString() != "" && editText7.getText().length() > 0) {
            num2 = Double.parseDouble(editText7.getText().toString());
            } else {
            num2 = Double.valueOf(0);
            }

        return Double.toString(num2 - num1);
    }



    private String addProfpercent() {
        Double num1;
        Double num2;

        if(editText6.getText().toString() != "" && editText6.getText().length() > 0) {
            num1 = Double.parseDouble(editText6.getText().toString());
        } else {
            num1 = Double.valueOf(0);
        }
        if(editText8.getText().toString() != "" && editText8.getText().length() > 0) {
            num2 = Double.parseDouble(editText8.getText().toString());
        } else {
            num2 = Double.valueOf(0);
        }
        return Double.toString((num2 / num1) * 100);
    }
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Number of Shares"
        android:id="@+id/textNumShares"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="50dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Initial Share Price"
        android:id="@+id/textInitialPrice"
        android:layout_below="@+id/textNumShares"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/editText1"
        android:layout_alignBottom="@+id/textNumShares"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="false"
        android:maxLength="8"
        android:gravity="right"
        android:width="100dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_alignBottom="@+id/textInitialPrice"
        android:layout_alignRight="@+id/editText1"
        android:layout_alignEnd="@+id/editText1"
        android:maxLength="8"
        android:gravity="right"
        android:width="100dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Target/Current Share Price"
        android:id="@+id/textTargetPrice"
        android:layout_below="@+id/textInitialPrice"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText3"
        android:layout_alignBottom="@+id/textTargetPrice"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:maxLength="8"
        android:gravity="right"
        android:width="100dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Brokerage Buy Fee(s)"
        android:id="@+id/textBuyFee"
        android:layout_below="@+id/textTargetPrice"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText4"
        android:layout_alignBottom="@+id/textBuyFee"
        android:layout_alignRight="@+id/editText3"
        android:layout_alignEnd="@+id/editText3"
        android:maxLength="5"
        android:gravity="right"
        android:width="100dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Brokerage Sell Fee(s)"
        android:id="@+id/textSellFee"
        android:layout_below="@+id/textBuyFee"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText5"
        android:layout_alignBottom="@+id/textSellFee"
        android:layout_alignRight="@+id/editText4"
        android:layout_alignEnd="@+id/editText4"
        android:maxLength="5"
        android:gravity="right"
        android:width="100dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Total Purchase Cost"
        android:id="@+id/textTotalCost"
        android:layout_below="@+id/textSellFee"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText6"
        android:layout_alignBottom="@+id/textTotalCost"
        android:layout_alignLeft="@+id/editText5"
        android:layout_alignStart="@+id/editText5"
        android:maxLength="8"
        android:gravity="right"
        android:width="100dp"
        android:editable="false"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:longClickable="false"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Proceeds"
        android:id="@+id/textProceeds"
        android:layout_below="@+id/textTotalCost"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText7"
        android:layout_alignBottom="@+id/textProceeds"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:maxLength="8"
        android:gravity="right"
        android:width="100dp"
        android:editable="false"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:longClickable="false"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Net Profit"
        android:id="@+id/textNetProfit"
        android:layout_below="@+id/textProceeds"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText8"
        android:layout_alignBottom="@+id/textNetProfit"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:maxLength="8"
        android:gravity="right"
        android:width="100dp"
        android:editable="false"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:longClickable="false"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Return on Investment"
        android:id="@+id/textReturn"
        android:layout_below="@+id/textNetProfit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editText9"
        android:layout_alignBottom="@+id/textReturn"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:maxLength="2"
        android:gravity="right"
        android:width="100dp"
        android:editable="false"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:longClickable="false"/>

    <EditText
        android:layout_width="wrap_content"
        android:inputType="numberDecimal"
        android:layout_height="wrap_content"
        android:id="@+id/editTest1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="44dp"
        android:width="100dp"/>

</RelativeLayout>

共有1个答案

田宇
2023-03-14

试试这个,

必须插入带有“0”的小数点,而不仅仅是“。”也可以通过添加“0”以编程方式解决此问题。按照以下步骤更新代码,

double num2 = Double.parseDouble("0" + editText2.getText().toString());

对所有接受小数的编辑文本执行相同的操作。

 类似资料:
  • 当我尝试在Android Studio3中添加图像时,应用程序就会崩溃。请帮帮忙。 logcat: activity_main.xml 主要活动.java 当我删除ImageView时,应用程序就可以正常工作了。我已经尝试了一切,调整图像大小,重新安装android Studio。 有人能把问题弄清楚吗?

  • 我正在创建一个呼叫阻止应用程序,当我添加复选框或单选按钮到我的布局,应用程序开始崩溃。 活动代码。 } 广播接收机代码 公共void onReceive(上下文、意图){ XML文件如下所示 错误日志如下

  • 当我尝试更新应用程序时,应用程序崩溃了。在我们的应用程序中,我们正在缓存中保存更新的版本,然后尝试从缓存中安装应用程序。应用程序由于android.os.fileuriexposedexception而崩溃。我发现通过intent.getData()在应用程序之外公开了日志android.os.fileuriexposedexception:file:///storage/emulated/0/t

  • 我有一个问题,每当我尝试添加任何监听器到我的切换按钮,应用程序甚至没有启动,只是崩溃。在这个代码示例中,我使用SetOnCheckedChangeListener进行了尝试。 当我在没有任何监听器的情况下设置这个值时,它已经崩溃了: mainactivity.java 正如您所看到的,我试图一如既往地将setOnCheckedChangeListener添加到tglButton中。但我不明白为什么

  • 在以前的代码上,谁医生帮了我 现在,android应用程序在我的手机上运行时崩溃了,这是错误日志 第一个问题是在我用相机扫描二维码后,它不能显示在二维码的结果进入 第二个问题是,我从存储器中选择了一个QRcode图像,然后点击确认,它崩溃了 下面是我认为的问题 类型不匹配:推断的类型是Uri?但乌里是意料之中的 冗余SAM构造函数 'onRequestPermissionsResult(Int,数