2012-09-14 34 views
-1

我實際上把這個代碼格式爲vogella.com點擊Android中的按鈕後傳遞第二個數據

我想在第二個活動中單擊按鈕時將第二個活動的數據傳遞給第一個活動。爲了確保傳遞的價值是正確的,我希望它在返回到第一個活動時顯示爲敬酒。

這是我在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> 

這是佈局的第二個代碼。 RES/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)。

沒有人知道如何解決這個請。謝謝。

+0

閱讀[thiskey](http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html),那就是問題所在 – Aprian

回答

1

在祝酒詞必須更換「這個」與getBaseContext參數()

0

你是一個內部類中,這應該是IntentintentActivity.this。

 Toast.makeText(IntentintentActivity.this, data.getExtras().getString("returnKey1"), 
     Toast.LENGTH_SHORT).show(); 
相關問題