简体   繁体   中英

How to get value from EditText on the activity to use it on another activity?

How to get value from EditText (editProtein) like this

protected void protein() {
    EditText editCalorie = (EditText)findViewById(R.id.editCalorie);
    double pro = Double.parseDouble(editCalorie.getText().toString());
    EditText editProtein = (EditText)findViewById(R.id.editProtein);
    editProtein.setText(Double.toString(pro * 0.25 * 4));
}

on the activity to use it on another activity? What code should I use on the activity and on another activity, thanks

There are quite a few different ways that you can share data between different Activity classes. How you do it really depends on your specific application and what sort of lifetime that data should have.

The first way is to pass it to the new Activity you launch from the first Activity by means of the putExtras() method to send arbitrary information to the other activity. To fire up the second Activity , you use an Intent ; what you're doing is packing some additional arbitrary data (in this case perhaps your protein stuff):

Bundle bundle = new Bundle();
bundle.putString(“some_protein_stuff″, textStringFromYourEditText);

Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);

On ActivityClass2, we can read this parameter using the syntax:

Bundle bundle = this.getIntent().getExtras();
String param1 = bundle.getString(“some_protein_stuff″);

However, as an alternative, rather than passing the data to the new Activity when you invoke it, you may also want to consider using Shared Preferences . This is a mechanism by which you can store data very easily in your application by means of key-value pairs. If there is an possibility that you will want to use the value that the user input via the EditText in various other Activity classes and also want to use that same value when the application is closed down and opened again later, then it will still be available to you, by simply pulling it back out of the Shared Preferences. Preferences are very easy to use and there are various tutorials on SO or elsewhere.

As another alternative, you could consider sharing data like this 'globally' by making a class that extends android.app.Application , or create a Singleton class, which achieves a similar result to that.

The first most obvious solution of bundling the data in with the Intent when starting the new Activity is probably what you're after, but the latter two suggestions may also be of interest depending on how your application works and how you want that data to be persisted.

You can pass values to another activity. Something like:

intent.putExtra("CALORIE", calorie);
intent.putExtra("PROTEIN", protein);

Corresponding to this, in the activity where you want to use these values, use something like:

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    double calorie = extras.getDouble("CALORIE");
    double protein= extras.getDouble("PROTEIN");
}

EditText extends TextView which has getText method.

http://developer.android.com/reference/android/widget/TextView.html#getText%28%29

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