• 从一个Activity打开另外一个Activity


    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
        
        public void openActivity(View v){
            //打开新的Activity
            Intent intent = new Intent(this, OtherActivity.class);//激活组件,显示意图:明确指定了组件名称的意图叫显示意图
            //关于指定组件名称,还有很多写法
            //1> intent.setClass(this, OtherActivity.class);//指定要激活的组件名称
            //2> intent.setClassName(this, "cn.itcast.activitys.OtherActivity");//还可以激活别的应用的Activity
            //3> intent.setComponent(new ComponentName(this, OtherActivity.class));
            // intent.setClassName(this, "cn.itcast.activitys.OtherActivity");
            /*
            intent.putExtra("company", "传智播客");
            intent.putExtra("age", 5);
            */
            Bundle bundle = new Bundle();
            bundle.putString("company", "CSDN");
            bundle.putInt("age", 11);
            intent.putExtras(bundle);
            
            //startActivity(intent);
            startActivityForResult(intent, 100);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            String result = data.getStringExtra("result");
            Toast.makeText(getApplicationContext(),result, 1).show();
            
            super.onActivityResult(requestCode, resultCode, data);
        }
        
        
    }
    public class OtherActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.other);
            
            TextView textView = (TextView) this.findViewById(R.id.textView);
            
            Intent intent = getIntent();//获取用于激活它的意图对象
        /*    String company = intent.getStringExtra("company");
            int age = intent.getIntExtra("age", 0);
        */
            Bundle bundle = intent.getExtras();
            String company = bundle.getString("company");
            int age = bundle.getInt("age");
            
            textView.setText("公司名称:"+ company+ ",年限:"+ age);
        }
    
        public void closeActivity(View v){
            Intent intent = new Intent();
            intent.putExtra("result", "老方与丽丽的故事,后面省略2000字");
            setResult(20, intent);//设置返回数据
            finish();//关闭Activity(关闭窗口)
        }
    }
  • 相关阅读:
    Android系统启动:1-综述
    在高通Fastmmi模式中增强交互方式
    Ubuntu 18.04安装xdrp以使用远程桌面
    如何在Android 确定 lunch对应的内核配置
    Android ADB命令集锦
    Android日志系统(logging system)
    汉诺塔游戏
    设置静态ip
    navicat的下载、激活
    上传本地文件到github(码云)上(小乌龟方式,sourcetree方式)
  • 原文地址:https://www.cnblogs.com/heml/p/3514506.html
Copyright © 2020-2023  润新知