简体   繁体   English

使用EditText更新TextView的值

[英]Update Value For TextView using EditText

I am trying to sent the text in an EditText from one activity through an intent extra to another activity. 我试图将EditText中的文本从一个活动通过一个额外的意图发送到另一个活动。 The text will then be used to update a TextView in the second Activity. 然后,该文本将用于更新第二个Activity中的TextView。 The EditText activity was invoked using startActivityForResult(). EditText活动是使用startActivityForResult()调用的。 I have the following code. 我有以下代码。

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
    mEditText = (EditText) findViewById(R.id.editText);
    CharSequence userInput = mEditText.getText();
    // TODO - Create a new intent and save the input from the EditText field as an extra
    Intent returnIntent = new Intent(ExplicitlyLoadedActivity.this, ActivityLoaderActivity.class);
    returnIntent.putExtra("returnInput", userInput);
    // TODO - Set Activity's result with result code RESULT_OK
    setResult(RESULT_OK);
    // TODO - Finish the Activity
    finish();
}

This is then sent back to the following 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(resultCode == RESULT_OK && requestCode == GET_TEXT_REQUEST_CODE) {
        mUserTextView.setText(data.getCharSequenceExtra("returnInput"));
    }
}

Where mUserTextView is the TextView I want to update. 其中mUserTextView是我要更新的TextView。 Thanks. 谢谢。

You did not use the intent that you created in enterClicked() 您没有使用您在enterClicked()创建的意图

Change 更改

setResult(RESULT_OK);

to

setResult(RESULT_OK, returnIntent);

and it should work! 它应该工作!

You can refer to this link . 您可以参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM