没有将EditText中输入的文本返回到Activity上的调用活动

时间:2014-10-11 16:51:51

标签: android onactivityresult

MainActivity.java - 只需一个按钮和一个textview。按钮单击启动具有edittext的ExplicitlyLoadedActivity。用户输入内容并点击"输入"按钮,返回MainActivity.java,并在MainActivity的文本视图中设置文本。

public class ActivityLoaderActivity extends Activity {

static private final int GET_TEXT_REQUEST_CODE = 1;
public String text = "No Text Entered!";
String requiredValue="a";
// TextView that displays user-entered text from ExplicitlyLoadedActivity runs
private TextView mUserTextView;
Intent in;    
@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();

        }
    });        
}


// 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();
    explicitIntent.setClass(getApplicationContext(), ExplicitlyLoadedActivity.class);


    // TODO - Start an Activity using that intent and the request code defined above
    startActivityForResult(explicitIntent, GET_TEXT_REQUEST_CODE);


}    
@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.
    if (requestCode == RESULT_OK) {

            requiredValue = data.getStringExtra("text").toString();
            mUserTextView.setText(requiredValue);
        } 
}
}

ExplicitlyLoadedActivity.java

public class ExplicitlyLoadedActivity extends Activity {

private EditText mEditText;
public String s = "abc";
@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()");
    Toast.makeText(ExplicitlyLoadedActivity.this, "Button clicked...",
            Toast.LENGTH_SHORT).show();
    // TODO - Save user provided input from the EditText field
    s = mEditText.getText().toString();
    Toast.makeText(ExplicitlyLoadedActivity.this, "Explicit : " + s,
            Toast.LENGTH_SHORT).show();

    // TODO - Create a new intent and save the input from the EditText field as extra
    Intent i = getIntent(); //I don't know if this is the problem
    i.putExtra("text", s);
    // TODO - Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK, i);

    // TODO - Finish the Activity
    finish();
}
}

2 个答案:

答案 0 :(得分:0)

你必须构建新的Intent,用“ - Intent i = new Intent()”替换“Intent i = getIntent()”

答案 1 :(得分:0)

您怀疑此问题与您的ExplicitlyLoadedActivity类有关。在enterClicked()方法中,您错误地创建了Intent。你需要把;

Intent explicitIntent = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);

一旦完成,您应该会发现您的代码可以正常工作。

此致 V