Android-startActivityForResult用法

  • 从子Activity获取返回结果
    public void startActivityForResult(Intent intent, int requestCode)
    该方法的第一个参数是intent,第二个参数是请求代码。请求代码先发送给子activity,然后再返回给父activity的用户定义整数值。当一个activity启动多个不同类型的子activity,且需要判断区分消息回馈方时,通常会用到该请求代码

  • 设置返回结果
    public final void setResult(int resultCode)
    public final void setResult(int resultCode,Intent data)
    一般来说,参数resultCode可以是以下两个预定义常量中的任何一个
    Activity.RESULT_OK
    Activity.RESULT_CANCELED

  • 一个例子的效果图
    这里写图片描述

  • Demo目录
    这里写图片描述

  • 字符串资源

    <string name="one">1+1=?</string>
    <string name="two">2+2=?</string>

    <string name="one_answer">2</string>
    <string name="two_answer">4</string>
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">

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

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/two"/>
</LinearLayout>

  • MainActivity.java
public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_ONE = 1;
    private static final int REQUEST_TWO = 2;

    private Button btn1;
    private Button btn2;

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

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(ChooseActivity.newIntent(MainActivity.this), REQUEST_ONE);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(ChooseActivity.newIntent(MainActivity.this), REQUEST_TWO);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            showResult(requestCode,ChooseActivity.getResultAnswer(data));
        }
    }

    private void showResult(int requestCode, int answer) {
        String str;
        if (requestCode == answer) {
            str = "正确";
        } else {
            str = "错误";
        }
        Snackbar.make(this.getWindow().getDecorView(), str, Snackbar.LENGTH_LONG).setAction("Action", null).show();
    }
}

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

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

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/two_answer"/>
</LinearLayout>

  • ChooseActivity.java
public class ChooseActivity extends AppCompatActivity {

    public static final String RESULT_ANSWER = "result_answer";

    public static Intent newIntent(Context context) {
        return new Intent(context, ChooseActivity.class);
    }

    public static int getResultAnswer(Intent intent) {
        return intent.getIntExtra(RESULT_ANSWER, 0);
    }

    private Button btn1;
    private Button btn2;

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

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent();
                i.putExtra(RESULT_ANSWER, 1);
                setResult(RESULT_OK, i);
                finish();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent();
                i.putExtra(RESULT_ANSWER, 2);
                setResult(RESULT_OK, i);
                finish();
            }
        });
    }
}

  • 如果要使用Snackbar,要加入依赖
    compile 'com.android.support:design:24.0.0-beta1'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值