0

假设我有两个活动,即AddViewSeeView

SeeView中,有一个按钮可以带我进行AddView活动。在AddView活动中,我将输入将在SeeViewas上显示的文本TextView

然后,单击 上的按钮AddView,我将被带到显示SeeView添加的 TextView 的AddView位置。如何以编程方式执行此操作?

4

2 回答 2

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();
            }
        })
    }
}
于 2013-05-19T08:41:03.557 回答
0

在您的 FirstActivity 中说主要活动,您有一个编辑文本和一个按钮。用户在 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);
    });

在您的第二个活动中检索它:

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

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

第二个.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>

第二活动

    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);
    }
}
}
于 2013-05-19T08:15:26.830 回答