2016-08-29 3 views
-2

Ich habe zwei Aktivitäten erstellt. Der erste akzeptiert den Namen eines Schülers über einen Bearbeitungstext und hat eine Senden-Schaltfläche. Wenn die Senden-Schaltfläche gedrückt wird, öffnet sich die nächste Aktivität, die einen Bearbeitungstext (um die Markierungen zu akzeptieren) zusammen mit einer Zurück-Schaltfläche aufweist.Wie senden Sie die Daten von der aktuellen Aktivität an die vorherige Aktivität?

Ich möchte die Markierungen, die in die erste Aktivität eingegeben wurden, zurückgeben, wenn die Zurück-Schaltfläche der zweiten Aktivität gedrückt wird, so dass die Markierungen im ersten Bearbeitungstext angezeigt werden.

Bitte geben Sie mir den Code für die beiden Aktivitäten.

+1

Entschuldigung SO ist kein kostenloser Kodierungsdienst. –

+0

Wenn Sie die Aktivitäten bereits erstellt haben, sollten Sie den Code bereitstellen, den Sie bisher haben. – Laurel

Antwort

0

Sie können Aktivität für das Ergebnis verwenden. Ein Beispiel ist here

+0

Obwohl die Frage nicht zu SO gehört, ist Ihre Antwort von sehr geringer Qualität. Warum sollte das OP "Aktivität für das Ergebnis verwenden"? Eine ** gute Antwort ** wird immer eine Erklärung haben, was getan wurde und warum es so gemacht wurde, nicht nur für das OP, sondern auch für zukünftige Besucher von SO. –

0

Hallo Akanksha Gahalout gegeben, wenn ich verstehe Ihre Frage, können Sie Daten zwischen zwei Aktivitäten und umgekehrt übergeben wollen, wenn dies der Fall, sollten Sie Intent und PutExtrat verwenden() verwenden Methode Um Daten von einer Aktivität zu einer anderen zu übertragen, können Sie Daten aus der zweiten Aktivität mithilfe von Bundle und getExtras() Methode wiederherstellen.

Hier ist ein Beispiel:

FirstActivity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="${relativePackage}.${activityClass}" > 

    <EditText 
     android:id="@+id/EditTextFirstActivity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/StudentName" 
     android:ellipsize="start" 
     android:gravity="center_horizontal" 
     android:labelFor="@+id/EditText1" /> 

    <Button 
     android:id="@+id/buttonFirstActivity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/OK" /> 

    <TextView 
     android:id="@+id/TextViewFirstActivity" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textColor="#000000" 
     android:ellipsize="start" 
     android:gravity="center_horizontal" 
     android:textSize="20sp" /> 

</LinearLayout> 

FirstActivity.java:

package dz.A; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class FirstActivity extends Activity implements OnClickListener { 
    private TextView textView; 
    private EditText editTextFirstActivity; 
    private Button buttonFirstActivity; 

    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.first_activity); 
     editTextFirstActivity = (EditText) findViewById(R.id.EditTextFirstActivity); 
     textView = (TextView) findViewById(R.id.TextViewFirstActivity); 
     buttonFirstActivity = (Button) findViewById(R.id.buttonFirstActivity); 
     buttonFirstActivity.setOnClickListener(this); 
     Intent iin = getIntent(); 
     Bundle b = iin.getExtras(); 

     if (b != null) { 
      String marks = (String) b.get("STUDENT_MARKS"); 
      textView.setText(marks); 
     } 

    } 

    @Override 
    public void onClick(View v) { 
     if (v == buttonFirstActivity) { 
      Log.i("OK", "onClickOK"); 
      Intent i = new Intent(this, SecondActivity.class); 
      Log.i("OK", "IntentOK"); 
      Log.i("OK", "startActivity(i)OK"); 
      String studentName = editTextFirstActivity.getText().toString() 
        .trim(); 
      Log.i("OK", "studentNameOK : " + studentName); 
      i.putExtra("STUDENT_NAME", "Student Name : " + studentName); 
      Log.i("OK", "i.putExtraOK"); 
      startActivity(i); 
      finish(); 
      Log.i("OK", "finish()OK"); 

     } 

    } 


} 

SecondActivity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/EditTextSecondActivity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ellipsize="start" 
     android:gravity="center_horizontal" 
     android:hint="@string/Marks" 
     android:inputType="number" 
     android:labelFor="@+id/EditText1" /> 

    <Button 
     android:id="@+id/buttonSecondActivity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/sendMarks" /> 

    <TextView 
     android:id="@+id/TextViewSecondActivity" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:textColor="#000000" 
     android:textSize="20sp" /> 

</LinearLayout> 

SecondActivity.java:

package dz.A; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SecondActivity extends Activity implements OnClickListener { 
    private TextView textViewSecondActivity; 
    private EditText editTextSecondActivity; 
    private Button buttonSecondActivity; 

    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second_activity); 
     textViewSecondActivity = (TextView) findViewById(R.id.TextViewSecondActivity); 
     editTextSecondActivity = (EditText) findViewById(R.id.EditTextSecondActivity); 
     buttonSecondActivity = (Button) findViewById(R.id.buttonSecondActivity); 
     buttonSecondActivity.setOnClickListener(this); 
     Intent iin = getIntent(); 
     Bundle b = iin.getExtras(); 

     if (b != null) { 
      String studentName = (String) b.get("STUDENT_NAME"); 
      textViewSecondActivity.setText(studentName); 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     if (v == buttonSecondActivity) { 
      String marks = editTextSecondActivity.getText().toString().trim(); 
      Intent i = new Intent(this, FirstActivity.class); 
      i.putExtra("STUDENT_MARKS", "Student Marks : " + marks); 
      startActivity(i); 
      finish(); 

     } 

    } 
} 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="dz.A" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="17" 
     android:targetSdkVersion="23" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="dz.A.FirstActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="dz.A.SecondActivity"/> 


    </application> 

</manifest> 

String.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Project</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="OK">OK</string> 
    <string name="Marks">Marks</string> 
    <string name="sendMarks">sendMarks</string> 
    <string name="StudentName">StudentName</string> 

</resources> 

Ich hoffe, dass dir das hilft.

Verwandte Themen