如何使用onActivityRequest正确返回用户的文本输入

时间:2014-02-28 06:27:00

标签: java android

我遇到某个实验室作业的问题,目标是在屏幕上返回用户的文字。然而,在查看了最深的最深处的互联网之后,我找不到答案。

这是我的代码,请告诉我如何返回用户的输入

AcitivityLoaderActivity类启动活动的代码ExplicitlyLoadedActivity:

    // TODO - Create a new intent to launch the ExplicitlyLoadedActivity class
    Intent explicitActivity = new Intent(ActivityLoaderActivity.this,ExplicitlyLoadedActivity.class);
    // TODO - Start an Activity using that intent and the request code defined above
    startActivity(explicitActivity);

这是用于接收用户编辑文本

的结果的代码
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==GET_TEXT_REQUEST_CODE){
        if(resultCode==RESULT_OK){
            mUserTextView.setText(data.getStringExtra("givenText"));

        }//end if

    }//end if

}

ExplicitlyLoaderActivity类的代码:

private void enterClicked() {
    final int GET_TEXT_REQUEST_CODE = 1;
    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("givenText",givenText);
    // TODO - Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK,editText);
    // TODO - Finish the Activity
    finish();
}

2 个答案:

答案 0 :(得分:1)

AcitivityLoaderActivity类的代码:

使用

        final int GET_TEXT_REQUEST_CODE = 1;
         Intent i=new Intent(AcitivityLoaderActivity.this,ExplicitlyLoadedAcitivty.class);
                 startActivityForResult(i,GET_TEXT_REQUEST_CODE);

onActivityResult()方法

  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==GET_TEXT_REQUEST_CODE){
    if(resultCode==RESULT_OK){
        mUserTextView.setText(data.getStringExtra("givenText"));

    }//end if

}//end if

  }

<强> ExplicitlyLoadedAcitivty

     private void enterClicked() {
final int GET_TEXT_REQUEST_CODE = 1;
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("givenText",givenText);
// TODO - Set Activity's result with result code RESULT_OK
setResult(RESULT_OK,editText);
// TODO - Finish the Activity
finish();
     }

答案 1 :(得分:0)

试试这个

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==GET_TEXT_REQUEST_CODE){
        if(resultCode==RESULT_OK){
            Bundle extras = data.getExtras();
            String editTextString = extras.getString("end_location");

        }//end if

    }//end if

}