Activity跳转及返回值(相册图片的获取)

BaseActivity

package com.chuanxidemo.shaoxin.demo05;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
 * Created by shaoxin on 2017/2/21.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {

    

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        init();
        setListener();
    }

    protected abstract void setContentView();

    protected abstract void setListener();

    protected abstract void init();

}

MainActivity

package com.chuanxidemo.shaoxin.demo05;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends BaseActivity {

    private Button getBack;
    private ImageView img;
    private Button getValue;



    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void setListener() {
        getBack.setOnClickListener(this);
        getValue.setOnClickListener(this);
    }

    @Override
    protected void init() {
        getBack = (Button) findViewById(R.id.get_back);
        img = (ImageView) findViewById(R.id.img);
        getValue = (Button) findViewById(R.id.get_value);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_back:
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,0x123);//跳转并发送请求码
                break;
            case R.id.get_value:
                Intent intent2 = new Intent(this,Main.class);
//                intent2.putExtra("number",34);
                startActivityForResult(intent2,0x12);//跳转并发送请求码
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x123 && resultCode == RESULT_OK){//比对请求码和接收码
            img.setImageURI(data.getData());
        }
        if (requestCode == 0x12 && resultCode == 0x124){//比对请求码和接收码
            Log.v("msg",data.getIntExtra("number",0)+"");
        }
    }
}

Main

package com.chuanxidemo.shaoxin.demo05;

import android.content.Intent;
import android.view.View;
import android.widget.Button;

/**
 * Created by shaoxin on 2017/2/22.
 */

public class Main extends BaseActivity {
    private Button backValue;


    @Override
    protected void setContentView() {
        setContentView(R.layout.main);
    }

    @Override
    protected void setListener() {
        backValue.setOnClickListener(this);
    }

    @Override
    protected void init() {
        backValue = (Button) findViewById(R.id.back_value);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.putExtra("number",34);//设置想要接收的参数
        setResult(0x124,intent);//设置接收码
        finish();
    }
}

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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.chuanxidemo.shaoxin.demo05.MainActivity">

    <Button
        android:id="@+id/get_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取图片" />
    <Button
        android:id="@+id/get_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取返回值"/>
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:id="@+id/back_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="返回值"/>
</LinearLayout>

 

转载于:https://www.cnblogs.com/ShaoXin/p/6429671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值