当我在第一个屏幕上按Enter键时,我正在尝试设置文本,因此它看起来像第二个图像。每当我尝试设置文本时,第二个屏幕上都不显示任何内容。我已经部分取出了第一个活动中的一些代码,因为它们无关紧要。
我的第一个活动
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);
}
}
}
}
我的第二项活动
public class 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();
}
}
答案 0 :(得分:2)
一个显而易见的问题是,您要将字符串内容设置为文字"mEditText"
而不是mEditText.getText().toString()
,这将具有EditText
的实际内容:
resultIntent.putExtra("userInputMessage ", "mEditText" );
然后:
String value = (String) data.getExtras().getString("userInputMessage");
下一个问题是,在第一次调用中userInputMessage
之后会有一个空格,以便在您稍后尝试获取额外数据时不会匹配。
最好将其定义为public static final String
值,并在两个地方使用引用。