Android / Java将值从一个屏幕传递到另一个屏幕并显示值

时间:2014-02-03 22:50:25

标签: java android

我是初学者,遇到麻烦。 我有两页。在logon.java上,用户输入名称并单击按钮。在第二个屏幕上,我需要“Hello”+他们在第一页上输入的名称。 所以对于我的第一页:

  public class LogonActivity extends Activity
{ 
private EditText enterName;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //associate view with activity
    setContentView(R.layout.logon);
    //set the button
    Button button=(Button)findViewById(R.id.button);
    //access the field where the user entered the name
    final EditText editTextName= (EditText)findViewById(R.id.enterName);

    //button listener
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //save the editTextName to a new variable -enterName
            String enterName=editTextName.getText().toString(); 

            //create explicit intent
    Intent nameResultsIntent = new 
Intent(getApplicationContext(), 
ConfirmationActivity.class);
            //pass that to next activity
            nameResultsIntent.putExtra("PERSON_NAME",enterName);
            startActivity(nameResultsIntent);
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.logon_main, menu);
    return true;

在第二页是:        公共类ConfirmationActivity扩展了Activity {

EditText enterName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //associate the layout with this activity using setContentView

    setContentView(R.layout.confirmation);

    //get the name the user entered
    String enterName=getIntent().getStringExtra("PERSON_NAME");

    findViewById(R.id.confirmationText);

    String confirmationText = enterName;
    //confirmationText.setText(enterName);
    finish();
}

}

}

}

所以...它是ConfirmationPage上的最后一行。

第一页上的文本字段是:android:id =“@ + id / enterName” 我想要文本出现的第二页上的字段是:

有人可以帮助最后一行在第二页上显示文字吗?

1 个答案:

答案 0 :(得分:2)

尝试这样的事情

//get the name the user entered
String enterName=getIntent().getStringExtra("PERSON_NAME");

EditText confirmationText = (EditText)findViewById(R.id.confirmationText);

confirmationText.setText(enterName);
finish();