zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    函数模板、函数模板特化、重载函数模板、非模板函数重载
    输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)
    文件的读写、二进制文件的读写、文件随机读写
    文件流(fstream, ifstream, ofstream)的打开关闭、流状态
    流类库继承体系(IO流,文件流,串流)和 字符串流的基本操作
    对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector
    operator new 和 operator delete 实现一个简单内存泄漏跟踪器
    异常与继承、异常与指针、异常规格说明
    程序错误、异常(语法、抛出、捕获、传播)、栈展开
    C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6429671.html
Copyright © 2011-2022 走看看