在其他两个活动的主Activity中显示数据

时间:2017-08-20 17:47:07

标签: java android android-intent android-activity

我是Android Studio的新手。我正在尝试将EditMessageEditSendTo个活动中的数据发送到我的TestExplicitIntents活动。这是一个学校项目,但我要求的只是创建EditSendTo活动并在TestExplicitIntents活动中显示电话号码。

我可以在其他活动的课程之后执行此操作,但我想学习如何显示来自两个编辑活动的数据。我尝试了几种方法,但是一旦按下EditMessage按钮,我尝试的所有内容都会以EditSendTonull生成Done结果结束。

public class TestExplicitIntents extends Activity {

    public static final String CLASS_TAG = "TestExplicitIntents";
    public static final int NEW_MESSAGE_REQUEST = 1;
    public static final int NEW_PHONE_REQUEST = 1;

    private String message = "";
    private String phone = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test_explicit_intents);

        // Getting to the views defined in the XML files.
        TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
        tvMessageDetails.setBackgroundColor(Color.GREEN);
        tvMessageDetails.setMovementMethod(new ScrollingMovementMethod());
        message ="Is it St. Patricks Day?";
        phone = "";
        setSummary();

        // Responding to an event - the onClick for the Edit Message Button
        // Using a named inner class
        Button btnEditMessage;
        btnEditMessage = (Button) this.findViewById(R.id.btnEditMessage);
        HandleButtonEditMessageOnClick buttonEditMessageOnClick;
        buttonEditMessageOnClick = new HandleButtonEditMessageOnClick();
        btnEditMessage.setOnClickListener(buttonEditMessageOnClick);

        // Responding to an event - the onClick for the Edit Send To Button
        // Using a named inner class
        Button btnEditSendTo;
        btnEditSendTo = (Button) this.findViewById(R.id.btnEditSendTo);
        HandleButtonEditSendToOnClick buttonEditSendToOnClick;
        buttonEditSendToOnClick = new HandleButtonEditSendToOnClick();
        btnEditSendTo.setOnClickListener(buttonEditSendToOnClick);

    }

    /**
     * Put together a summary of the phone and message and display it.
     */
    private void setSummary() {
        StringBuilder summary;

        summary = new StringBuilder("Sending To:\n");
        summary.append(phone);
        summary.append("\n\nMessage:\n");
        summary.append(message);
        TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
        tvMessageDetails.setText(summary);
    }
    /**
     * Handle Edit Button OnClick by starting the activity This is an example of
     * starting another activity using an explicit Intent.
     *
     */
    @SuppressWarnings("rawtypes")
    public class HandleButtonEditMessageOnClick implements OnClickListener {

        public static final String CLASS_TAG = "HandleButtonEditMessageOnClick";

        public void onClick(View v) {
            Log.i(CLASS_TAG, "onClick started...");

            // Example of an EXPLICIT intent, as we are naming the java class to use
            // (EditMessage.class)
            Intent editIntent;
            Activity sourceActivity;
            Class destinationClass;

            sourceActivity = TestExplicitIntents.this;
            destinationClass = EditMessage.class;
            editIntent = new Intent(sourceActivity, destinationClass);

            // Sending information to the intent receiver through the Intent object
            editIntent.putExtra("CURRENT_MESSAGE", TestExplicitIntents.this.message);

            //startActivity(editIntent);
            startActivityForResult(editIntent, NEW_MESSAGE_REQUEST);
        }
    }

    @SuppressWarnings("rawtypes")
    public class HandleButtonEditSendToOnClick implements OnClickListener {

        public static final String CLASS_TAG = "HandleButtonEditSendToOnClick";

        public void onClick(View v) {
            Log.i(CLASS_TAG, "onClick started...");

            Intent editSendIntent;
            Activity startActivity;
            Class endClass;

            startActivity = TestExplicitIntents.this;
            endClass = EditSendTo.class;
            editSendIntent = new Intent(startActivity, endClass);

            // Sending information to the intent receiver through the Intent object
            editSendIntent.putExtra("CURRENT_PHONE", TestExplicitIntents.this.phone);

            //startActivity(editIntent);
            startActivityForResult(editSendIntent, NEW_PHONE_REQUEST);
        }
    }

    @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);

        String newMessage = getIntent().getStringExtra("NEW_MESSAGE");
        String curMessage = getIntent().getStringExtra("CURRENT_MESSAGE");
        String newPhone = getIntent().getStringExtra("NEW_PHONE");
        String curPhone = getIntent().getStringExtra("CURRENT_PHONE");

        // Check which request we're responding to
        if (requestCode == NEW_MESSAGE_REQUEST) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                message = newMessage;
                phone = curPhone;
                setSummary();
            }
        }
        if (requestCode == NEW_PHONE_REQUEST) {
            if (resultCode == RESULT_OK) {
                message = curMessage;
                phone = newPhone;
                setSummary();
            }
        }
    }
}

以下是EditMessage

public class EditMessage extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_message);
        // Get the intent for this activity. Every activity has an intent and
        // set the EditText contents to the string in the extra info that comes with
        // the intent
        Intent editIntent;
        EditText etMessage;
        editIntent = this.getIntent();
        String theMessage;
        theMessage = editIntent.getStringExtra("CURRENT_MESSAGE");
        etMessage = (EditText) this.findViewById(R.id.etMessage);
        etMessage.setText(theMessage);

        // Get an event handler going for the Done button so that we can return the
        // new message
        Button btnDone = (Button) this.findViewById(R.id.btnDone);
        btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Handles the Button Done onClick event by creating a resulting Intent and
     * finishing
     */
    private class ButtonDoneOnClickHandler implements OnClickListener {

        public void onClick(View v) {

            Intent intent = new Intent();
            intent.putExtra("NEW_MESSAGE", ((EditText) findViewById(R.id.etMessage)).getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}

以下是EditSendTo

public class EditSendTo extends Activity {

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

        // Get the intent for this activity. Every activity has an intent and
        // set the EditText contents to the string in the extra info that comes with
        // the intent
        Intent editIntent;
        EditText etPhone;
        editIntent = this.getIntent();
        String thePhone;
        thePhone = editIntent.getStringExtra("CURRENT_PHONE");
        etPhone = (EditText) this.findViewById(R.id.etPhone);
        etPhone.setText(thePhone);

        // Get an event handler going for the Done button so that we can return the
        // new message
        Button btnDone = (Button) this.findViewById(R.id.btnDone);
        btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Handles the Button Done onClick event by creating a resulting Intent and
     * finishing
     */
    private class ButtonDoneOnClickHandler implements OnClickListener {

        public void onClick(View v) {

            Intent intent = new Intent();
            intent.putExtra("NEW_PHONE", ((EditText) 
findViewById(R.id.etPhone)).getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}

我试图解决这个问题几天,没有找到答案 我真的很感激指向正确的方向。

1 个答案:

答案 0 :(得分:0)

您的代码应如下所示:

TestExplicitIntents.java

public class TestExplicitIntents extends Activity {
public static final String CLASS_TAG = "TestExplicitIntents";
public static final int NEW_MESSAGE_REQUEST = 1;
public static final int NEW_PHONE_REQUEST = 2;

private String message = "";
private String phone = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test_explicit_intents);

    // Getting to the views defined in the XML files.
    TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
    tvMessageDetails.setBackgroundColor(Color.GREEN);
    tvMessageDetails.setMovementMethod(new ScrollingMovementMethod());
    message = "Is it St. Patricks Day?";
    phone = "";
    setSummary();

    // Responding to an event - the onClick for the Edit Message Button
    // Using a named inner class
    Button btnEditMessage;
    btnEditMessage = (Button) this.findViewById(R.id.btnEditMessage);
    HandleButtonEditMessageOnClick buttonEditMessageOnClick;
    buttonEditMessageOnClick = new HandleButtonEditMessageOnClick();
    btnEditMessage.setOnClickListener(buttonEditMessageOnClick);

    // Responding to an event - the onClick for the Edit Send To Button
    // Using a named inner class
    Button btnEditSendTo;
    btnEditSendTo = (Button) this.findViewById(R.id.btnEditSendTo);
    HandleButtonEditSendToOnClick buttonEditSendToOnClick;
    buttonEditSendToOnClick = new HandleButtonEditSendToOnClick();
    btnEditSendTo.setOnClickListener(buttonEditSendToOnClick);

}

/**
 * Put together a summary of the phone and message and display it.
 */
private void setSummary() {
    StringBuilder summary;

    summary = new StringBuilder("Sending To:\n");
    summary.append(phone);
    summary.append("\n\nMessage:\n");
    summary.append(message);
    TextView tvMessageDetails = (TextView) findViewById(R.id.tvMessageDetails);
    tvMessageDetails.setText(summary);
}

/**
 * Handle Edit Button OnClick by starting the activity This is an example of
 * starting another activity using an explicit Intent.
 */
@SuppressWarnings("rawtypes")
public class HandleButtonEditMessageOnClick implements View.OnClickListener {

    public static final String CLASS_TAG = "HandleButtonEditMessageOnClick";

    public void onClick(View v) {
        Log.i(CLASS_TAG, "onClick started...");

        // Example of an EXPLICIT intent, as we are naming the java class to use
        // (EditMessage.class)
        Intent editIntent;
        Activity sourceActivity;
        Class destinationClass;

        sourceActivity = TestExplicitIntents.this;
        destinationClass = EditMessage.class;
        editIntent = new Intent(sourceActivity, destinationClass);

        // Sending information to the intent receiver through the Intent object
        editIntent.putExtra("CURRENT_MESSAGE", TestExplicitIntents.this.message);

        //startActivity(editIntent);
        startActivityForResult(editIntent, NEW_MESSAGE_REQUEST);
    }
}

@SuppressWarnings("rawtypes")
public class HandleButtonEditSendToOnClick implements View.OnClickListener {

    public static final String CLASS_TAG = "HandleButtonEditSendToOnClick";

    public void onClick(View v) {
        Log.i(CLASS_TAG, "onClick started...");

        Intent editSendIntent;
        Activity startActivity;
        Class endClass;

        startActivity = TestExplicitIntents.this;
        endClass = EditSendTo.class;
        editSendIntent = new Intent(startActivity, endClass);

        // Sending information to the intent receiver through the Intent object
        editSendIntent.putExtra("CURRENT_PHONE", TestExplicitIntents.this.phone);

        //startActivity(editIntent);
        startActivityForResult(editSendIntent, NEW_PHONE_REQUEST);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Check which request we're responding to
    if (requestCode == NEW_MESSAGE_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            message = data.getStringExtra("NEW_MESSAGE");
            setSummary();
        }
    } else if (requestCode == NEW_PHONE_REQUEST) {
        if (resultCode == RESULT_OK) {
            phone = data.getStringExtra("NEW_PHONE");
            setSummary();
        }
    }
}
}

EditSendTo类:

public class extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_send_to);

    // Get the intent for this activity. Every activity has an intent and
    // set the EditText contents to the string in the extra info that comes with
    // the intent
    Intent editIntent;
    EditText etPhone;
    editIntent = this.getIntent();
    String thePhone;
    thePhone = editIntent.getStringExtra("CURRENT_PHONE");
    etPhone = (EditText) this.findViewById(R.id.etPhone);
    etPhone.setText(thePhone);

    // Get an event handler going for the Done button so that we can return the
    // new message
    Button btnDone = (Button) this.findViewById(R.id.btnDone);
    btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * Handles the Button Done onClick event by creating a resulting Intent and
 * finishing
 */
private class ButtonDoneOnClickHandler implements View.OnClickListener {

    public void onClick(View v) {

        Intent intent = new Intent();
        intent.putExtra("NEW_PHONE", ((EditText)
                findViewById(R.id.etPhone)).getText().toString());
        setResult(RESULT_OK, intent);
        finish();
    }
}
}

EditMessage类:

public class EditMessage extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_message);
        // Get the intent for this activity. Every activity has an intent and
        // set the EditText contents to the string in the extra info that comes with
        // the intent
        Intent editIntent;
        EditText etMessage;
        editIntent = this.getIntent();
        String theMessage;
        theMessage = editIntent.getStringExtra("CURRENT_MESSAGE");
        etMessage = (EditText) this.findViewById(R.id.etMessage);
        etMessage.setText(theMessage);

        // Get an event handler going for the Done button so that we can return the
        // new message
        Button btnDone = (Button) this.findViewById(R.id.btnDone);
        btnDone.setOnClickListener(new ButtonDoneOnClickHandler());
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Handles the Button Done onClick event by creating a resulting Intent and
     * finishing
     */
    private class ButtonDoneOnClickHandler implements View.OnClickListener {

        public void onClick(View v) {

            Intent intent = new Intent();
            intent.putExtra("NEW_MESSAGE", ((EditText) findViewById(R.id.etMessage)).getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}