使用EditText更新TextView的值

时间:2015-05-31 06:34:09

标签: java android

我正在尝试将EditText中的文本从一个活动发送到另一个活动。然后,该文本将用于更新第二个Activity中的TextView。使用startActivityForResult()调用EditText活动。我有以下代码。

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();
}

然后将其发送回以下代码。

@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"));
    }
}

其中mUserTextView是我想要更新的TextView。感谢。

1 个答案:

答案 0 :(得分:1)

您没有使用在setResult(RESULT_OK);

中创建的意图

更改

setResult(RESULT_OK, returnIntent);

qemu-monitor-command --hmp myvm 'info snapshots'

它应该有效!

您可以参考this link

相关问题