Android如何使用onActivityResult接收更多数据

时间:2015-02-21 16:38:29

标签: android onactivityresult

我用startActivityForResult(myIntent, 1);启动对话活动以收回一些数据。 protected void onActivityResult(int requestCode, int resultCode, Intent data)只能收回一种类型的数据吗?

我需要取回一个字符串(来自EditText)和一个资源颜色(RadioButton的背景) 我可以把所有两个都放进returnIntent.putExtra("result",result); ?? 或者我只能发一件事吗?

这是对话活动的代码

public class ActivityAddMateria extends Activity {

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

    final Button exit_button = (Button) findViewById(R.id.exit_dialog_materia);
    exit_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //No input
            Intent returnIntent = new Intent();
            setResult(RESULT_CANCELED, returnIntent);
            //Exit from Dialog
            finish();
        }
    });

    final Button accept_button = (Button) findViewById(R.id.add_materia);
    accept_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Find EditText for take data
            EditText nome_materia = (EditText)findViewById(R.id.nome_materia);
            //Put result into variable result that is send back
            String result = nome_materia.getText().toString();; 

            Intent returnIntent = new Intent();
            returnIntent.putExtra("result",result);
            setResult(RESULT_OK,returnIntent);
            // Exit to Dialog
            finish();
        }
    });

 }
}

这是接受输入的Activity的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){

            String result = data.getStringExtra("result");

            Materia materia;
            materia = new Materia();
            materia.setMateria(result);
            materia.setColor(getResources().getColor(R.color.verde));
            DB.getMaterie().add(materia);

            LinearLayout mLayout = (LinearLayout) findViewById(R.id.contenitorematerie);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT));
            params.setMargins(30, 0, 0, 0);
            params.width = getResources().getDimensionPixelSize(R.dimen.text_view_width);

            for(int i=0; i< DB.getMaterie().size(); i++)
            {
                textView = new TextView(this);
                textView.setText(DB.getMaterie().get(i).getMateria());
                textView.setBackgroundColor(DB.getMaterie().get(i).getColor());
                textView.setGravity(Gravity.CENTER); //centro la scritta nella textview
                textView.setLayoutParams(params);   //assegno i parametri impostati sopra
                mLayout.addView(textView);
            }

        } 

        if (resultCode == RESULT_CANCELED) {
            //Nessuna materia inserita
        }
    }
}//onActivityResult

我需要根据materia.setMateria(result); materia.setColor(getResources().getColor(R.color.verde));

的结果动态设置颜色

1 个答案:

答案 0 :(得分:3)

  

我可以将所有两个放入returnIntent.putExtra(“结果”,结果);?

欢迎您使用不同的密钥添加两个额外内容(例如"result""this is another extra")。