使用intent extras从另一个活动设置textview

时间:2014-10-14 05:15:22

标签: android android-intent textview

我试图在用户输入另一个结果的字符串后获取文本视图以进行更新。当使用下面的方法时,我得到的是编辑文本的代码地址。类似" android.widget.Edittext(b142f388等等。"无论用户在其他活动中输入什么内容。我在这里缺少什么?

从用户收集的字符串:

private void enterClicked() {

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

    //Save user provided input from the EditText field
    String result = mEditText.toString();

    //Create a new intent and save the input from the EditText field as an extra
    Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
    i.putExtra("RESULT_STRING", result);

    //Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK, i);

    //Finish the Activity
    finish();

}

活动结果:

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

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

    // RESULT_OK result code and a recognized request code
    // If so, update the Textview showing the user-entered text.

    if ( resultCode == RESULT_OK){
        if(requestCode == GET_TEXT_REQUEST_CODE){           
        String userData = data.getStringExtra("RESULT_STRING");
        mUserTextView.setText(userData);
    }    
}

}

2 个答案:

答案 0 :(得分:1)

String result = mEditText.getText().toString();

首先应用此项并检查结果

答案 1 :(得分:0)

您需要使用getText()方法从editText获取文本:

尝试使用此代码:String result = mEditText.getText().toString();

private void enterClicked() {

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

//Save user provided input from the EditText field
String result = mEditText.getText().toString();

//Create a new intent and save the input from the EditText field as an extra
Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
i.putExtra("RESULT_STRING", result);

//Set Activity's result with result code RESULT_OK
setResult(RESULT_OK, i);

//Finish the Activity
finish();

}

相反:

private void enterClicked() {

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

    //Save user provided input from the EditText field
    String result = mEditText.toString();

    //Create a new intent and save the input from the EditText field as an extra
    Intent i = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
    i.putExtra("RESULT_STRING", result);

    //Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK, i);

    //Finish the Activity
    finish();

}