您现在的位置是:首页 > 文章详情

安卓之页面跳转与传值和按钮事件

日期:2017-05-03点击:410

一:新建页面

即新建Activity,new-other-Android Activity,next,

image

新建Activity的时候,

1:eclipse会自动创建Layout,我们发现Layout目录下会多了对应的xml文件;

2:ec会自动在AndroidManifest.xml中创建对应的activity节点;

需要注意的是,这些都是ec帮我们自动创建的,我们完全可以手动创建 class,然后让它继承自activity,然后指定layout的那个xml,然后手动创建节点完成。

 

二:点击按钮进行页面跳转

在主页面创建一个button,对应

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginLeft="41dp"
        android:layout_marginTop="73dp"
        android:text="Button" 
        android:onClick="Btn1OnClick"/>

可以在xml中指定按钮事件方法,那么,我们在页面的后台代码中,可以直接实现此方法为:

public void Btn1OnClick(View view){    
    Intent intent = new Intent();
    intent.setClass(MainActivity.this, Test2Activity.class);
    startActivity(intent);
}   

方法中的3行代码就是用于页面跳转。

 

三:页面传值

有的时候,在跳转页面时还需要传递数据,这个时候如何做呢?
如果数据比较少,比如只要传一个名字,那么只要j加一句"intent.putExtra("Name", "feng88724");"即可,代码如下:


Intent intent = new Intent();  
intent.setClass(A.this, B.class);  
intent.putExtra("Name", "feng88724");  
startActivity(intent);  

如果数据比较多,就需要使用 Bundle类了,代码如下: (说明直接看注释)


Intent intent = new Intent(A.this, B.class);  
/* 通过Bundle对象存储需要传递的数据 */  
Bundle bundle = new Bundle();  
/*字符、字符串、布尔、字节数组、浮点数等等,都可以传*/  
bundle.putString("Name", "feng88724");  
bundle.putBoolean("Ismale", true);  
/*把bundle对象assign给Intent*/  
intent.putExtras(bundle);  
startActivity(intent); 

 

以上我们讲的都是如何进行页面跳转及数据传递,那么在另一个页面B上,应该如何接收数据呢?
在A页面上是以Bundle封装了对象,自然在B页面也是以Bundle的方式来解开封装的数据。主要通过"getIntent().getExtras()"方法来获取Bundle,然后再从Bundle中获取数据。 也可以通过" this.getIntent().getStringExtra("Name");"方法直接从Intent中获取数据。
从Bundle获取数据的代码:


@Override  
public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        /*加载页面*/  
        setContentView(R.layout.main);  
        /*获取Intent中的Bundle对象*/  
        Bundle bundle = this.getIntent().getExtras();  
        /*获取Bundle中的数据,注意类型和key*/  
        String name = bundle.getString("Name");  
        boolean ismale = bundle.getBoolean("Ismale");  


有时,在页面跳转之后,需要返回到之前的页面,同时要保留用户之前输入的信息,这个时候该怎么办呢?
在页面跳转后,前一个Activity已经被destroy了。如果要返回并显示数据,就必须将前一个Activity再次唤醒,同时调用某个方法来获取并显示数据。
要实现这个效果,需要做以下几步:
1. 首先,从A页面跳转到B页面时,不可以使用"startActivity()"方法,而要使用"startActivityForResult"方法。
2. 在A页面的Activity中,需要重写"onActivityResult"方法

 

@Override  
protected void onActivityResult(int requestCode,int resultCode,Intent data){  
    switch(requestCode){  
    case RESULT_OK:  
        /*取得来自B页面的数据,并显示到画面*/  
        Bundle bundle = data.getExtras();  
        /*获取Bundle中的数据,注意类型和key*/  
        String name = bundle.getString("Name");  
        boolean ismale = bundle.getBoolean("Ismale");  
    }  

 

3. 在B页面上加一个返回按钮,并在事件写如下代码:


/*给上一个Activity返回结果*/  
B.this.setResult(RESULT_OK,intent);  
/*结束本Activity*/  
B.this.finish(); 


本文转自最课程陆敏技博客园博客,原文链接:http://www.cnblogs.com/luminji/p/4545144.html,如需转载请自行联系原作者

原文链接:https://yq.aliyun.com/articles/349701
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章