0

我想将值从一页传递到另一页我的代码是:

public class main extends Activity implements OnClickListener {

private static final String TAG = "AskMeShowMe2";
private String choice = null;
private String fname = null;
private Menu myMenu = null;
 final static int START =0;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG,"Main - onCreate() ...");
    setContentView(R.layout.activity_main);

    View promptButton = findViewById(R.id.submit_button);
    promptButton.setOnClickListener((OnClickListener) this);
}

public void onClick(View v) {
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.greetingsRadioGroup);
    int checkedRadioButton = radioGroup.getCheckedRadioButtonId();

    switch (checkedRadioButton) {
    case R.id.mrButton : 
      choice = "Mr.";
      break;
  case R.id.mrsButton : 
      choice = "Mrs.";
      break;
  case R.id.msButton: 
      choice = "Ms.";
      break;
  case R.id.drButton: 
      choice = "Dr.";
      break;
    }
    switch(v.getId()){
    case R.id.submit_button:
        Intent j = new Intent(this, Info.class);
        j.putExtra("choice", choice); 
        startActivity(j);
        fname=String.valueOf(R.id.firstname);
        Intent t = new Intent(this, Info.class);
        j.putExtra("fname", fname); 
        startActivity(t);

        break;
    }
}

以及其他 Info.Class :

public class Info extends Activity {
private static final String TAG = "Info";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info);

    Intent myIntent = getIntent(); 
    Bundle b = myIntent.getExtras(); 
    String choice = b.getString("choice"); 
    String fname = b.getString("fname");

   Log.i(TAG,"selection: " + choice); 
   TextView textView = (TextView) findViewById(R.id.showmeText); 
   textView.append(": " + choice); 
   TextView textView1 = (TextView) findViewById(R.id.fname); 
   textView1.append(": " + fname); 

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.my_menu, menu);
    return true;
}

}

当我运行这个程序时,当我单击提交按钮时,我的 Info.class 页面不显示,你能帮我解决它吗?

4

2 回答 2

1

利用

Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice); 
fname=String.valueOf(R.id.firstname);
j.putExtra("fname", fname); 
startActivity(j);

代替

 Intent j = new Intent(this, Info.class);
    j.putExtra("choice", choice); 
    startActivity(j);
    fname=String.valueOf(R.id.firstname);
    Intent t = new Intent(this, Info.class);
    j.putExtra("fname", fname); 
    startActivity(t);

因为目前您第一次调用 startActivity intent j,第二次调用 startActivity ,intent t所以删除一个并将选择和 fname 放在一个意图中,并且只调用一次 startActivity 。

于 2012-10-08T05:27:51.070 回答
1

将 Switch 块更改为以下内容:

switch(v.getId()){
    case R.id.submit_button:
        Intent j = new Intent(this, Info.class);
        j.putExtra("choice", choice); 
        fname=String.valueOf(R.id.firstname);
        j.putExtra("fname", fname); 
        startActivity(j);

        break;
    }

当您启动 Activity Info 两次时,将附加内容仅添加到一个更早开始的意图中。

于 2012-10-08T05:28:48.347 回答