将活动A的数据传递给活动B.

时间:2011-11-26 08:51:10

标签: android

我想在这里展示的是我试图将活动A中的数据传递给活动B.活动A主要有3个文本框供我输入,然后按一个按钮转到活动B(确认页面) )在活动B中,我能够显示我在活动A中键入的内容。我是Android的新手,那么有人可以指导我完成此操作吗?

在活动A中

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitya);

    Textview01 = (EditText) this.findViewById(R.id.txtView1);
    Textview02 = (EditText) this.findViewById(R.id.txtView2);
    Textview03 = (EditText) this.findViewById(R.id.txtView3);

    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Intent i = new Intent(ActivityA.this, ActivityB.class); 
            i.putExtra("Textview01", txtView1.getText().toString());
            i.putExtra("Textview02", txtView2.getText().toString());
            i.putExtra("Textview03", txtView3.getText().toString());
            startActivity(i);

在活动B中。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.confirmbooking);

    TextView txtPickup = (TextView) this.findViewById(R.id.txtPickup);
    TextView txtLocation = (TextView) this.findViewById(R.id.txtLocation);
    TextView txtDestination = (TextView) this.findViewById(R.id.txtDestination);

    txtLocation.setText(getIntent().getStringExtra("Location"));
    txtPickup.setText(getIntent().getStringExtra("Pick Up Point"));
    txtDestination.setText(getIntent().getStringExtra("Destination"));

在我的Activity B XML中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="txtView01:" />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/txtView01"></TextView>

    <TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="txtView02:"></TextView>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/txtView02"></TextView>

    <TextView android:id="@+id/TextView02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="txtView03:"></TextView>
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/txtView03"></TextView>

    <Button android:id="@+id/btnButton" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="Book now" />

</LinearLayout>

有人可以告诉我这是否正确?我收到一些错误,如弹出的Instrumental.class。

LogCat显示:

11-26 17:27:40.895: INFO/ActivityManager(52): Starting activity: Intent { cmp=ActivityA/.ActivityB (has extras) }
    11-26 17:27:42.956: DEBUG/dalvikvm(252): GC_EXPLICIT freed 156 objects / 11384 bytes in 346ms
    11-26 17:27:47.815: DEBUG/dalvikvm(288): GC_EXPLICIT freed 31 objects / 1496 bytes in 161ms

4 个答案:

答案 0 :(得分:1)

更改为

<强> A

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitya);

EditText  Textview01 = (EditText) findViewById(R.id.txtView1);
EditText  Textview02 = (EditText) findViewById(R.id.txtView2);
EditText  Textview03 = (EditText) findViewById(R.id.txtView3);

Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        Intent i = new Intent(ActivityA.this, ActivityB.class); 
        i.putExtra("Textview01", Textview01 .getText().toString());
        i.putExtra("Textview02", Textview02 .getText().toString());
        i.putExtra("Textview03", Textview03 .getText().toString());
        startActivity(i);

<强>乙

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activityb);

    TextView txtView01 = (TextView) findViewById(R.id.txtView01);
    TextView txtView02 = (TextView) findViewById(R.id.txtView02);
    TextView txtView03 = (TextView) findViewById(R.id.txtView03);

    String s1 = getIntent().getStringExtra("Textview01");
    String s2 = getIntent().getStringExtra("Textview02");
    String s3 = getIntent().getStringExtra("Textview03");

    txtView01.setText(s1);
    txtView02.setText(s2);
    txtView03.setText(s3); 

答案 1 :(得分:0)

首先你做:

txtView01 = (TextView) this.findViewById(R.id.txtView01);

然后你重新声明这个变量:

String txtView01 = getIntent().getStringExtra("Textview01");

这是一个错误,应该阻止你甚至尝试它。我猜你忘了展示一些代码,比如;在String声明之前,声明了txtView01,以及如何?

答案 2 :(得分:0)

创建Button类型的变量。然后实现onClickListener。

Button bt1=(Button) findViewById(R.id.button);

更改为活动B中的所有字符串赋予的变量名称。

答案 3 :(得分:0)

你做过像

 txtView01 = (TextView) this.findViewById(R.id.txtView01);
        txtView02 = (TextView) this.findViewById(R.id.txtView02);
        txtView03 = (TextView) this.findViewById(R.id.txtView03);

然后将文本分配给TextView,以便您解决问题

你需要制作3个String变量并分配给它,

像String str1,str2,str3 ..

str1 = getIntent().getStringExtra("Textview01");
str2 = getIntent().getStringExtra("Textview02");
str3 = getIntent().getStringExtra("Textview03");

然后将Text设置为TextView 等,

txtView01.setText(str1);
相关问题