通过意图传递多个字符串并显示它们

时间:2013-03-22 21:20:58

标签: android-intent

我是开发Android应用程序的初学者,我有一个功课,所以我需要你的帮助。 我想将3个字符串从一个活动传递到第二个动作,然后在第二个活动中显示这些字符串。用户必须输入字符串。

多数民众赞成我的代码我不知道是什么问题。      Main.java

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.View;
 import android.widget.EditText;

 public class Main extends Activity {

public  static final String EXTRA_MESSAGE = "com.mobile.activity4.class" ;
public  static final String EXTRA_MESSAGE2 = "com.mobile.activity4.class" ;
public  static final String EXTRA_MESSAGE3 = "com.mobile.activity4.class" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.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 myIntent = new Intent(this,DisplayMessageActivity.class);


    EditText editText  = (EditText) findViewById(R.id.edit_message1);
    EditText editText2 = (EditText) findViewById(R.id.edit_message2);
    EditText editText3 = (EditText) findViewById(R.id.edit_message3);
    Bundle extras = new Bundle();

    String message = editText.getText().toString();
    String message2 = editText2.getText().toString();
    String message3 = editText3.getText().toString();

    extras.putString("EXTRA_MESSAGE",message);
    extras.putString("EXTRA_MESSAGE2",message2);
    extras.putString("EXTRA_MESSAGE3",message3);

    myIntent.putExtras(extras);
    startActivity(myIntent);



}

  }

`


DisplayMessageAcivity.java

import android.app.Activity;
 import android.content.Intent;
            import android.os.Bundle;
          import android.view.Menu;
          import android.widget.TextView;

       public class DisplayMessageActivity extends Activity {

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

    Bundle extras = getIntent().getExtras();
    String message  =  extras.getString(Main.EXTRA_MESSAGE);
    String message2 = extras.getString(Main.EXTRA_MESSAGE2);
    String message3 = extras.getString(Main.EXTRA_MESSAGE3);

    TextView textView = new TextView(this);
    TextView textView2 = new TextView(this);
    TextView textView3 = new TextView(this);

    textView.setTextSize(40);
    textView2.setTextSize(40);
    textView3.setTextSize(40);

    textView.setText(message);
    textView2.setText(message2);
    textView3.setText(message3);

    setContentView(textView);
    setContentView(textView2);
    setContentView(textView3);
}

@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;
}

       }

请帮帮我 感谢

1 个答案:

答案 0 :(得分:0)

你在这面面遇到什么问题?

在DisplayMessageActivity中,您必须将所有textview保留在布局中,并在setcontentview中设置布局,如下所示,

      TextView textView = new TextView(this);
      TextView textView2 = new TextView(this);
     TextView textView3 = new TextView(this);
     LinearLayout linear=new LinearLayout(this);
     linear.addView(textView);
     linear.addView(textView2);
     linear.addView(textView3);

     setContentView(linear); 

您也可以在xml中完成此操作

将所有文本视图保留在xml布局中,并将setcontentview中的布局设置为

       setContentView(R.layout.acitivyt_layout);

如果您有任何疑问,请告诉我。