开始结果的新活动

时间:2013-07-09 20:23:26

标签: android android-activity

我遇到了一个无法从活动中回拨的问题。将表单活动1输入到活动2但不能返回。这是我的代码:

活动1:

OnClickListener changeTextOnClickListener = new OnClickListener() {

  @Override
  public void onClick(View v) {
    Intent intent = new Intent(getApplicationContext(), Setting.class);

    startActivityForResult(intent, CHANGETEXT);

  }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);

  if (resultCode == RESULT_OK) {
    switch (requestCode) {
      case CHANGETEXT:
      Bundle b = data.getExtras();
      this.Text = b.getString("Text");

    }
  }
}  

活动2:

@Override
public void onClick(View v) {
  // TODO Auto-generated method stub
  Bundle b = new Bundle();
  Intent intent = new Intent();
  newText.setText(newText.getText().toString());
  Toast.makeText(getApplicationContext(),
    newText.getText().toString(), Toast.LENGTH_LONG).show();
  b.putString("Text", newText.getText().toString());
  intent.putExtras(b);
  setResult(RESULT_OK, intent);
  finish();

}

谁能告诉我哪里出错了?万分感谢。

清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.shotboxv3.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.example.shotboxv3.Setting"
        android:label="@string/app_name" >

    </activity>
</application>

1 个答案:

答案 0 :(得分:0)

由于它是实现OnClickListener的Activity,因此在Activity 2的onCreate中,您需要将要单击的按钮的侦听器设置为它所在的Activity:

myButton.setOnClickListener(this);