【问题标题】:Update Value For TextView using EditText使用 EditText 更新 TextView 的值
【发布时间】:2015-05-31 14:38:38
【问题描述】:

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

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。谢谢。

【问题讨论】:

    标签: java android


    【解决方案1】:

    你没有使用你在enterClicked()中创建的意图

    改变

    setResult(RESULT_OK);
    

    setResult(RESULT_OK, returnIntent);
    

    它应该可以工作!

    您可以参考this link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多