使用2 textedit中的数据计算并在textview中显示结果

时间:2015-06-09 22:53:35

标签: android textview calculator textedit

我想从2个文本编辑中的数据中计算出一个值,并在textview中显示结果。我该怎么办?

BMI计算器:

private TextView deinbmi;
    private EditText weight;
    private EditText height;

    public void calculator ()
    {
        double ergebnis;
        weight=(EditText)findViewById(R.id.text_calculator_weight);
        height=(EditText)findViewById(R.id.text_calculator_height);
        double a=Double.parseDouble(weight.getText().toString());
        double b=Double.parseDouble(height.getText().toString());
        ergebnis=a*b;
        deinbmi.setText(""+ergebnis);
    }

我没有想法;我刚开始使用Java。我可以用C ++编程但只是控制台。

如果我在互联网上搜索此问题,每个人都有另一个版本来解决这个问题,但这些答案都不适合我吗?

2 个答案:

答案 0 :(得分:0)

试试这个工作代码。

你在创建内部缺少Textview,然后添加一个按钮以获得答案

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<!---header and side bar for user name and logout menu -starts here -->
<div class = "nav">
  <ul>
    <li><a href="home.php">Home</a></li>
    <li><a href="#">Portfolio</a>
      <ul>
        <li><a href="#">Active Directory</a>
        <li><a href="#">HelpDesk</a>
        <li><a href="#">CTS</a>
        <li><a href="#">Exchange/Infra</a>
        <li><a href="#">Others</a>
      </ul>
    </li>
    <li><a href="#">Downloads</a></li>
    <li><a href="#">Blog</a></li>
    <li><a href="#">News</a></li>
    <li><a href="#">Contact US</a></li>
  </ul>
</div>
<!--horizantal navigation bar ends here -->

<!---vetical navigation bar starts here-->
<div class="nav1">
  <label for="toggle2" id="kl">Active Directory</label>
  <ul class="animate" style="display:none" id="kll">
    <li class="animate"><a href="#" name="div1">Create Domain User</a></li>
    <li class="animate"><a href="#" name="div2">Domain Password Reset</a></li>
    <li class="animate"><a href="#" name="div3">Domain Joining</a></li>
  </ul>
</div>
<!---vetical navigation bar ends here-->

<div class="content">
  <div id="div1" style="display:none">
    <!---->
    <div id="AD-FORM">
      <h2>AD-FORM</h2>
      <form name="adform" action="/" onsubmit="return validateForm()" method="post">
        <label>Emp ID :</label>
        <input id="id" name="empid" placeholder="" type="text">
        <br>
        <br>

        <label>Full Name :</label>
        <input id="name" name="FName" placeholder="Enter your full name" type="text">
        <br>
        <br>

        <label>Designation:</label>
        <input id="name" name="desig" placeholder="Enter your Designation" type="text">
        <br>
        <br>
          
        <label for='DO'>D.O:</label>
        <br>
        <select name="DO" style="WIDTH: 195px; padding: 2px; margin-top: 2px; border: 2px solid #ccc; padding-left: 2px; font-size: 16px; font-family: raleway">
          <option value="">Select a D.O...</option>
          <option value="AHMEDABAD">AHMEDABAD</option>
          <option value="BANGLORE">BANGLORE</option>
          <option value="CHENNAI">CHENNAI</option>
          <option value="COIMBATORE">COIMBATORE</option>
          <option value="DELHI">DELHI</option>
          <option value="ERNAKULAM">ERNAKULAM</option>
          <option value="HYDERABAD">HYDERABAD</option>
          <option value="KARUR">KARUR</option>
          <option value="KOLKATA">KOLKATA</option>
          <option value="MADURAI">MADURAI</option>
          <option value="MUMBAI">MUMBAI</option>
          <option value="SALEM">SALEM</option>
          <option value="TAMBARAM">TAMBARAM</option>
          <option value="TRICHY">TRICHY</option>
          <option value="VIJAYAWADA">VIJAYAWADA</option>
          <option value="VISAKHAPATNAM">VISAKHAPATNAM</option>
        </select>
        <br>
        <br>

        <label>BranchCode:</label>
        <input id="name" name="branch" placeholder="Enter your BranchCode" type="number" min="1000" max="9999">
        <br>
        <br>

        <input name="submit" type="submit" value=" Submit ">
      </form>
    </div>
  </div>

  <!---->
  <div id="div2" style="display:none">
  </div>

  <div id="div3" style="display:none">
    Another Test
  </div>

  <div id="div4" style="display:none">
    Final Test
  </div>
</div>

答案 1 :(得分:0)

public class calculator extends ActionBarActivity {

private TextView deinbmi;
private EditText weight;
private EditText height;

public void calculator ()
{
    double ergebnis;
    weight=(EditText)findViewById(R.id.text_calculator_weight);
    height=(EditText)findViewById(R.id.text_calculator_height);
    double a=Double.parseDouble(weight.getText().toString());
    double b=Double.parseDouble(height.getText().toString());
    ergebnis=a*b;
    deinbmi.setText(""+ergebnis);
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_calculator, menu);
    //rechnen ?!
    Button credits_calculate = (Button)    
   findViewById(R.id.button_calculate);
    credits_calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        calculator();
        }
    });
    //back
    Button credits_back = (Button)  

  findViewById(R.id.button_calculator_back);
    credits_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            finish();
        }
    });
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
              return true;
       }

        return super.onOptionsItemSelected(item);
    }
}

是我的活动

我尝试使用你的代码但是

    Answer=(Button)findViewById(R.id.answerbuttton);
    Answer.SetOnclicklistener(this);

他不明白这个