0

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

那是我的代码,我不知道有什么问题。主.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;
}

       }

请帮助我谢谢

4

1 回答 1

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

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

于 2013-03-23T05:06:03.567 回答