Android页面跳转以及数据传递实现
2017-09-04 14:01
447 查看
Android的页面跳转通过intent来实现,如果要传递数据,可以通过bundle类来实现,或者直接通过intent.putExtra()方法,把键值对传进去。
实现页面跳转主要有以下几步:
一、创建一个布局文件,并在manifest进行注册
创建布局文件:
创建一个class文件,并继承activity类,重写onCreate方法,绑定对应的布局文件
注册到manifest:
二、通过Intent实现跳转
这里我们设置一个按键,点击从主页面跳到测试页面,并把两个键值对传过去
按键布局就不贴了,大家都懂。跳转主要是实例化Intent对象,调用其setClass方法,里面的两个参数,第一个是当前页面.this,第二个则是跳转目标页面.class,最后调用startActivity(intent)方法跳转。
跳转之前,如果如果需要传递数据,可以通过实例化bundle对象,调用其putString()方法把要传送的键值对放进去,然后调用intent.putExtras(bundle)即可。或者是,如果要传送的数量很少,也可以
a243
直接用intent.putExtra(key,value)方法来实现。
三、目标页面接收数据
实现跳转页面之后,怎么把传过来的数据接收呢?直接看代码
如果是通过bundle传送的,则先获取对应的bundle对象,再调用bundle对象getString()方法,把对应键的值取出来。如果是利用intent直接传过来的,那么直接
另外补充一点,如果想要实现页面在跳转之后不能通过返回键返回上一页面的话,直接在startactivity(intent)方法之后加一句finish();即可。
实现页面跳转主要有以下几步:
一、创建一个布局文件,并在manifest进行注册
创建布局文件:
创建一个class文件,并继承activity类,重写onCreate方法,绑定对应的布局文件
public class Test extends Activity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_turnto); }
注册到manifest:
二、通过Intent实现跳转
这里我们设置一个按键,点击从主页面跳到测试页面,并把两个键值对传过去
public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.turnToId); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putString("me","xiaoming"); bundle.putString("you","xiaoxi"); Intent intent = new Intent(); intent.putExtras(bundle); // intent.putExtra("hello","world"); intent.setClass(MainActivity.this,Test.class); startActivity(intent); } }); } }
按键布局就不贴了,大家都懂。跳转主要是实例化Intent对象,调用其setClass方法,里面的两个参数,第一个是当前页面.this,第二个则是跳转目标页面.class,最后调用startActivity(intent)方法跳转。
跳转之前,如果如果需要传递数据,可以通过实例化bundle对象,调用其putString()方法把要传送的键值对放进去,然后调用intent.putExtras(bundle)即可。或者是,如果要传送的数量很少,也可以
a243
直接用intent.putExtra(key,value)方法来实现。
三、目标页面接收数据
实现跳转页面之后,怎么把传过来的数据接收呢?直接看代码
public class Test extends Activity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_turnto); Bundle bundle2 = this.getIntent().getExtras(); String me = bundle2.getString("me"); String you = bundle2.getString("you"); String hello = this.getIntent().getStringExtra("hellio"); System.out.println(me + " " + you + " " + hello); } }
如果是通过bundle传送的,则先获取对应的bundle对象,再调用bundle对象getString()方法,把对应键的值取出来。如果是利用intent直接传过来的,那么直接
this.getIntent().getStringExtra("hellio")即可,参数同样是键字符串。
另外补充一点,如果想要实现页面在跳转之后不能通过返回键返回上一页面的话,直接在startactivity(intent)方法之后加一句finish();即可。
public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.turnToId); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); bundle.putString("me","xiaoming"); bundle.putString("you","xiaoxi"); Intent intent = new Intent(); intent.putExtras(bundle); intent.putExtra("hello","world"); intent.setClass(MainActivity.this,Test.class); startActivity(intent); finish(); } }); } }
相关文章推荐
- android中页面跳转以及数据在Activity之间的传递
- StoryBoard实现的页面跳转以及数据传递的方法
- Android 使用Intent实现Activity跳转和数据传递
- 简单实现iphone页面跳转和页面间数据传递
- 使用storyboard实现页面跳转,简单的数据传递
- Android 实现页面跳转并传递参数教程
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- Android基础笔记(七)-页面跳转和数据传递
- Android基础笔记(七)-页面跳转和数据传递
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递
- 简单实现iphone页面跳转和页面间数据传递
- Android数据库的增删改查和ListView以及页面跳转的实现
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- 使用storyboard实现页面跳转,简单的数据传递
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- (4.4.1)使用storyboard实现页面跳转,简单的数据传递
- [转]使用storyboard实现页面跳转,简单的数据传递
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- android初步实现Activity之间的跳转和数据传递