我有两个编辑文本的输入,这些值从这两个编辑文本中存储,而不是在两个不同的textViews中的下一个活动中显示。我遇到的问题是在输出端,两个文本视图都显示相同的文本,而两个编辑文本的输入值都不同。
这是我的代码。
public final static String EXTRA_MESSAGE = "com.logcatdev.lovescanner.MESSAGE";
public final static String EXTRA_MESSAGE2 = "com.logcatdev.lovescanner.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.names);
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Do something in response to button
Intent intent = new Intent(getApplicationContext(), Scan.class);
EditText editText = (EditText) findViewById(R.id.editText2);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Intent intent2 = new Intent(getApplicationContext(), Scan.class);
//get the value of the editText, convert it to string and store it in a string variable
EditText editText2 = (EditText) findViewById(R.id.editText1);
String message2 = editText2.getText().toString();
intent2.putExtra(EXTRA_MESSAGE, message2);
startActivity(intent);
}
});
下一次活动
Intent intent = getIntent();
Intent intent2 = getIntent();
String message = intent.getStringExtra(Names.EXTRA_MESSAGE);
String message2 = intent2.getStringExtra(Names.EXTRA_MESSAGE2);
//set the value of message to the textview
TextView textView = (TextView)findViewById(R.id.textView2);
textView.setText(message);
TextView textView1 = (TextView)findViewById(R.id.textView3);
textView1.setText(message2);
我在哪里做错了?
答案 0 :(得分:1)
更改不同值的密钥,例如
public final static String EXTRA_MESSAGE = "com.logcatdev.lovescanner.MESSAGE1";
public final static String EXTRA_MESSAGE2 = "com.logcatdev.lovescanner.MESSAGE2";
然后将onClickListener更新为:
@Override
public void onClick(View arg0) {
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
Intent intent = new Intent(getApplicationContext(), Scan.class);
intent.putExtra(EXTRA_MESSAGE, editText1.getText().toString());
intent.putExtra(EXTRA_MESSAGE2, editText2.getText().toString());
startActivity(intent);
}
在您的下一个活动中
Intent intent = getIntent();
String message1 = intent.getStringExtra(Names.EXTRA_MESSAGE);
String message2 = intent.getStringExtra(Names.EXTRA_MESSAGE2);
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1.setText(message1);
TextView textView2 = (TextView) findViewById(R.id.textView3);
textView2.setText(message2);
答案 1 :(得分:0)
你正在创造两种不同的意图。您只需要一个意图来启动第二个活动,然后您可以添加多个String附加功能。所以在你的第一个活动中就是这样做
Intent intent = new Intent(getApplicationContext(), Scan.class);
EditText editText = (EditText) findViewById(R.id.editText2);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
EditText editText2 = (EditText) findViewById(R.id.editText1);
String message2 = editText2.getText().toString();
intent.putExtra(EXTRA_MESSAGE2, message2);
您可以使用
在第二项活动中检索它们String message = intent.getStringExtra(Names.EXTRA_MESSAGE);
String message2 = intent.getStringExtra(Names.EXTRA_MESSAGE2);
答案 2 :(得分:0)
我在第一个活动的java代码中进行了更改
public final static String EXTRA_MESSAGE = "com.logcatdev.lovescanner.MESSAGE";
public final static String EXTRA_MESSAGE2 = "com.logcatdev.lovescanner.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.names);
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Do something in response to button
Intent intent = new Intent(getApplicationContext(), Scan.class);
EditText editText = (EditText) findViewById(R.id.editText1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
//get the value of the editText, convert it to string and store it in a string variable
EditText editText2 = (EditText) findViewById(R.id.editText2);
String message2 = editText2.getText().toString();
intent.putExtra(EXTRA_MESSAGE2, message2);
startActivity(intent);
}
});
发送不同的消息时,您必须发送不同的字符串密钥。 使用此代码可以帮助您!!