简体   繁体   中英

How to get an EditText value to go from one activity to another as an int

As the title suggests, I am trying to take values from EditText fields and send them (back) to another activity as modified int values to be used with buttons. The concept of the program is as follows:

The first activity encompasses a tip calculator and there is a button that lets you update the tip amounts for each type of tip (excellent, average, lacking). The second activity allows you to see the original values for this (20, 18, 14) and modify them. I wrote it all and I can pull the values, but I don't know why it doesn't edit them, code below:

Adding clarification: The first activity has a button you can click that leads to the second activity, and then from there you can edit the original tip values, (one or all), and there is a button there that you click to update them (they should not update otherwise). I don't see why the code I have doesn't do that.

package com.example.lab2;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    int excellent_tip = 20;
    int average_tip = 18;
    int bad_tip = 14;
    Button update_button;

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

        update_button = findViewById(R.id.update_button);

        update_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent (MainActivity.this, MainActivity2.class);
                Bundle extras = new Bundle();
                extras.putString("excellent_value", excellent_tip + "");
                extras.putString("average_value", average_tip + "");
                extras.putString("lacking_value", bad_tip + "");
                intent.putExtras(extras);
                startActivity(intent);
            }
        });

        Intent intent = getIntent();
        int excelVal = intent.getIntExtra(MainActivity2.excellNewVal, 20);
        int avgVal = intent.getIntExtra(MainActivity2.avgNewVal, 18);
        int lackVal = intent.getIntExtra(MainActivity2.lackNewVal, 14);

        excellent_tip = excelVal;
        average_tip = avgVal;
        bad_tip = lackVal;
    }

    @SuppressLint("NonConstantResourceId")
    public void onRadioButtonClicked(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        float bill;
        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.excellent_button:
                if (checked) {
                    EditText b = (EditText)findViewById(R.id.bill);
                    if (b.getText().toString().equals(""))
                        bill = 0;
                    else bill = Float.parseFloat(b.getText().toString());
                    compute_tip(bill, excellent_tip);
                }
                break;
            case R.id.average_button:
                if (checked) {
                    EditText b = (EditText)findViewById(R.id.bill);
                    if (b.getText().toString().equals(""))
                        bill = 0;
                    else bill = Float.parseFloat(b.getText().toString());
                    compute_tip(bill, average_tip);
                }
                break;
            case R.id.lacking_button:
                if (checked) {
                    EditText b = (EditText)findViewById(R.id.bill);
                    if (b.getText().toString().equals(""))
                        bill = 0;
                    else bill = Float.parseFloat(b.getText().toString());
                    compute_tip(bill, bad_tip);
                }

                break;
        }
    }
    @SuppressLint("DefaultLocale")
    public static String roundToTwoDigit(float paramFloat) {
        return String.format("%.2f%n", paramFloat);
    }
    void compute_tip(float bill, int percent) {
        float pct= (float)percent/100;
        float tip = bill * pct;
        float total = bill + tip;
        TextView t = (TextView)findViewById(R.id.computed_tip);
        String s = roundToTwoDigit(tip);
        t.setText(s);
        t = (TextView)findViewById(R.id.bill_total);
        s = roundToTwoDigit(total);
        t.setText(s);

    }
}

This is the second activity:

package com.example.lab2;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity2 extends AppCompatActivity {

    EditText excellent_input, average_input, lacking_input;
    Button update_tip;
    int excellentTip, averageTip, lackingTip;
    public static String excellNewVal, avgNewVal, lackNewVal;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        excellent_input = findViewById(R.id.excellent_input);
        average_input = findViewById(R.id.average_input);
        lacking_input = findViewById(R.id.lacking_input);

        Bundle extras = getIntent().getExtras();
        String excellentVal = extras.getString("excellent_value");
        excellentTip = Integer.parseInt(excellentVal);
        String averageVal = extras.getString("average_value");
        averageTip = Integer.parseInt(averageVal);
        String lackingVal = extras.getString("lacking_value");
        lackingTip = Integer.parseInt(lackingVal);

        excellent_input.setHint(excellentVal);
        average_input.setHint(averageVal);
        lacking_input.setHint(lackingVal);

        update_tip = findViewById(R.id.update_tip);

        update_tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity2.this, MainActivity.class);

                String input1 = excellent_input.getText().toString();
                if (input1.length() != 0) {
                    excellentTip = Integer.parseInt(input1);
                    intent.putExtra("excellNewVal", excellentTip);
                }
                else intent.putExtra("excellNewVal", 20);

                String input2 = average_input.getText().toString();
                if (input2.length() != 0) {
                    averageTip = Integer.parseInt(input2);
                    intent.putExtra("avgNewVal", averageTip);
                }
                else intent.putExtra("avgNewVal", 18);

                String input3 = lacking_input.getText().toString();
                if (input2.length() != 0) {
                    lackingTip = Integer.parseInt(input3);
                    intent.putExtra("lackNewVal", lackingTip);
                }
                else intent.putExtra("lackNewVal", 14);
            }
        });
    }
}

Not sure what I am doing wrong.

You need to use onActivityResult, what you are doing is wrong! In your first activity instead of calling startActivity(intent); , use startActivityForResult(intent, requestCode); . And in your second activity to repass data to the first, you use setResult(RESULT_OK, intent) , your intent should contains your numbers modified. Let's backk to the first activity, you need to override onActivityResult , and inside it you get your data. Take a look how your code should be: MainActivity1:

public class MainActivity extends AppCompatActivity {
int excellent_tip = 20;
int average_tip = 18;
int bad_tip = 14;
Button update_button;

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

    update_button = findViewById(R.id.update_button);

    update_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent (MainActivity.this, MainActivity2.class);
            Bundle extras = new Bundle();
            intent.putExtra("excellent_tip", excellent_tip + "");
            intent.putExtra("average_tip", average_tip + "");
            intent.putExtra("bad_tip", bad_tip + "");
            startActivityForResult(intent, 1);
        }
    });

    excellent_tip = 20;
    average_tip = 18;
    bad_tip = 14;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK){
        Intent intent = getIntent();
        excellent_tip = Integer.parseInt(data.getStringExtra("excellNewVal"));
        average_tip = Integer.parseInt(data.getStringExtra("avgNewVal"));
        bad_tip = Integer.parseInt(data.getStringExtra("lackNewVal"));
    }
}

}

MainActivity2:

public class MainActivity2 extends AppCompatActivity {

EditText excellent_input, average_input, lacking_input;
Button update_tip;
int excellentTip, averageTip, lackingTip;
public static String excellNewVal, avgNewVal, lackNewVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    excellent_input = findViewById(R.id.excellent_input);
    average_input = findViewById(R.id.average_input);
    lacking_input = findViewById(R.id.lacking_input);

    Intent intent = getIntent();
    String excellentVal = intent.getExtras().getString("excellent_tip");
    excellentTip = Integer.parseInt(excellentVal);
    String averageVal = intent.getExtras().getString("average_tip");
    averageTip = Integer.parseInt(averageVal);
    String lackingVal = intent.getExtras().getString("bad_tip");
    lackingTip = Integer.parseInt(lackingVal);

    excellent_input.setHint(excellentVal);
    average_input.setHint(averageVal);
    lacking_input.setHint(lackingVal);

    update_tip = findViewById(R.id.update_tip);

    update_tip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();

            String input1 = excellent_input.getText().toString();
            if (input1.length() != 0) {
                intent.putExtra("excellNewVal", input1);
            }
            else intent.putExtra("excellNewVal", "20");

            String input2 = average_input.getText().toString();
            if (input2.length() != 0) {
                intent.putExtra("avgNewVal", input2);
            }
            else intent.putExtra("avgNewVal", "18");

            String input3 = lacking_input.getText().toString();
            if (input2.length() != 0) {
                intent.putExtra("lackNewVal", input3);
            }
            else intent.putExtra("lackNewVal", "14");
            
            setResult(RESULT_OK, intent);
            finish();
        }
    });
}

}

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