1、窗口直接跳转
1 Intent intent = new Intent(MainActivity.this, B.class);2 startActivity(intent);
2、传值跳转
A—>B
1 Intent intent = new Intent(this, B.class);2 Bundle bundle = new Bundle();3 bundle.putString("Apple", "ss");4 intent.putExtras(bundle);5 startActivity(intent, 1);
B接收A的传值
1 Bundle bundle = this.getIntent().getExtras();2 String reFlage = bundle.getString("Apple");
3、有返回值的跳转
我们只是需要在另外加上两段代码:
(1)将B中需要返回的值传给A:
1 Intent intent = new Intent(AppleActivity.this, MyFristActivity.class);2 intent.putExtra("Apple", "Apple!");3 intent.putExtra("Not", "What\'s it!");4 setResult(1, intent);5 finish(); // 表示关闭B,否则A不能够接收B的传值
(2)在A中重载onActivityResult方法来接收返回的参数。
1 /* 2 * 接收回传 3 */ 4 @Override 5 public void onActivityResult(int requestCode, int resultCode, Intent data) { 6 super.onActivityResult(requestCode, resultCode, data); 7 8 String result = data.getExtras().getString("Not"); 9 appleTextView = (TextView) findViewById(R.id.btn_return_apple_tv);10 appleTextView.setText(result);11 12 }
4、弹出dialog
dialog可以变相的看做是一个Acitivity,其构建方式和Activity一致,只需将其形式改变为弹出框,有两种方法:
(1)在Mainfest.xml文件中修改样式为:
android:theme="@android:style/Theme.Dialog"
(2)调用代码:
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog, (ViewGroup) findViewById(R.id.dialog)); new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout) .setPositiveButton("确定", null) .setNegativeButton("取消", null).show();