0

朋友我有 2 个文本框...用户必须填写所有文本框,然后按提交按钮。按下提交按钮后,新活动应该开始,所有填写的信息都应该显示在那里。我已经尝试使用意图,但第二个活动仅显示一个文本框的内容。请告诉我该怎么办?提前致谢

package com.example.myfirstapp;

public class MainActivity extends Activity {

static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void sendMessage(View view)
{
    Intent intent = new Intent(this, DisplayMessageActivity.class); 

    EditText editText = (EditText) findViewById(R.id.edit_message);
    EditText editText1 = (EditText) findViewById(R.id.edit_message2);
    String message = editText.getText().toString();
    String message2 = editText1.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    intent.putExtra(EXTRA_MESSAGE, message2);
    startActivity(intent);

}

}

这是我的第二个活动

package com.example.myfirstapp;

public class DisplayMessageActivity extends Activity {

enter code here

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_display_message);

    // Get the message from the intent

    Intent intent = getIntent();

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
    // Show the Up button in the action bar.

   String message2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView textView1 = new TextView(this);
    textView1.setTextSize(40);
    textView1.setText(message2);
    setContentView(textView1);
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_message, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

我只收到第二个文本字段的文本(消息 2)...

该怎么办???

4

10 回答 10

6

假设您有三个 ID 分别为 edt1、edt2、edt3 的 EditText 和一个提交按钮和两个活动,即 MainActivity,所有三个标签和一个按钮都驻留,另一个活动,其中有三个 TextView,ID 分别为 txt1、txt2、txt3,您在其中必须显示信息。

您可以通过以下方式使用意图将信息从一个活动发送到另一个活动:

intent.putExtra("Key", "Data");

其中 Key 是您可以从中发送数据的任何字符串,而 data 是您要发送的信息。请记住,此密钥需要在另一个活动中接收数据。

然后在 MainActivity 首先您需要找到视图,然后在提交按钮单击侦听器上,您需要执行以下操作:

btnSubmit.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(TestActivity.this, AnotherActivity.class);
            intent.putExtra("Name", edt1.getText().toString());
            intent.putExtra("Phone", edt2.getText().toString());
            intent.putExtra("Age", edt3,getText().toString())
            startActivity(intent);
        }
    });

然后在 AnotherActivity 中,您需要找到您定义的所有 TextViews 的视图,然后获取 MainActivity 发送的数据并将其设置为 textview,您需要执行以下操作:

txt1.setText(getIntent.getStringExtra("Name"));
txt2.setText(getIntent.getStringExtra("Phone"));
txt3.setText(getIntent.getStringExtra("Age"));

请记住,密钥必须与发送数据时指定的相同。因此,将它们声明为类的资源字符串或public static final String字段是最佳实践,而不是尝试手动使它们保持同步。

于 2013-08-27T06:03:03.087 回答
4

Try this to pass the values.

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "textbox1");
i.putExtra("Value2", "textbox2"); 
i.putExtra("Value3", "textbox3");
StartActivity(i);

Receiving Activity

Intent in = getIntent();
String tv1= in.getExtras().getString("Value1");
String tv2= in.getExtras().getString("Value2");
String tv3= in.getExtras().getString("Value3");
于 2013-08-27T05:48:41.903 回答
3

您可以使用该Intent.putExtra方法来执行此操作。

例如:

Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("key", "value");
// ... etc

在目标活动的OnResumeor中:OnStart

Intent intent = getIntent();
String value = intent.getStringExtra("key");
// ... etc

看:

http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String , java.lang.String)

http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)

于 2013-08-27T05:49:49.447 回答
3

获取文本框的值并将其存储到一些变量前,val1然后val2使用

Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("Textbox_val1", val1);
intent.putExtra("Textbox_val2", val2);
startActivity(intent)

在第二个 Activity 中写入

Bundle extras = getIntent().getExtras(); 
if(extras !=null) {
String value1 = extras.getString(Textbox_val1);
String value2 = extras.getString(Textbox_val2);
}
于 2013-08-27T05:44:27.343 回答
3
  1. 使用putExtra(),Intent和使用来自不同文本框的不同键
  2. 在另一个 Activity 中,intent.getExtras()通过您的密钥使用和检索信息
于 2013-08-27T05:44:48.203 回答
1

有一个有用的方法是共享偏好。通过它您可以从任何活动中创建、编辑、删除数据,也可以从任何活动中获取数据。

要从要存储数据的活动创建或编辑共享首选项:

String share_pref_file = "your_file_name";      
SharedPreferences prefs1 = getSharedPreferences(
        share_pref_time_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefs1.edit();
editor1.putString("your_data", data); //data is your variable
editor1.commit();

要从您想要数据的活动中获取数据:

String share_pref_file = "your_file_name";
SharedPreferences prefs = getSharedPreferences(share_pref_file,
    Context.MODE_PRIVATE);
String strJson = prefs.getString("your_data", "");

删除:

String share_pref_file = "your_file_name";
SharedPreferences prefs1 = getSharedPreferences(
            share_pref_file, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs1.edit();
    editor.remove(share_pref_file);
    editor.clear();
    editor.commit();
于 2013-08-27T05:42:55.167 回答
1

使用 ArrayList 将大量数据从一个活动发送到另一个活动的最佳方式之一。

这是一个示例将完整的数组列表数据发送到其他活动。

http://prashantandroid.blogspot.in/2013/08/passing-arraylist-from-one-activity-to.html

我希望这能帮到您。

于 2013-08-27T06:19:44.170 回答
0
and for receiving     ``                                                     extrasIntent in = getIntent();
    String first= in.getExtras().getString("FirstTxt");
    String second= in.getExtras().getString("SecondTxt");
    String third= in.getExtras().getString("ThirdTxt");                         it worked for me
于 2015-04-17T12:00:41.387 回答
0
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("FirstTxt", textbox1);
intent.putExtra("SecondTxt", textbox2); 
intent.putExtra("ThirdTxt", textbox3);
StartActivity(intent); 
于 2015-04-17T11:55:05.773 回答
0

这很简单

Intent i = new Intent(this, NewActivity.class);

i.putExtra("variable_name","variable_value"); 开始活动(一);

它开始第二个活动,您可以使用 from bundle 找到相同的数据

捆绑附加服务 = getIntent().getExtras();

if (extras != null){ String value = extras.getString("variable_name"); }

于 2015-04-17T12:09:58.530 回答