简体   繁体   中英

How do I get text from a calculated String to a TextView field?

it seems like I am close, but I am missing something with this. I want to create a tip calculator where you enter the amount of your original bill, then click a % button (10, 15, or 20) which calculates your tip and places it into a TextView field, which represents your total bill (called finalBill) with tip added.

But I tried using finalBill.setText(finalB.getText()); , but I get an error saying getText() is undefined. I have only set up the 10% button right now, but once I get it right, I will apply this code to the 15 and 20 methods too.

The original bill will be entered by the user as a double (although it's in an EditText field), the calculation (in the 10% button) needs to happen with doubles, and the final bill should be either a String or some text(?) so that it can fill the TextView field called finalBill. Please try to stick with the original code as much as possible, as I'm a beginner and get confused easily. Thanks for your help!

package com.example.nonitips;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    public EditText originalBill;
    public double billWithTip; 
    String billString;
    public TextView finalBill;


    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        originalBill = (EditText)findViewById(R.id.originalBill);
    }

    public void onTen (View v) {
        Toast.makeText(this, "Tipping at 10%", Toast.LENGTH_SHORT).show();

        originalBill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double bill = Double.parseDouble(originalBill.getText().toString());
                billWithTip = (bill * 0.10) + bill;
                String billString = String.valueOf(billWithTip);
                finalBill = (TextView)findViewById(R.id.finalBill);
                finalBill.setText(billString.getText());
            }
        });

    }

    public void onFifteen (View v) {
        Toast.makeText(this, "Tipping at 15%", Toast.LENGTH_SHORT).show();
    }

    public void onTwenty (View v) {
        Toast.makeText(this, "Tipping at 20%", Toast.LENGTH_SHORT).show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
@Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        originalBill = (EditText)findViewById(R.id.originalBill);
        Button btnTen = (Button) findViewById(R.id.buttonTenPer);  // where your layout has a button with an id "buttonTenPer"
       btnTen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double bill = Double.parseDouble(originalBill.getText().toString());
                billWithTip = (bill * 0.10) + bill;
                String billString = String.valueOf(billWithTip);
                finalBill = (TextView)findViewById(R.id.finalBill);
                finalBill.setText(billString);
            }
        });
    }

So, you were trying to use a String as if it was a TextView:

finalBill.setText(billString.getText());

but this is wrong.

This is correct:

finalBill.setText(billString);

There is another issue: you redeclare billString:

String billString = String.valueOf(billWithTip);

But you already declared in the beginning

String billString;

So, this is enough:

billString = String.valueOf(billWithTip);

And... to apply 10%, this is an overkill:

billWithTip = (bill * 0.10) + bill;

You just need to do so:

billWithTip = bill * 1.10;

And more:

The onClick listener isn't assigned to a Button... it's assigned to an EditText:

originalBill.setOnClickListener(new View.OnClickListener() {
// ...

and originalBill is really an EditText:

public EditText originalBill;
// ...
originalBill = (EditText)findViewById(R.id.originalBill);

So, let me assume you have a button called btnTenPct in your layout:

Do this:

public Button btnTenPct;
// ...
btnTenPct = (Button) findViewById(R.id.btnTenPct);
// ...
btnTenPct.setOnClickListener(new View.OnClickListener() {
// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM