[英]Android/Java passing value from one screen to another and displaying value
我是初学者,遇到麻烦。 我有两页。 在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”我希望文本出现的第二页上的字段是:
有人可以帮助最后一行显示第二页上的文字吗?
尝试这样的事情
//get the name the user entered
String enterName=getIntent().getStringExtra("PERSON_NAME");
EditText confirmationText = (EditText)findViewById(R.id.confirmationText);
confirmationText.setText(enterName);
finish();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.