Android textfield 和 textview 从一个布局到另一个布局
Android textfield and textview from a layout to another
我有两种布局。
一个是activity_main.xml
另一个是another_activity.xml
在 activity_main
我制作了一个登录屏幕。所以你必须输入你的名字并按“登录”。
user_field = (EditText)findViewById(R.id.name_field);
presentation = (TextView)findViewById(R.id.textView2);
Button signup = (Button)findViewById(R.id.login_butt);
signup.setOnClickListener(new OnClickListener() {
public void onClick(View view){
String danut = user_field.getText().toString();
setContentView(R.layout.another_activity);
presentation.append("danu");
}
});
在您输入您的姓名并按注册后,它会将您重定向到第二个布局并提示您的姓名,"Welcome, your name".
我尝试在 OnClick 中切换指令(通过在附加指令后设置内容视图,但仍然没有成功)
So the result is: you put your name, press the signup/login button,
you are redirected to the new page and there is only "Welcome, " and
nothing.
提前致谢。
更新:
EditText user_field;
TextView presentation;
String final_text = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aprinde =(Button)findViewById(R.id.aprinde);
Button sting = (Button)findViewById(R.id.sting);
Button signup = (Button)findViewById(R.id.login_butt);
user_field = (EditText)findViewById(R.id.name_field);
presentation = (TextView)findViewById(R.id.textView2)
signup.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
String danut = user_field.getText().toString();
final_text = "Welcome, " + danut;
setContentView(R.layout.another_activity);
presentation.setText(final_text);
}
}
);
完整代码+图片。
http://i.imgur.com/BNyDCWa.png
模拟器上的结果,和图片预览是一样的。
试试这个,
String final_text = "";
signup.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
String danut = user_field.getText().toString();
final_text = "Welcome, " + danut;
setContentView(R.layout.another_activity);
presentation.setText = final_text ;
}
}
);
更新: 到目前为止,我们尝试在更改采用空值且仅设置 "Welcome," 的布局后从编辑文本中获取文本文字.
您可以使用 intent,在第一个 activity 中您将拥有登录名 activity,
在另一个中会有 "Welcome..."
传递数据:
第一个Activity:
Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("name",danut);
startActivity(intent);
第二个Activity:
Intent intent = getIntent();
String name = intent.getStringExtra("name");
我有两种布局。
一个是activity_main.xml
另一个是another_activity.xml
在 activity_main
我制作了一个登录屏幕。所以你必须输入你的名字并按“登录”。
user_field = (EditText)findViewById(R.id.name_field);
presentation = (TextView)findViewById(R.id.textView2);
Button signup = (Button)findViewById(R.id.login_butt);
signup.setOnClickListener(new OnClickListener() {
public void onClick(View view){
String danut = user_field.getText().toString();
setContentView(R.layout.another_activity);
presentation.append("danu");
}
});
在您输入您的姓名并按注册后,它会将您重定向到第二个布局并提示您的姓名,"Welcome, your name".
我尝试在 OnClick 中切换指令(通过在附加指令后设置内容视图,但仍然没有成功)
So the result is: you put your name, press the signup/login button, you are redirected to the new page and there is only "Welcome, " and nothing.
提前致谢。
更新:
EditText user_field;
TextView presentation;
String final_text = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aprinde =(Button)findViewById(R.id.aprinde);
Button sting = (Button)findViewById(R.id.sting);
Button signup = (Button)findViewById(R.id.login_butt);
user_field = (EditText)findViewById(R.id.name_field);
presentation = (TextView)findViewById(R.id.textView2)
signup.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
String danut = user_field.getText().toString();
final_text = "Welcome, " + danut;
setContentView(R.layout.another_activity);
presentation.setText(final_text);
}
}
);
完整代码+图片。
http://i.imgur.com/BNyDCWa.png
模拟器上的结果,和图片预览是一样的。
试试这个,
String final_text = "";
signup.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
String danut = user_field.getText().toString();
final_text = "Welcome, " + danut;
setContentView(R.layout.another_activity);
presentation.setText = final_text ;
}
}
);
更新: 到目前为止,我们尝试在更改采用空值且仅设置 "Welcome," 的布局后从编辑文本中获取文本文字.
您可以使用 intent,在第一个 activity 中您将拥有登录名 activity, 在另一个中会有 "Welcome..." 传递数据:
第一个Activity:
Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("name",danut);
startActivity(intent);
第二个Activity:
Intent intent = getIntent();
String name = intent.getStringExtra("name");