Langsung ke konten utama

Belajar Intent (Bagian 2)

Buat Class Java dan Layout untuk Intent1
Layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >
    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Pemanggil"        />
    <TextView android:text="Parameter Pemanggil" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPemanggil" android:layout_width="match_parent">
        <requestFocus></requestFocus>
    </EditText>

    <TextView android:text="Parameter Penerima2" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPenerima2" android:layout_width="match_parent"></EditText>

    <TextView android:text="Parameter Penerima3" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPenerima3" android:layout_width="match_parent"></EditText>

    <Button android:text="Buka Layout Penerima2" android:id="@+id/btnPemanggil2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    <Button android:text="Buka Layout Penerima3" android:id="@+id/btnPemanggil3" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
Java
package com.example.adehodijah.pertemuan2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TheIntentActivity1 extends Activity {
    private static final int REQUEST_CODE2 = 2;
    private static final int REQUEST_CODE3 = 3;

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

        Button btnPemanggil2 = (Button) findViewById(R.id.btnPemanggil2);

        btnPemanggil2.setOnClickListener(new View.OnClickListener() {
            @Override    public void onClick(View view) {
//                hanya manggil saja, tanpa passing parameter//                Intent intent = new Intent(TheIntentActivity1.this, TheIntentActivity2.class);//                startActivity(intent);
                EditText textPemanggil = (EditText) findViewById(R.id.editTextParameterPemanggil);
                String dataPemanggil = textPemanggil.getText().toString();

                Intent intent2 = new Intent(TheIntentActivity1.this, TheIntentActivity2.class);
                intent2.putExtra("value1", dataPemanggil );
                startActivityForResult(intent2, REQUEST_CODE2);

            }
        });

        Button btnPemanggil3 = (Button) findViewById(R.id.btnPemanggil3);

        btnPemanggil3.setOnClickListener(new View.OnClickListener() {
            @Override    public void onClick(View view) {
//                hanya manggil saja, tanpa passing parameter//                Intent intent = new Intent(TheIntentActivity1.this, TheIntentActivity2.class);//                startActivity(intent);
                EditText textPemanggil = (EditText) findViewById(R.id.editTextParameterPemanggil);
                String dataPemanggil = textPemanggil.getText().toString();

                Intent intent3 = new Intent(TheIntentActivity1.this, TheIntentActivity3.class);
                intent3.putExtra("value1", dataPemanggil );
                startActivityForResult(intent3, REQUEST_CODE3);
            }
        });
    }

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE2) {
            if(resultCode == RESULT_OK) {
                if (data.hasExtra("return_value1")) {
                    EditText text2 = (EditText) findViewById(R.id.editTextParameterPenerima2);
                    text2.setText(data.getExtras().getString("return_value1"));
                }
            }

        }else if (requestCode == REQUEST_CODE3) {
            if(resultCode == RESULT_OK) {
                if (data.hasExtra("return_value1")) {
                    EditText text3 = (EditText) findViewById(R.id.editTextParameterPenerima3);
                    text3.setText(data.getExtras().getString("return_value1"));
                }
            }else if(resultCode == RESULT_CANCELED){
                // DO Nothing            }
        }
    }
}

Buat Class Java dan Layout untuk Intent2
Layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >
    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Penerima2"        />
    <TextView android:text="Parameter Pemanggil" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPemanggil" android:layout_width="match_parent">
        <requestFocus></requestFocus>
    </EditText>
    <TextView android:text="Parameter Penerima" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPenerima" android:layout_width="match_parent"></EditText>
    <Button android:text="Buka Layout Pemanggil" android:id="@+id/btnPenerima" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
Java
package com.example.adehodijah.pertemuan2;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TheIntentActivity2 extends Activity {

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

        Button btnPenerima = (Button) findViewById(R.id.btnPenerima);

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }
        String value1 = extras.getString("value1");

        if (value1 != null ) {
            EditText textPenerima = (EditText) findViewById(R.id.editTextParameterPemanggil);
            textPenerima.setText(value1);
        }

        btnPenerima.setOnClickListener(new View.OnClickListener() {
            @Override    public void onClick(View view) {
//                hanya manggil saja, tanpa passing parameter//                Intent intent = new Intent(TheIntentActivity2.this, TheIntentActivity1.class);//                startActivity(intent);                finish();
            }
        });
    }

    @Override    public void finish() {
        Intent data = new Intent();
        EditText textPenerima = (EditText) findViewById(R.id.editTextParameterPenerima);
        String dataPenerima = textPenerima.getText().toString();
        data.putExtra("return_value1", dataPenerima);
        setResult(RESULT_OK, data);
        super.finish();
    }
}

Buat Class Java dan Layout untuk Intent3
Layout
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >
    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Penerima3"        />
    <TextView android:text="Parameter Pemanggil" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPemanggil" android:layout_width="match_parent">
        <requestFocus></requestFocus>
    </EditText>
    <TextView android:text="Parameter Penerima" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPenerima" android:layout_width="match_parent"></EditText>

    <Button android:text="Buka Layout Pemanggil OK" android:id="@+id/btnPenerimaRESULTOK" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    <Button android:text="Buka Layout Pemanggil CANCEL" android:id="@+id/btnPenerimaCANCELED" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>
Java
package com.example.adehodijah.pertemuan2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TheIntentActivity3 extends Activity {

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

        Button btnOK = (Button) findViewById(R.id.btnPenerimaRESULTOK);

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }
        String value1 = extras.getString("value1");

        if (value1 != null ) {
            EditText textPenerima = (EditText) findViewById(R.id.editTextParameterPemanggil);
            textPenerima.setText(value1);
        }

        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override    public void onClick(View view) {
//                hanya manggil saja, tanpa passing parameter//                Intent intent = new Intent(TheIntentActivity2.this, TheIntentActivity1.class);//                startActivity(intent);                Intent data = new Intent();
                EditText textPenerima = (EditText) findViewById(R.id.editTextParameterPenerima);
                String dataPenerima = textPenerima.getText().toString();
                data.putExtra("return_value1", dataPenerima);
                setResult(RESULT_OK, data);
                finish();
            }
        });

        Button btnCancel = (Button) findViewById(R.id.btnPenerimaCANCELED);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override    public void onClick(View view) {
                //setResult(RESULT_CANCELED);                finish();
            }
        });
    }


}

Komentar

Postingan populer dari blog ini

Menampilkan Data MySQL

Web Service: getAccount.php <?php include "connect_db.php"; $query = "SELECT username,password FROM tbaccount"; $result = mysql_query($query, $link) or die('Errorquery:  '.$query); $rows = array(); while ($r = mysql_fetch_assoc($result)) {     $rows[] = $r; } $data = "{Account:".json_encode($rows)."}"; echo $data; ?> Layout Adapter <? xml version= "1.0" encoding= "utf-8" ?> < LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android :layout_width= "fill_parent" android :layout_height= "fill_parent" android :orientation= "vertical" > < LinearLayout android :layout_width= "fill_parent" android :layout_height= "fill_parent" android :layout_weight= "1" android :orientation= "horizontal" > < TextView android...