gpt4 book ai didi

java - 如何在一个TextView中添加各种editText

转载 作者:行者123 更新时间:2023-11-29 23:14:24 26 4
gpt4 key购买 nike

我是编程新手,我需要你的帮助,在 editText txt50 中插入数字时出现错误,应用程序崩溃请帮助我,我不知道错误是什么:

代码:

public class Main2Activity extends AppCompatActivity {

private EditText cinco, cien, doscientos, quinientos, mil, dosmil, cincomil, diezmil, veintemil, cincuentamil, cienmil;
private TextView diezmob;

public static final String nombres = "names";
TextView txtBienvenido;

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

txtBienvenido = (TextView) findViewById(R.id.txtbienvenido);
String usuario = getIntent().getStringExtra("names");
txtBienvenido.setText("¡Bienvenido(a) Hermano(a) " + usuario + "!");

diezmob = (TextView) findViewById(R.id.txtdiezmob);
cinco = (EditText) findViewById(R.id.txt50);
cien = (EditText) findViewById(R.id.txt100);
doscientos = (EditText) findViewById(R.id.txt200);
quinientos = (EditText) findViewById(R.id.txt500);
mil = (EditText) findViewById(R.id.txt1000);
dosmil = (EditText) findViewById(R.id.txt2000);
cincomil = (EditText) findViewById(R.id.txt5000);
diezmil = (EditText) findViewById(R.id.txt10000);
veintemil = (EditText) findViewById(R.id.txt20000);
cincuentamil = (EditText) findViewById(R.id.txt50000);
cienmil = (EditText) findViewById(R.id.txt100000);

cinco.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 ((cinco.toString().equals("")) && (!cinco.toString().equals(null)) && (cinco.toString().isEmpty() || (cinco.toString().length() >= 0))) {
double valor1 = Double.parseDouble((cinco.getText().toString()));
double valor2 = Double.parseDouble((cien.getText().toString()));
double valor3 = Double.parseDouble((doscientos.getText().toString()));
double valor4 = Double.parseDouble((quinientos.getText().toString()));
double valor5 = Double.parseDouble((mil.getText().toString()));
double valor6 = Double.parseDouble((dosmil.getText().toString()));
double valor7 = Double.parseDouble((cincomil.getText().toString()));
double valor8 = Double.parseDouble((diezmil.getText().toString()));
double valor9 = Double.parseDouble((veintemil.getText().toString()));
double valor10 = Double.parseDouble((cincuentamil.getText().toString()));
double valor11 = Double.parseDouble((cienmil.getText().toString()));
double suma = (valor1 * 50) + (valor2 * 100) + (valor3 * 200) + (valor4 * 500) + (valor5 * 1000) + (valor6 * 2000) + (valor7 * 5000) + (valor8 * 10000) + (valor9 * 20000) + (valor10 * 50000) + (valor11 * 100000);
String resultado = String.valueOf((int) suma);
diezmob.setText(String.valueOf(resultado));
} else {
diezmob.setText("0");
}
}

@Override
public void afterTextChanged(Editable s) {
}
});
} }

LogCat:在 editText txt50 中插入数字时出错,应用程序崩溃:

java.lang.NumberFormatException: Invalid double: ""

最佳答案

我用这段代码解决了这个错误,任何错误请说出来。

package com.example.josue.login;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Calendar;


public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

private TextView diezmob;

public static final String nombres = "names";
TextView txtBienvenido;

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

txtBienvenido = (TextView) findViewById(R.id.txtbienvenido);
String usuario = getIntent().getStringExtra("names");
txtBienvenido.setText("¡Bienvenido(a) Hermano(a) " + usuario + "!");

diezmob = (TextView) findViewById(R.id.txtdiezmob);

findViewById(R.id.btncalcular).setOnClickListener(this);
findViewById(R.id.btncalcular5).setOnClickListener(this);
findViewById(R.id.btncalcular10).setOnClickListener(this);
findViewById(R.id.btncalcular15).setOnClickListener(this);
}

@Override
public void onClick(View v) {

switch (v.getId()) {

case R.id.btncalcular:

double cinco, cien, doscientos, quinientos, mil, dosmil, cincomil, diezmil, veintemil, cincuentamil, cienmil;

String Cinco = ((EditText) findViewById(R.id.txt50)).getText().toString();
String Cien = ((EditText) findViewById(R.id.txt100)).getText().toString();
String Doscientos = ((EditText) findViewById(R.id.txt200)).getText().toString();
String Quinientos = ((EditText) findViewById(R.id.txt500)).getText().toString();
String Mil = ((EditText) findViewById(R.id.txt1000)).getText().toString();
String Dosmil = ((EditText) findViewById(R.id.txt2000)).getText().toString();
String Cincomil = ((EditText) findViewById(R.id.txt5000)).getText().toString();
String Diezmil = ((EditText) findViewById(R.id.txt10000)).getText().toString();
String Veintemil = ((EditText) findViewById(R.id.txt20000)).getText().toString();
String Cincuentamil = ((EditText) findViewById(R.id.txt50000)).getText().toString();
String Cienmil = ((EditText) findViewById(R.id.txt100000)).getText().toString();

if (Cinco != null && !Cinco.equals("")) {
cinco = Double.valueOf(Cinco);

}else{
cinco = 0;

}

if (Cien != null && !Cien.equals("")){
cien = Double.valueOf(Cien);

}else{
cien=0;

}

if (Doscientos != null && !Doscientos.equals("")) {
doscientos = Double.valueOf(Doscientos);

}else{
doscientos=0;

}

if (Quinientos != null && !Quinientos.equals("")) {
quinientos = Double.valueOf(Quinientos);

}else{
quinientos = 0;

}
if (Mil != null && !Mil.equals("")){
mil = Double.valueOf(Mil);

}else{
mil = 0;

}
if (Dosmil != null && !Dosmil.equals("")) {
dosmil = Double.valueOf(Dosmil);

}else {
dosmil = 0;

}
if (Cincomil != null && !Cincomil.equals("")) {
cincomil = Double.parseDouble(Cincomil);

}else {
cincomil = 0;

}
if (Diezmil !=null && !Diezmil.equals("")) {
diezmil = Double.valueOf(Diezmil);

}else {
diezmil = 0;

}

if (Veintemil != null && !Veintemil.equals("")) {
veintemil = Double.valueOf(Veintemil);

}else {
veintemil = 0;

}

if (Cincuentamil != null && !Cincuentamil.equals("") ) {
cincuentamil = Double.valueOf(Cincuentamil);

}else {
cincuentamil = 0;

}

if (Cienmil != null && !Cienmil.equals("") ) {
cienmil = Double.valueOf(Cienmil);

}else {
cienmil = 0;

}



double suma = (cinco * 50) + (cien * 100) + (doscientos * 200) + (quinientos * 500) + (mil * 1000) +
(dosmil * 2000) + (cincomil * 5000) + (diezmil * 10000) + (veintemil * 20000) + (cincuentamil * 50000) +
(cienmil * 100000);
String resultado = String.valueOf((int)(suma));
diezmob.setText(String.valueOf(resultado));

break;

case R.id.btncalcular5:

Intent i = new Intent(this, Main5Activity.class);
i.putExtra("dato",diezmob.getText().toString());
startActivity(i);

break;

case R.id.btncalcular10:
Intent ii = new Intent(this, Main5Activity.class);
startActivity(ii);
break;

case
R.id.btncalcular15:
Intent iii = new Intent(this, Main5Activity.class);
startActivity(iii);
break;

default:
break;
}
}
}

关于java - 如何在一个TextView中添加各种editText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com