• 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>
  • 相关阅读:
    input type=file过滤图片
    tinymce 设置和获取编辑器的内容
    node+express+static完成简单的文件下载
    js代码段
    常用的正则表达式(方便自己看)
    纯js实现日期选取功能
    node+express4+multiparty实现简单文件上传
    判断浏览器类别及版本
    mysql解压缩版安装和卸载
    linux常用命令(自己感觉常用的)
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6429671.html
Copyright © 2020-2023  润新知