setresult不适用于setonactivityresult

时间:2015-08-10 09:13:21

标签: android

我的代码如下所示:

private void intialize() {
        // TODO Auto-generated method stub
        question=(EditText) findViewById(R.id.question);
        startfr=(Button) findViewById(R.id.startfr);
        start=(Button) findViewById(R.id.start);
        result=(TextView) findViewById(R.id.result);
        start.setOnClickListener(this);
        startfr.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start:
            String que=question.getText().toString();
            Bundle extra= new Bundle();
            extra.putString("key", que);
            Intent a=new Intent(Data.this,Openedclass.class);
            a.putExtras(extra);
            startActivity(a);
            break;

        case R.id.startfr:
            Intent i=new Intent(Data.this,Openedclass.class);
            startActivityForResult(i,0);
            break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("hello");
        if (resultCode==100) {
            Bundle result1=data.getExtras();
            s=result1.getString("answer");

        }
        result.setText("s");
    }


}

public class Openedclass extends Activity implements OnClickListener,OnCheckedChangeListener{
    TextView question,result;
    Button return1;
    RadioGroup rg;
    String answer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send);
        intialize();
    /*  Bundle get=getIntent().getExtras();
        String que=get.getString("key");
        question.setText(que);
    */}

    private void intialize() {
        // TODO Auto-generated method stub
        question=(TextView) findViewById(R.id.tvquestion);
        result=(TextView) findViewById(R.id.result);
        return1=(Button) findViewById(R.id.send);
        rg=(RadioGroup) findViewById(R.id.answer);
        rg.setOnCheckedChangeListener(this);
        return1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent i=new Intent();
        Bundle data=new Bundle();
        data.putString("answer", answer);
        System.out.println(answer);
        i.putExtras(data);
        setResult(100, i);
        finish();

    }



    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
        case R.id.ans1:
            answer="right";
            break;

        case R.id.ans2:
            answer="almost";
            break;

        case R.id.ans3:
            answer="both";
            break;
        }
        Toast t1=Toast.makeText(this, answer, Toast.LENGTH_LONG);
        t1.show();
        result.setText(answer);
    }


}

1 个答案:

答案 0 :(得分:0)

您始终将result设置为包含" s"用你的代码:

result.setText("s"); 

请改用:

result.setText(""+s);