【问题标题】:Android - When I entered in EditText then value is not calculate in next EditText?Android - 当我输入 EditText 时,值不在下一个 EditText 中计算?
【发布时间】:2014-09-01 17:01:45
【问题描述】:

amntpaid EditText 值应该添加到 EditText 中。但是当我运行下面的代码时,添加没有发生。 检查我的代码。

String subtotal=String.valueOf(amntpaid);
String total =String.valueOf(totamnt);
total = total+subtotal;
String ncr=total+"";
totalamt.setText(ncr);

【问题讨论】:

  • @GiruBhai 他无法添加两个 EditText 值。
  • 没有整数,怎么计算?将字符串转换为整数,然后进行加法。现在你只是连接字符串。
  • 是的,正确的@Aniruddha 只有它的连接......我将如何添加?
  • 您必须发布完整代码才能理解问题。
  • @17Coder 你知道Java的基础吗?或者更确切地说是任何编程语言?正如阿尼鲁达所提到的。请将您的完整代码和更好的描述发布到您真正想问的内容。

标签: android string android-edittext


【解决方案1】:

您正在将一个字符串添加到另一个字符串,您需要将字符串数据类型转换为 Integer,然后只有它会添加

String subtotal = String.valueOf(amntpaid);
String total = String.valueOf(totamnt);
int sum = Integer.parseInt(total) + Integer.parseInt(subtotal);
totalamt.setText(sum + "");

【讨论】:

  • @Aniruddha 请证明你的说法,我想知道原因让自己清楚
  • @nobalG 这个total = Integet.parseInt(total) + Integet.parseInt(subtotal); 对吗?他正在将字符串分配给 int (这是不可能的)。他应该先完成int grandTotal = Integet.parseInt(total) + Integet.parseInt(subtotal);,然后再完成total = grandTotal + "";
  • total = Integet.parseInt(total) + Integet.parseInt(subtotal); 行不正确。试试int totalAsInt = Integet.parseInt(total) + Integet.parseInt(subtotal);
  • @Aniruddha 我想知道这个“将 int 分配给字符串变量?不好。”.....为什么?
  • @henry000 :我们都犯了严重的错误,从答案中复制粘贴 total = Integet.parseInt(total) + Integet.parseInt(subtotal);。应该是 Integer.parseInt() 他写成 Integet.parseInt()
【解决方案2】:

您正在尝试添加 2 个字符串,首先将其转换为 int

String amount = amntpaid.getText().toString();
String total = totamnt.getText().toString();
int totalInt = Integer.parseInt(amount) + Integer.parseInt(total);
totalamt.setText("" + totalInt);

【讨论】:

    【解决方案3】:

    做这样的事情: 将amntpaid 和totamnt 相加并将其存储在某个int 变量中,例如var 喜欢

    var=Integer.parseInt(amntpaid.getText())+Integer.parseInt(totamnt.getText());
    String value_to_be_set=String.valueOf(var);
    totalamnt.setText(value_to_be_set);
    

    其中totalamnt 是您要在其中设置值的TextView,amntpaid 和totamnt 是您的EditText,您将从那里获取输入的输入值。

    【讨论】:

    • 我可以试试这个——'int subtotal = Integer.parseInt(amntpaid); int 总计 = Integer.parseInt(totamnt); int var=小计+总计;字符串 var1=String.valueOf(var); totalamt.setText(var1);'
    • 是的,但您只需要使用 amntpaid 和 totamnt 调用 getText(),例如:amntpaid.getText() 和 totamnt.getText()
    • 在我已经打电话之前... String amntpaid=PaymentPaid.getText().toString(); String totamnt=totalamt.getText().toString();
    • 那么没关系,你可以跳过那个getText()
    • int subtotal = Integer.parseInt(amntpaid.getText());显示错误 - String 类型的方法 getText() 未定义
    【解决方案4】:

    这只是一个示例,您必须进行相应的修改。

    TextView resultTV;
    EditText ed1, ed2;
    Button add;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.your_xml);
    
    resultTV = (TextView) findViewById (R.id.your_tv_id)  // do the same for both edittexts and button
    
    
     add.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
      String s1 = ed1.getText().toString();
      String s2 = ed2.getText().toString();
      if (!s1.trim().equals("") && !s2.trim().equals("")
      {
         int first = Integer.parseInt(s1);
         int second = Integer.parseInt(s2);
         int sum = first + second;
          resutTV.setText(sum + "");
      }
       else
       resultTV.setText("Please enter the values");
     }
    
        });
    

    【讨论】:

      【解决方案5】:

      你需要使用任何一个

      Integer.parseInt(stringValue);
      

      或

      Integer.valueOf(stringValue);
      

      对于那些编辑文本。

      试试下面的代码:

      int i = Integer.parseInt(et1.getText().toString().trim());
      
      int j = Integer.parseInt(et2.getText().toString().trim());
      
      int total = i + j;
      
      String sum = total + "";
      

      【讨论】:

      • 如果您的答案与答案之一相同,那么您应该投票而不是给出相同的答案。这就是 stackoverflow 的工作原理。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      相关资源
      最近更新 更多