2017-08-20 11 views
0

私はAndroid Studioの新機能です。 EditMessageEditSendToのアクティビティをTestExplicitIntentsアクティビティに送信しようとしています。これは学校プロジェクトですが、私がしなければならないことは、EditSendToアクティビティを作成し、TestExplicitIntentsアクティビティに電話番号を表示することでした。2つの他のアクティビティからメインアクティビティにデータを表示

他のアクティビティのレッスンの後でこれを行うことができますが、両方の編集アクティビティからデータを表示する方法を学習したいと思います。私はいくつかのアプローチを試しましたが、試してみるとEditMessageまたはEditSendToのいずれかで終了すると、Doneボタンを押すとnullという結果が得られます。ここで

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(); 
     } 
    } 
} 

私は数日間これを理解しようとした答えを見つける運を持っていませんでした。
私は本当に正しい方向にポインタを感謝します。

あなたのコードは以下のようにする必要があり

答えて

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(); 
     } 
    } 
} 
+0

これは 'EditMessageのために働きます'活動、 'EditSendTo'はまだヌルの結果を生成します。 – Jaqtaris

+0

答えでコードを更新しました。今すぐチェックしてください –

+0

'TestExplicitIntents'クラスの' onActivityResult'メソッドを変更しました。私は値を保持するために 'message = message'と' phone = phone'を使用すると、うまくいきました。 – Jaqtaris

関連する問題