gpt4 book ai didi

具有 Intent 的 Android onActivityResult()

转载 作者:行者123 更新时间:2023-11-29 20:17:59 26 4
gpt4 key购买 nike

当我在第一个屏幕上按 enter 键时,我试图设置文本,使其看起来像第二个图像。每当我尝试设置文本时,第二个屏幕上什么也没有显示。我已经部分删除了第一个 Activity 中的一些代码,因为它们不相关。

enter image description here enter image description here

我的第一个 Activity

public class ActivityLoaderActivity extends Activity {

static private final int GET_TEXT_REQUEST_CODE = 1;

private void startExplicitActivation() {

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

Intent explicitIntent = null;
explicitIntent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
startActivityForResult(explicitIntent, GET_TEXT_REQUEST_CODE);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Entered onActivityResult()");
if (requestCode == GET_TEXT_REQUEST_CODE) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
String value = (String) data.getExtras().getString("userInputMessage");
mUserTextView.setText(value);
}
}
}
}

我的第二个 Activity
公共(public)类 ExplicitlyLoadedActivity 扩展 Activity {

    static private final String TAG = "Lab-Intents";

private EditText mEditText;

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

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

Intent resultIntent = new Intent();
resultIntent.putExtra("userInputMessage ", "mEditText" );

setResult(RESULT_OK, resultIntent);

finish();
}
}

最佳答案

一个明显的问题是您将字符串内容设置为文字 "mEditText" 而不是 mEditText.getText().toString(),这将具有EditText 的实际内容:

resultIntent.putExtra("userInputMessage ", "mEditText" );

然后:

String value = (String) data.getExtras().getString("userInputMessage");

下一个问题是在第一次调用中 userInputMessage 之后有一个空格,因此当您稍后尝试取回额外数据时它不会匹配。

更好的做法是在某处将其定义为 public static final String 值,并在两个地方都使用该引用。

关于具有 Intent 的 Android onActivityResult(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33592537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com