-1

我实际上采用了vogella.com的代码。

单击第二个活动中的按钮时,我想将数据从第二个活动传递到第一个活动。为了确保传递的值是正确的,我希望它在返回第一个活动时显示为敬酒。

这是我在 firstactivity_layout 上的代码。res/activity_intentintent.xml

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

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="60dip"
        android:text="First Activity. Press button to call second activity"
        android:textSize="20sp" >
    </TextView>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Calling an intent" >
    </Button>

</LinearLayout> 

这是布局的第二个代码。水库/picklayout.xml

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



    <EditText
        android:id="@+id/input11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>



    <EditText
        android:id="@+id/input22"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

这是第一个布局的活动代码,IntentintentActivity.java

public class IntentintentActivity extends Activity {
    Button btn1;
    private static final int REQUEST_CODE = 10;

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

    }

    public void onClick(View view) {
        Intent i = new Intent(this, PickActivity.class);
        i.putExtra("Value1", "This value one for ActivityTwo ");
        i.putExtra("Value2", "This value two ActivityTwo");
        // Set the request code to any code you like, you can identify the
        // callback via this code
        startActivityForResult(i, REQUEST_CODE);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
          if (data.hasExtra("returnKey1")) {
            Toast.makeText(this, data.getExtras().getString("returnKey1"),
                Toast.LENGTH_SHORT).show();
          }
        }
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_intentintent, menu);
        return true;
    }

最后是第二个活动,PickActivity.java

public class PickActivity extends Activity{ 
Button submit;
EditText text1, text2;

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

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
          return;
        }
        String value1 = extras.getString("Value1");
        String value2 = extras.getString("Value2");
        if (value1 != null && value2 != null) {
          text1 = (EditText) findViewById(R.id.input11);
          text2 = (EditText) findViewById(R.id.input22);
          text1.setText(value1);
          text2.setText(value2);            

        }   
        submit = (Button)findViewById(R.id.button1);
        /*
        submit.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                Toast.makeText(this, text1.getText(), 2500).show();
            }
        });
        */
    }

public void onClick(View v) {
    Toast.makeText(this, text1.getText(), 2500).show();
    finish();
  }     

      @Override
      public void finish() {
        Intent data = new Intent(this, IntentintentActivity.class);
        Toast.makeText(this, "finish state", 2500).show();
        // Return some hard-coded values
        data.putExtra("returnKey1", "Swinging on a star. ");
        data.putExtra("returnKey2", "You could be better then you are. ");
        setResult(RESULT_OK, data);
        int result = 1;
        //startActivityForResult(data, result);
        super.finish();
      }

}

我试图将按钮的侦听器放在第二个活动中,就像这样。

public class PickActivity extends Activity{ 
Button submit;
EditText text1, text2;

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

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
          return;
        }
        String value1 = extras.getString("Value1");
        String value2 = extras.getString("Value2");
        if (value1 != null && value2 != null) {
          text1 = (EditText) findViewById(R.id.input11);
          text2 = (EditText) findViewById(R.id.input22);
          text1.setText(value1);
          text2.setText(value2);            

        }   
        submit = (Button)findViewById(R.id.button1);

        submit.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                Toast.makeText(this, text1.getText(), 2500).show();
            }
        });

    }

但是它说Toast.makeText(...)不适用于参数(新 View.onClickListener)。

请问有谁知道如何解决这个问题。谢谢你。

4

2 回答 2

1

在您的 Toast 中,您必须将 'this' 参数替换为 getBaseContext()

于 2012-09-14T01:07:54.873 回答
0

你在一个内部类中,这应该是 IntentintentActivity.this。

     Toast.makeText(IntentintentActivity.this, data.getExtras().getString("returnKey1"),
        Toast.LENGTH_SHORT).show();
于 2012-09-14T01:08:11.987 回答