获取要在textView上显示的editText值

时间:2013-12-15 18:42:00

标签: android android-activity textview android-edittext settext

我有2个活动。我得到的文字是:

save.setOnClickListener(new View.OnClickListener() {

@Override   
public void onClick(View v) {

Toast.makeText(getApplicationContext(), "Saved Directory!",Toast.LENGTH_SHORT).show();

Intent intent = new Intent(NewCard.this, CardBox.class);

EditText txtName = (EditText) findViewById(R.id.txtName);
EditText txtCompany= (EditText) findViewById(R.id.txtCompany);
EditText txtPhone= (EditText) findViewById(R.id.txtPhone);
EditText txtMobile = (EditText) findViewById(R.id.txtMobile);
EditText txtAddress = (EditText) findViewById(R.id.txtAddress);
EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
EditText txtWebsite = (EditText) findViewById(R.id.txtWebsite);
EditText txtTitle = (EditText) findViewById(R.id.txtTitle);

String Name=txtName.getText().toString(),
Company=txtCompany.getText().toString(), 
Phone=txtPhone.getText().toString(), 
Mobile=txtMobile.getText().toString(), Address=txtAddress.getText().toString(), 
Email=txtEmail.getText().toString(), Website=txtWebsite.getText().toString(), 
Title=txtTitle.getText().toString();

intent.putExtra(EXTRA_MESSAGE, Name);
intent.putExtra(EXTRA_MESSAGE, Company);
intent.putExtra(EXTRA_MESSAGE, Phone);
intent.putExtra(EXTRA_MESSAGE, Mobile);
intent.putExtra(EXTRA_MESSAGE, Address);
intent.putExtra(EXTRA_MESSAGE, Email);
intent.putExtra(EXTRA_MESSAGE, Website); 
intent.putExtra(EXTRA_MESSAGE, Title);
startActivity(intent);
}
});

我将它设置在textView上:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_box);

    // Get the message from the intent
    Intent intent;
    intent = getIntent();
    String Name = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
    String Company = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
    String Phone = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
    String Mobile = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
    String Address = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
    String Email = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
    String Website = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
    String Title = intent.getStringExtra(NewCard.EXTRA_MESSAGE);

    //Display the message in textview
    TextView stv1 = (TextView) findViewById(R.id.stv1);
    TextView stv2 = (TextView) findViewById(R.id.stv2);
    TextView stv3 = (TextView) findViewById(R.id.stv3);
    TextView stv4 = (TextView) findViewById(R.id.stv4);
    TextView stv5 = (TextView) findViewById(R.id.stv5);
    TextView stv6 = (TextView) findViewById(R.id.stv6);
    TextView stv7 = (TextView) findViewById(R.id.stv7);
    TextView stv8 = (TextView) findViewById(R.id.stv8);

    stv1.setText(Name);
    stv2.setText(Company);
    stv3.setText(Phone);
    stv4.setText(Mobile);
    stv5.setText(Address);
    stv6.setText(Email);
    stv7.setText(Website);
    stv8.setText(Title);

最后,当我点击“保存”按钮。 New Activity打开,我看到所有textView都设置为stv8.setText(Title)。

有什么问题?没有错误!

3 个答案:

答案 0 :(得分:1)

您的所有密钥都相同NewCard.EXTRA_MESSAGE。这就是为什么从名称到标题的所有字符串都是相同的。你需要不同的钥匙

intent.putExtra(EXTRA_MESSAGE, Name);
intent.putExtra(EXTRA_MESSAGE, Company);
intent.putExtra(EXTRA_MESSAGE, Phone);
intent.putExtra(EXTRA_MESSAGE, Mobile);
intent.putExtra(EXTRA_MESSAGE, Address);
intent.putExtra(EXTRA_MESSAGE, Email);
intent.putExtra(EXTRA_MESSAGE, Website);
intent.putExtra(EXTRA_MESSAGE, Title);

然后你有

String Name = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Company = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Phone = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Mobile = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Address = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Email = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Website = intent.getStringExtra(NewCard.EXTRA_MESSAGE);
String Title = intent.getStringExtra(NewCard.EXTRA_MESSAGE);

NewCard

中需要的内容
public final static String EXTRA_NAME = "com.example.myfirstapp.NAME";
public final static String EXTRA_COMPANY = "com.example.myfirstapp.COMPANY";
... // similarly for others

然后

intent.putExtra(EXTRA_NAME,姓名);    intent.putExtra(EXTRA_COMPANY,Company);    ...... //与其他人类似 然后在SecondActivity

String Name = intent.getStringExtra(NewCard.EXTRA_NAME);
String Name = intent.getStringExtra(NewCard.EXTRA_COMPANY);
... // similarly for others

我猜你跟着

http://developer.android.com/training/basics/firstapp/starting-activity.html

但你也需要不同的钥匙

public Intent putExtra (String name, String value)

将扩展数据添加到intent中。该名称必须包含包前缀,例如,应用com.android.contacts将使用如(示例)"com.android.contacts.ShowAll".

之类的名称

参数

name    The name of the extra data, with package prefix.
value   The String data value.

返回相同的Intent对象,用于将多个调用链接到单个语句中。

答案 1 :(得分:0)

进行以下更改!

在第一项活动中

intent.putExtra("name", Name);

在第二个:

String Name = intent.getStringExtra("name");

对所有字段重复此操作!

答案 2 :(得分:0)

我不确定“NewCard.EXTRA_MESSAGE”是否可以用于每个值。我之前没有使用过这种方法。每次使用具有相同参数“NewCard.EXTRA_MESSAGE”的Intent.getStringExtra()时,代码如何区分两个值?

您可以使用以下方法。在您从EditText收集数据的活动中,添加以下代码

  Bundle data=new Bundle();
  data.putString("name",Name);
  data.putString("company",Company);
  data.putString("email",Email);
  data.putString("addr",Address);
  data.putString("title",Title);
  data.putString("web",Website);
  data.putString("ph",Phone);
  intent.putExtra("bun",data);

在另一个将值放入TextView的活动中,在onCreate方法

中添加以下代码
     Bundle bun=this.getIntent().getBundleExtra("bun"); // FIXED THIS LINE
     String Name=bun.getString("name");
     String Email=bun.getString("email");
     String Address=bun.getString("addr");
      // AND EXTRACT ALL OTHER VALUES USING CORRESPONDING KEYS IN SIMILAR MANNER