簡體   English   中英

如何使用按鈕將String值從一個活動傳遞到兩個不同的活動?

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

我想通過單擊按鈕將從EditText獲得的值傳遞給兩個活動。 我設法將值傳遞給一個活動,將其顯示在textview中,我想對另一個活動進行同樣的操作,但由於只有一個按鈕,我不確定切換大小寫是否是正確的使用方法。

這是第一個活動的代碼:

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);
    }
});


}   
}

這是第二個顯示值的活動:

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);
    }

    });


}

第三項活動:

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();
}
}

我只想將權重值傳遞給第三項活動。

您一次只能將值傳遞給一個活動。但是,如果要將權重值傳遞給第三個活動,則可以在第一個活動中設置其值,然后在第三個活動中進行檢索。 所以基本上我的意思是

  1. 在putExtra的幫助下,將要傳遞的值放在SecondActivity中。
  2. 設置sharedPrefernce變量。
  3. startActivity(intent);

您可以將值保存在“共享首選項”中,然后更新值。o單擊“按鈕”。這樣,其他活動將可以使用這些值。

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();

}

然后在其他兩個活動中,從“共享首選項”中獲取數據,如下所示:

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

您還可以像這樣在第二個活動中獲取字符串:

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

我看不到您在代碼中調用第三個活動(CompareWeight)的位置。

當您啟動CompareWeight活動時,您可以像使用第二個活動一樣使用意向附加值傳遞體重值。

如果此時沒有權重值,則需要將其保存-@Avadhani Y對於使用SharedPreferences有很好的建議。 或者,您可以使用SQLite數據庫來存儲您的值。

在startActivity之前使用PutExtra。

這是因為在您的Second Activity您只是在調用Intent而不傳遞任何值,

在第二活動中

替換意圖

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

有了這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM