2015-02-06 48 views
0

我想在ActivityLoaderActivity中顯示來自ExplicitlyLoadedActivity的編輯文本。 ActivityLoaderActivity從意圖活動中獲取價值-roid

package course.labs.intentslab; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class ActivityLoaderActivity extends Activity { 

static private final int GET_TEXT_REQUEST_CODE = 1; 
static private final String URL = "http://www.google.com"; 
static private final String TAG = "Lab-Intents"; 

// For use with app chooser 
static private final String CHOOSER_TEXT = "Load " + URL + " with:"; 

// TextView that displays user-entered text from ExplicitlyLoadedActivity runs 
private TextView mUserTextView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_loader_activity); 

    // Get reference to the textView 
    mUserTextView = (TextView) findViewById(R.id.textView1); 

    // Declare and setup Explicit Activation button 
    Button explicitActivationButton = (Button) findViewById(R.id.explicit_activation_button); 
    explicitActivationButton.setOnClickListener(new OnClickListener() { 

     // Call startExplicitActivation() when pressed 
     @Override 
     public void onClick(View v) { 

      startExplicitActivation(); 

     } 
    }); 

    // Declare and setup Implicit Activation button 
    Button implicitActivationButton = (Button) findViewById(R.id.implicit_activation_button); 
    implicitActivationButton.setOnClickListener(new OnClickListener() { 

     // Call startImplicitActivation() when pressed 
     @Override 
     public void onClick(View v) { 

      startImplicitActivation(); 

     } 
    }); 

} 


// Start the ExplicitlyLoadedActivity 

private void startExplicitActivation() { 

    Log.i(TAG,"Entered startExplicitActivation()"); 

    // TODO - Create a new intent to launch the ExplicitlyLoadedActivity class 
    Intent explicitIntent = new Intent (ActivityLoaderActivity.this,ExplicitlyLoadedActivity.class); 


    // TODO - Start an Activity using that intent and the request code defined above 

    startActivity (explicitIntent); 

} 

// Start a Browser Activity to view a web page or its URL 

private void startImplicitActivation() { 

    Log.i(TAG, "Entered startImplicitActivation()"); 

    // TODO - Create a base intent for viewing a URL 
    // (HINT: second parameter uses Uri.parse()) 

    Intent baseIntent = new Intent (Intent.ACTION_VIEW,Uri.parse(URL)); 

    // TODO - Create a chooser intent, for choosing which Activity 
    // will carry out the baseIntent 
    // (HINT: Use the Intent class' createChooser() method) 
    Intent chooserIntent = Intent.createChooser(baseIntent,CHOOSER_TEXT); 


    Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction()); 


    // TODO - Start the chooser Activity, using the chooser intent 

    startActivity (chooserIntent); 
} 

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

    Log.i(TAG, "Entered onActivityResult()"); 

    // TODO - Process the result only if this method received both a 
    // RESULT_OK result code and a recognized request code 
    // If so, update the Textview showing the user-entered text 

     // Check which request we're responding to 
     if (requestCode == GET_TEXT_REQUEST_CODE) { 
      // Make sure the request was successful 
      if (resultCode == RESULT_OK) { 
       String input = data.getStringExtra("BAG"); 
       mUserTextView.setText(input); 

      } 
     } 

    } 
} 

'

ExplicityLoadedActivity

package course.labs.intentslab; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class ExplicitlyLoadedActivity extends Activity { 

static private final String TAG = "Lab-Intents"; 

private EditText mEditText; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.explicitly_loaded_activity); 

    // Get a reference to the EditText field 
    mEditText = (EditText) findViewById(R.id.editText); 

    // Declare and setup "Enter" button 
    Button enterButton = (Button) findViewById(R.id.enter_button); 
    enterButton.setOnClickListener(new OnClickListener() { 

     // Call enterClicked() when pressed 

     @Override 
     public void onClick(View v) { 

      enterClicked(); 

     } 
    }); 

} 

// Sets result to send back to calling Activity and finishes 

private void enterClicked() { 

    Log.i(TAG,"Entered enterClicked()"); 

    // TODO - Save user provided input from the EditText field 
    String givenText = mEditText.getText().toString(); 
    // TODO - Create a new intent and save the input from the EditText field as an extra 
    Intent editText = new Intent(); 
    editText.putExtra("BAG" ,givenText); 
    // TODO - Set Activity's result with result code RESULT_OK 
    setResult(RESULT_OK , editText); 
    // TODO - Finish the Activity 
    finish(); 
} 
} 

我看了很多帖子#2,但對我不起作用,文本不會出現。 我嘗試了許多變體,包括ActivityLoaderActivity的最後兩行代碼。

回答

0

如果你想留在ActivityLoaderActivity中,這兩行的目的還不清楚。只要刪除它們,你會沒事的。這是給定的,其餘的代碼當然是正確的。

Intent myIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class); 
    startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE); 

另外,如果你確實想重新啓動ActivityLoaderActivity,你應該通過「BAG」額外的參數,在新的意圖和讀取它ActivityLoaderActivity.onCreate():

Intent myIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class); 
myIntent.putExtra("BAG", input); 
startActivityForResult(myIntent, GET_TEXT_REQUEST_CODE); 

確保聲明字符串輸入;外面如果語句也是。

+0

感謝您的回答。我再次嘗試刪除這兩行。不工作。我試過沒有條件,結果一樣。 – 2015-02-06 11:22:57

+0

很高興看到你的代碼爲更好的答案,然後:( – 2015-02-07 19:52:41

+0

現在是所有的代碼,你可以看看startImplicitActivation。同樣沒有工作。 – 2015-02-09 06:06:02