-1

我需要帮助,我正在制作一个简单的应用程序,当我单击“Aceptar”按钮时,我不知道如何将来自微调器的字符串和人名返回到 MainActivity。

MainActivity.java

package com.example.holaamigos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    public final static String EXTRA_SALUDO = "com.example.holaamigos.SALUDO";

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

        final EditText txtNombre = (EditText)findViewById(R.id.TxtNombre);
        final Button btnHola = (Button)findViewById(R.id.BtnHola);
        final CheckBox checkbox1 =(CheckBox)findViewById(R.id.checkBox1);
            checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton arg0,
                        boolean checked) {
                    if (checked)
                        {
                        Toast.makeText(checkbox1.getContext(), "Activo", Toast.LENGTH_LONG).show();
                        btnHola.setVisibility(0);
                        btnHola.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(MainActivity.this, ActivitySaludo.class);
                                String saludo = txtNombre.getText().toString();
                                intent.putExtra(EXTRA_SALUDO, saludo);
                                startActivity(intent);
                            }
                        });

                        }
                    else 
                    {           
                        Toast.makeText(checkbox1.getContext(), "Inactivo", Toast.LENGTH_SHORT).show();
                        btnHola.setVisibility(View.INVISIBLE);
                    }
                }
            });     
        }

    public void  HobbyReturn(int requestcode, int resultadocode, Intent data) {
        if (resultadocode == ActivitySaludo.ACEPTAR_OK); {
            String string = data.getStringExtra(ActivitySaludo.ACEPTAR_OK);
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

活动Saludo

package com.example.holaamigos;


import com.example.holaamigos.R.string;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class ActivitySaludo extends Activity {
    public static final String ACEPTAR_OK = "com.example.holaamigos.ACEPTAR_OK";
    String myspinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saludo);


        Intent intent = getIntent();
        String saludo = intent.getStringExtra(MainActivity.EXTRA_SALUDO);

        TextView txtCambiado = (TextView) findViewById(R.id.TxtSaludo);
        txtCambiado.setText(getString(R.string.hola_saludo) + " " + saludo);

        final Spinner spinner = (Spinner)findViewById(R.id.SpinnerSaludo);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.hobby, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener () {


            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                parent.getItemAtPosition(pos);
                myspinner = spinner.getItemAtPosition(pos).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                //another call

            }

        });

        final Button BtnAceptar=(Button) findViewById(R.id.buttonAceptar);
        BtnAceptar.setOnClickListener(new OnClickListener (){

            @Override
            public void onClick(View v) {
                Intent iboton = new Intent();   
                iboton.putExtra("HOBBY", myspinner);
                setResult(ACEPTAR_OK, iboton);
                finish();
            }


        });

    }

}
4

1 回答 1

1

您需要使用正在等待结果的标志开始您的第二个活动,因此您需要使用startActivityForResult而不是startActivity。 如果您需要更多信息,请查看教程,它应该涵盖了使事情正常运行所需的所有内容。

于 2013-10-23T21:53:36.570 回答