Intent的使用:顯示調用 Activity的跳轉

package com.zdsoft.startactivity1201;

import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_b, bt_c;
    private final int RC_B = 1;
    private final int RC_C = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        initView();
        initListener();
    }

    private void initView() {
        bt_b = (Button) findViewById(R.id.bt_b);
        bt_c = (Button) findViewById(R.id.bt_c);
    }

    private void initListener() {
        bt_b.setOnClickListener(this);
        bt_c.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_b:
                Intent intent = new Intent(AActivity.this, BActivity.class);
                startActivityForResult(intent, RC_B);
                break;
            case R.id.bt_c:
                Intent intent1 = new Intent(AActivity.this, CActivity.class);
                startActivityForResult(intent1, RC_C);
                break;
            default:
                break;
        }

    }

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

        switch (requestCode) {
            case RC_B:
                if (resultCode == RESULT_OK) {
                    Toast.makeText(AActivity.this, data.getStringExtra("txt"), Toast.LENGTH_SHORT).show();
                }
                break;
            case RC_C:
                if (resultCode == RESULT_OK) {
                    Toast.makeText(AActivity.this, data.getStringExtra("txt"), Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }
}

package com.zdsoft.startactivity1201;

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

public class BActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_b_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        initView();
        initListener();
    }

    private void initView() {
        bt_b_return = (Button) findViewById(R.id.bt_b_return);
    }

    private void initListener() {
        bt_b_return.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_b_return:
                Intent intent = new Intent();
                setResult(RESULT_OK, intent.putExtra("txt", "b頁面"));
                finish();
                break;
            default:
                break;
        }

    }
}

package com.zdsoft.startactivity1201;

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

public class CActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_c_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c);
        initView();
        initListener();
    }

    private void initView() {
        bt_c_return = (Button) findViewById(R.id.bt_c_return);
    }

    private void initListener() {
        bt_c_return.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_c_return:
                Intent intent = new Intent();
                setResult(RESULT_OK, intent.putExtra("txt", "c頁面"));
                finish();
                break;
            default:
                break;
        }

    }
}

<?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:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/bt_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳轉B頁面" />

    <Button
        android:id="@+id/bt_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳轉C頁面" />

</LinearLayout>

<?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:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="b頁面" />

    <Button
        android:id="@+id/bt_b_return"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回" />

</LinearLayout>

<?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:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="c頁面" />

    <Button
        android:id="@+id/bt_c_return"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回" />

</LinearLayout>

發佈了6 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章