0

我正在创建应用程序,其中一个类包含 imageview 和 textview,另一个包含 edittext,当我按下返回按钮时,第二类中的 edittext 必须存储并设置在第一类的 textview 中。这是我的代码。

BuckMain.java

package com.jumoun.ibuck;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class BuckMain extends Activity {

    ImageView ivSu;
    TextView tvName;

    private String sSuName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ivSu = (ImageView) findViewById(R.id.ivSu);
        tvName = (TextView) findViewById(R.id.tvName);
        ivSu.setClickable(true);

        ivSu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), BuckDisplay.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                sSuName = data.getStringExtra("name");
                tvName.setText(sSuName);
            }
        }
    }
}

BuckDisplay.java

    package com.jumoun.ibuck;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class BuckDisplay extends Activity {

    ImageView ivSu1;
    TextView tvDisplay;
    TextView tvName1;
    EditText etName1;
    String sName1;

    private int resultCode = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buck_display);
        ivSu1 = (ImageView) findViewById(R.id.ivSu1);
        tvDisplay = (TextView) findViewById(R.id.tvDisplay);
        tvName1 = (TextView) findViewById(R.id.tvName1);
        etName1 = (EditText) findViewById(R.id.etName1);
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        sName1 = etName1.toString();        
        Bundle bundle = new Bundle();
        bundle.putString("name", sName1);
        Intent intent = new Intent();
        setResult(resultCode, intent);
        intent.putExtras(bundle);
        super.onBackPressed();
    }
}

当我按下后退按钮时,它没有显示在头等舱:(

4

1 回答 1

2

你必须调用 startActivityForResult

ivSu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), BuckDisplay.class);
            startActivityForResult(intent, 1);
        }
    });  

并在 setResult 之后调用 super

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub

    Intent intent = new Intent();
    intent.putExtra("name", etName1.getText().toString());
    setResult(RESULT_OK, intent);

    super.onBackPressed();
}  

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {

            sSuName = data.getStringExtra("name");
            tvName.setText(sSuName);
        }
    }
}
于 2013-05-05T23:34:16.433 回答