简体   繁体   English

如何使用按钮将String值从一个活动传递到两个不同的活动?

[英]How do you pass String value using button from one activity to two different activities?

I would like to pass a value that I get from an EditText to two activities with a click of a button. 我想通过单击按钮将从EditText获得的值传递给两个活动。 I managed to pass the value to one activity, displaying it in textview and i would like to do the same to another activity but I'm not sure if switch case is the right method to use since there's only one button. 我设法将值传递给一个活动,将其显示在textview中,我想对另一个活动进行同样的操作,但由于只有一个按钮,我不确定切换大小写是否是正确的使用方法。

This is the codes for the first activity: 这是第一个活动的代码:

public class ProfInfo1 extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profinfo1);


    final EditText et0 = (EditText) findViewById (R.id.et_name);
    final EditText et1 = (EditText) findViewById (R.id.et_age);
    final EditText et2 = (EditText) findViewById (R.id.et_height);
    final EditText et3 = (EditText) findViewById (R.id.et_weight);

    Button back = (Button) findViewById(R.id.btn_back1);
    back.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        Intent a = new Intent(ProfInfo1.this, WelcomeActivity.class);
        startActivity(a);
    }

    });

    Button next = (Button) findViewById(R.id.btn_next1);
    next.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(ProfInfo1.this, ViewProf1.class);
        intent.putExtra("name", et0.getText().toString());
        intent.putExtra("age", et1.getText().toString());
        intent.putExtra("height", et2.getText().toString());
        intent.putExtra("weight", et3.getText().toString());
        startActivity(intent);
    }
});


}   
}

This is the second activity where the values are displayed: 这是第二个显示值的活动:

public class ViewProf1 extends Activity {
EditText age, height, weight;
String a, h, w;
Float age1, height1, weight1;
float bmi, cal;

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

    TextView nameView = (TextView) findViewById (R.id.view_name);
    nameView.setText("Hello " + getIntent().getExtras().getString("name") + ",");
    TextView ageView = (TextView) findViewById (R.id.view_age);
    ageView.setText("Age: " + getIntent().getExtras().getString("age"));
    TextView heightView = (TextView) findViewById (R.id.view_height);
    heightView.setText("Your current height is " + getIntent().getExtras().getString("height") + " m.");
    TextView weightView = (TextView) findViewById (R.id.view_weight);
    weightView.setText("Your current weight is " + getIntent().getExtras().getString("weight") + " kg.");

    a = getIntent().getExtras().getString("age");
    h = getIntent().getExtras().getString("height");
    w = getIntent().getExtras().getString("weight");

    age1 = Float.valueOf(a).floatValue();
    height1 = Float.valueOf(h).floatValue();
    weight1 = Float.valueOf(w).floatValue();

    //bmi calculation
    bmi = (float) (weight1/Math.pow(height1, 2));
    TextView showresult = (TextView) findViewById (R.id.view_bmi);
    showresult.setText("Your BMI is " + Float.valueOf(bmi) + ".");

    //cal intake calculation
    cal = (float) (((655 + (9.6 * weight1) + (1.8 * height1) - (4.7 * age1)) * 1.2) - 550);
    TextView showcal = (TextView) findViewById (R.id.view_cal);
    showcal.setText("Your ideal daily calories intake is " + Float.valueOf(cal) + ".");

    //next button
    Button next2 = (Button) findViewById (R.id.btn_next2);
    next2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        Intent a = new Intent(ViewProf1.this, FoodDiary.class);
        startActivity(a);
    }

    });

    //back button
    Button back2 = (Button) findViewById (R.id.btn_back2);
    back2.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        Intent a = new Intent(ViewProf1.this, ProfInfo1.class);
        startActivity(a);
    }

    });


}

the third activity: 第三项活动:

public class CompareWeight extends Activity {

EditText weight;
String w;
Float weight1;

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

    TextView weightView = (TextView) findViewById (R.id.test);
    weightView.setText("Your previous weight is " + getIntent().getExtras().getString("weight") + " kg.");

    w = getIntent().getExtras().getString("weight");

    weight1 = Float.valueOf(w).floatValue();
}
}

I only want to pass the weight value to the third activity. 我只想将权重值传递给第三项活动。

you can pass values to only one activity at a time.but if you want to pass weight value to third activity,then you can set it's value in first activity and retrieve in third activity. 您一次只能将值传递给一个活动。但是,如果要将权重值传递给第三个活动,则可以在第一个活动中设置其值,然后在第三个活动中进行检索。 So basically I mean 所以基本上我的意思是

  1. Put values that you want to pass in SecondActivity with the help of putExtra. 在putExtra的帮助下,将要传递的值放在SecondActivity中。
  2. Set the sharedPrefernce variable. 设置sharedPrefernce变量。
  3. startActivity(intent); startActivity(intent);

您可以将值保存在“共享首选项”中,然后更新值。o单击“按钮”。这样,其他活动将可以使用这些值。

Use SharedPreferences in onPause for current Activity as below and send the data to other activities as below: onPause中将SharedPreferences用于当前Activity ,如下所示,并将数据发送到其他活动,如下所示:

@Override
protected void onPause() 
{
  super.onPause(); 
  SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
  SharedPreferences.Editor editor = preferences.edit();  
  editor.putString("SearchText",edt.getText().toString());
  editor.commit();

}

and in other two Activities, get the data from Shared Preferences as below: 然后在其他两个活动中,从“共享首选项”中获取数据,如下所示:

 SharedPreferences preferences = getSharedPreferences("sharedPrefs", 0);
 String edtText = preferences.getString("SearchText","");

You can also get your string in the second activity like this : 您还可以像这样在第二个活动中获取字符串:

Bundle extras = getIntent().getExtras();
        if(extras != null) {
            item = extras.getString("myitempassed");
        }

I don't see where in the code you are calling the third activity (CompareWeight). 我看不到您在代码中调用第三个活动(CompareWeight)的位置。

When you start the CompareWeight activity, you can just pass in the weight value using intent extras like you did for the second activity. 当您启动CompareWeight活动时,您可以像使用第二个活动一样使用意向附加值传递体重值。

If you don't have the weight value at that point, then you'll need to save it - @Avadhani Y has a good suggestion for using SharedPreferences. 如果此时没有权重值,则需要将其保存-@Avadhani Y对于使用SharedPreferences有很好的建议。 Or you can use a SQLite DB to store your values. 或者,您可以使用SQLite数据库来存储您的值。

在startActivity之前使用PutExtra。

It is because in your Second Activity you just calling the Intent without passing any values in it, 这是因为在您的Second Activity您只是在调用Intent而不传递任何值,

In Second Activity 在第二活动中

Replace the Intent 替换意图

    Intent a = new Intent(ViewProf1.this, FoodDiary.class);
    startActivity(a); 

With this 有了这个

    Intent a = new Intent(ViewProf1.this, FoodDiary.class);
    a.putExtra("weight", weight1);
    startActivity(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将相同的String值从适配器传递到两个不同的活动 - How to pass same String value from adapter to two different activities 如何同时将数据从一个活动传递到两个或多个活动? - How to pass data from one activity to two or more activities simultaneously? 你如何通过2个活动传递一个字符串? - How do you pass a string through 2 activities? 如何将String从2个活动传递到一个活动? - How to pass String from 2 activities to one? 如何将按钮单击时的 int 值从一个活动传递到另一个活动? - How to pass int value on button click from one activity to another? 您如何在Android的两个不同活动之间通过Parse查询? - How do you query from Parse between two different activities in Android? 如何在Android中将两个字符串从一个活动传递到另一个活动 - How to pass two Strings from one Activity to another Activity in Android 从Android中的两个不同活动打开时的不同活动行为 - Different Behaviors of Activity when Opened from two Different Activities in Android 单击来自两个不同活动的两个单选按钮时,如何在新活动中显示文本? - How to display text in a new activity when two radio buttons from two different activities are clicked? 如何在一键式点击中关闭两个活动? - How to close two activities in one button click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM