2012-12-06 97 views
0

我在獲取我的程序的editText中的值時遇到問題。我在textView中設置了值,以便我可以看到代碼是否可以獲取它。但不幸的是它顯示是這樣的:獲取EditText中的值Android

[email protected] - > @符號後的數字變化在模擬器

爲什麼發生這種情況每次運行?這是我的代碼:

package com.example.ITax; 

import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

/** 
* Created with IntelliJ IDEA. 
* User: Karla Mae Jaro 
* Date: 12/3/12 
* Time: 3:58 PM 
* To change this template use File | Settings | File Templates. 
*/ 
public class AnnualComputation extends MyActivity 
{ 
String civil_status2; 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.annual_computation); 

    Bundle extras = getIntent().getExtras(); 

    if(extras != null) 
    { 
     civil_status2 = extras.getString("user_status"); 
    } 

    final Button btn_compute = (Button) findViewById(R.id.btn_compute_from_annual); 
    final Button btn_back = (Button) findViewById(R.id.btn_back_from_annual_computation); 

    btn_back.setOnClickListener(new Button.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Intent i = new Intent(getApplicationContext(), OpenChoices.class); 
      startActivity(i); 
     } 
    }); 

    btn_compute.setOnClickListener(new Button.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      final EditText net_salary = (EditText) findViewById(R.id.et_net_annual); 
      final EditText tax_due = (EditText) findViewById(R.id.et_taxdue); 
      final EditText tax_exe = (EditText) findViewById(R.id.et_taxexemption); 

      final TextView txt3 = (TextView) findViewById(R.id.textView3); 
      final TextView txt4 = (TextView) findViewById(R.id.textView4); 
      final TextView txt5 = (TextView) findViewById(R.id.textView5); 
      final TextView txt6 = (TextView) findViewById(R.id.textView6); 
      final TextView txt7 = (TextView) findViewById(R.id.textView7); 
      final TextView txt8 = (TextView) findViewById(R.id.textView8); 


      double netSalary, taxDue, rate = 0, exemption = 0, additional = 0, lowerlimit = 0, total; 
      String ns, td, r, e, a, t; 

      ns = net_salary.getText().toString(); 
      netSalary = Double.parseDouble(ns); 

      /* Getting the tax exemption */ 

      if ("SME".equals(civil_status2)) 
      { 
       exemption = 50000; 
      } 

      else if ("SM1".equals(civil_status2)) 
      { 
       exemption = 25000; 
      } 

      else if ("SM2".equals(civil_status2)) 
      { 
       exemption = 50000; 
      } 

      else if ("SM3".equals(civil_status2)) 
      { 
       exemption = 75000; 
      } 

      else if ("SM4".equals(civil_status2)) 
      { 
       exemption = 100000; 
      } 

      /* Getting the rate, additional, lowerlimit */ 

      if(netSalary <= 10000) 
      { 
       rate = 0.05; 
      } 

      else if((netSalary > 10000) && (netSalary <=30000)) 
      { 
       rate = 0.1; 
       additional = 5000; 
       lowerlimit = 10000; 
      } 

      else if ((netSalary > 30000) && (netSalary <= 70000)) 
      { 
       rate = 0.15; 
       additional = 2500; 
       lowerlimit = 30000; 
      } 

      else if((netSalary > 70000) && (netSalary <= 14000)) 
      { 
       rate = 0.20; 
       additional = 8500; 
       lowerlimit = 70000; 
      } 

      else if ((netSalary > 140000) && (netSalary <= 250000)) 
      { 
       rate = 0.25; 
       additional = 22500; 
       lowerlimit = 140000; 
      } 

      else if((netSalary > 250000) && (netSalary <= 500000)) 
      { 
       rate = 0.30; 
       additional = 50000; 
       lowerlimit = 250000; 
      } 

      else if (netSalary > 500000) 
      { 
       rate = 0.32; 
       additional = 125000; 
       lowerlimit = 500000; 
      } 

      taxDue = netSalary - exemption; 
      total = taxDue - lowerlimit; 
      total = total * rate; 
      total = total + additional; 

      /* Converting exemption from Double to String */ 


      td = String.valueOf(net_salary); 
      e = String.valueOf(exemption); 
      a = String.valueOf(additional); 
      r = String.valueOf(rate); 
      t = String.valueOf(total); 

      /* Placing the value to the editText (textbox) */ 

      tax_due.setText(td); 
      tax_exe.setText(e); 
      txt3.setText(civil_status2); 
      txt4.setText(td); 
      txt5.setText(e); 
      txt6.setText(t); 
      txt7.setText(r); 
      txt8.setText(a); 

     } 
    }); 
} 
} 

回答

6

使用netSalary代替net_salary

td = String.valueOf(netSalary); 

當你正在過編輯文本,而不是雙可變

+0

+ 1vote yes這是正確的答案!!!) –

+0

是的!我只是忽略了它。「<哈哈!非常感謝! :D 但我有另一個問題@Nunu,爲什麼我的變量,速率,附加和下限總是等於0.0,我的if-else語句有什麼問題嗎? –

+0

我可以看到的唯一的事情是在(否則如果((netSalary> 70000)&&(netSalary <= 14000)))你錯過了140000 – Nermeen

1

您在td所以td傳遞的EditTextnet_salary)的對象將有你的net_salary對象的字符串表示。

已存儲在netSalary varibale你的價值,從而傳遞代替:

td = String.valueOf(netSalary); 

併爲您的信息:

[email protected]是你EditText對象的字符串表示(即調用你EdittexttoString方法對象將返回此字符串)。

@之後的值是您的對象的內存地址。每次它都會發生變化,因爲每次在不同的內存位置創建對象時。的

+0

是的!我只是忽略了它。「<哈哈!非常感謝! :D哇!感謝您的信息,非常有幫助! 但我有另一個問題@Abu,爲什麼我的變量,速率,附加和下限總是等於0.0,我的if-else語句有什麼問題嗎? –

1

試試這個,

tax_due.setText(net_salary.getText().toString()); 
+0

0!我只是忽略了它。「<哈哈!非常感謝! :D 但是我有另外一個問題@Andro Selva,爲什麼我的變量,速率,附加和下限總是等於0.0,我的if-else語句有什麼問題嗎? –

1

如果問題是在這裏:

td = String.valueOf(net_salary); 

然後,這是因爲你正在試圖對待net_salary,這是一個在其中設置了字符串值的小部件,就像它只是一個字符串一樣。試試:

td = String.valueOf(net_salary.getText().toString()); 
+0

是的!我只是忽略了它。「<哈哈!非常感謝!:D 但我有另一個問題@goldilocks,爲什麼我的變量,速率,附加和下限總是等於0.0,我的if-else語句有什麼問題嗎? –