gpt4 book ai didi

android - 获取要在 textView 上显示的 editText 值

转载 作者:行者123 更新时间:2023-11-30 03:13:05 26 4
gpt4 key购买 nike

我有 2 个 Activity 。我通过以下方式获取文本:

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);

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

怎么了?没有错误!

最佳答案

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

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,公司); ...//其他人也一样然后在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

但是你还需要不同的 key

public Intent putExtra(字符串名称,字符串值)

向 Intent 添加扩展数据。该名称必须包含一个包前缀,例如应用程序 com.android.contacts 将使用类似 (Example)"com.android.contacts.ShowAll"的名称。

参数

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

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

关于android - 获取要在 textView 上显示的 editText 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20598220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com