• 安卓学习-activity-窗体调用和传参


    主要是传递自定义对象

     窗体A

    窗体B

    窗体A调用窗体B,B确定后回调A方法

    MainActivity

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            Button btn=(Button)findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent in=new Intent(MainActivity.this,Activity1.class);
                    Bundle bundle=new Bundle();
                    //传递boolean
                    bundle.putBoolean("boolean", true);
                    bundle.putStringArray("strings", new String[]{"苹果","鸭梨","香蕉"});
                    
                    ArrayList<HashMap<String, Object>> usersMap = new ArrayList<HashMap<String, Object>>();
                    for(int i=0;i<2;i++){
                        HashMap<String, Object> a=new HashMap<String, Object>();
                        a.put("id", i);
                        a.put("user"+i, new User("XXX"+i,i));
                        usersMap.add(a);
                    }
                    bundle.putSerializable("usersMap", usersMap);
    
                    ArrayList<User> users=new ArrayList<User>();
                    for(int i=0;i<2;i++){
                        User user=new User("XXX"+i,i);
                        users.add(user);
                    }
                    bundle.putSerializable("users", users);
                    User user=new User("XXX",1);
                    bundle.putSerializable("user", user);
                    
                    in.putExtras(bundle);
                    startActivityForResult(in, 100);
                }
            });
            
        }
        
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if((requestCode==100)&&(resultCode==100)){
                
                Toast.makeText(MainActivity.this, data.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
            }
        }
    }
    View Code

    Activity1

    public class Activity1 extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_activity1);
            
            Intent in=getIntent();
            Bundle ble=in.getExtras();
            
            EditText et=(EditText)findViewById(R.id.editText1);
            String str="boolean:"+ble.getBoolean("boolean")+"
    ";
            str=str+"数组:"+ble.getStringArray("strings")[0].toString()+"
    ";
            
            ArrayList<HashMap<String, Object>> usersMap=(ArrayList<HashMap<String, Object>>)ble.getSerializable("usersMap");
            str=str+"Map的List:"+usersMap.toString()+"
    ";
            
            ArrayList<User> users=(ArrayList<User>)ble.getSerializable("users");
            str=str+"User的List:"+users.toString()+"
    ";
            
            User user=(User)ble.getSerializable("user");
            str=str+"对象:"+user.toString()+"
    ";
            
            et.setText(str);
            
            Button btn=(Button)findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent in=new Intent();
                    in.putExtra("msg", "成功222");
                    setResult(100,in);
                    finish();
                }
            });
    
        }
    }
    View Code
  • 相关阅读:
    yii2之依赖注入与依赖注入容器
    MySQL查询性能优化
    MySQL索引(2)
    go es
    es数据操作入门
    es环境搭建
    gRPC接入etcd
    etcd简单介绍
    Go调用NSQ简单实践
    NSQ快速入门实践
  • 原文地址:https://www.cnblogs.com/weijj/p/4148270.html
Copyright © 2020-2023  润新知