您的位置:首页 > 其它

Intent的使用:显示调用 Activity的跳转

2016-12-06 17:00 351 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: