0

私はアプリを作成しています.1つのクラスにはimageviewとtextviewが含まれ、別のクラスにはedittextが含まれています.戻るボタンを押すと、2番目のクラスの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 に答える