如何将TextView添加到另一个活动

时间:2013-05-19 08:10:20

标签: android

假设我有两个活动,即AddViewSeeView

SeeView中,有一个按钮可以转到AddView个活动。在AddView活动中,我将输入SeeView上显示为TextView的文字。

然后,点击AddView上的按钮,我将转到显示SeeView中添加的TextView的AddView。我该如何以编程方式执行此操作?

2 个答案:

答案 0 :(得分:1)

视图在活动之间传递,您要做的是传递文本。 为此,您应该使用活动的startActivityForResult()onActivityResult()方法:

public class SeeView extends Activity {
    public static final int REQ_CODE_ADD_VIEW;

    private TextView textViewWithText;

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

        //this TextView is defined in xml: /res/layout/layout_of_see_view.xml
        //make sure its id is "@+id/text_view_with_text"
        textViewWithText=(TextView) findViewById(R.id.text_view_with_text);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //intent for AddView activity
                Intent intent=...;
                startActivityForResult(intent, REQ_CODE_ADD_VIEW, null);
            }
        })
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);

        if (resultCode==RESULT_OK) {
            if (requestCode==REQ_CODE_ADD_VIEW) {
                String text=data.getStringExtra("text", "");
                textViewWithText.setText(text);
            }
        }
    }
}

你的第二项活动:

public class AddView extends Activity {   
    private EditText editText;

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

        //this is done in case user quits your activity before
        //button is pressed
        setResult(RESULT_CANCELLED);    

        textViewWithText=(TextView) findViewById(R.id.edit_text);

        findViewById(R.id.another_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //prepare result for SeeView activity
                Intent intent=new Intent();
                intent.putStringExtra("text", editText.getText().toString());
                //set the result, it will be passed to onActivityResult() in SeeView activity
                setResult(RESULT_OK, intent);
                finish();
            }
        })
    }
}

答案 1 :(得分:0)

在您的FirstActivity中说主要活动是您有一个editext和一个按钮。用户在editext中输入值。在按钮上单击获取该值。使用意图将值传递给第二个活动。在第二个活动中检索值并在textview中显示相同的值。

     addView.setOnClickListener(new OnClickListener()
    {
       Intent i= new Intent("com.example.secondActivity");
      // Package name and activity
      // Intent i= new Intent(MainActivity.this,SecondActivity.Class);
      // Explicit intents
      i.putExtra("key",editext.getText().toString());//get value from editext
      // Parameter 1 is the key
      // Parameter 2 is your value
      startActiivty(i);
    });

在你的第二个Activity中检索它:

      Bundle extras = getIntent().();
      if (extras != null) {
       TextView tv= (TextView) findViewById(R.id.textview) 
      String value = extras.getString("key");
       //get the value based on the key 
       tv.setText(value);
      }

编辑:在您的第二个活动中,您可以执行类似下面的操作

second.xml

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="74dp"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="fill_parent"
    android:id="@+id/ll"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="157dp"
    android:orientation="vertical" >

  </LinearLayout>

 </RelativeLayout>

SecondActivity

    public class SecondActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
    Bundle extras = getIntent().getExtras();
    if(extras!=null)
    {
        TextView tv= new TextView(this);
        tv.setText(extras.getString("key").toString());
        ll.addView(tv);
    }
}
}