将大量数据从一个活动发送到另一个活动?

0 android

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

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

}
Run Code Online (Sandbox Code Playgroud)

}

这是我的第二项活动

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);
}
Run Code Online (Sandbox Code Playgroud)

}

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

该怎么办???

小智 6

假设你有三个EditText分别带有id的edt1,edt2,edt3和一个提交按钮和两个活动,即MainActivity,其中所有三个标签和一个按钮都位于另一个活动,其中有三个TextView分别带有id的txt1,txt2,txt3,你在哪里必须显示信息.

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

intent.putExtra("Key", "Data");
Run Code Online (Sandbox Code Playgroud)

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

然后在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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

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

txt1.setText(getIntent.getStringExtra("Name"));
txt2.setText(getIntent.getStringExtra("Phone"));
txt3.setText(getIntent.getStringExtra("Age"));
Run Code Online (Sandbox Code Playgroud)

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