activity之简单的页面跳转
1,在activity_main.xml中添加一个按钮
内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/container_1" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = "com.example.activity_tiaozhuan.MainActivity" tools:ignore = "MergeRootFrame" > < Button android:id = "@+id/bt_activity" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentRight = "true" android:layout_alignParentTop = "true" android:text = "跳转" /> </ RelativeLayout > |
2,新建一个跳转的目标页面的xml文件:activity_second.xml,并且添加一个TextView
内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/container_2" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = "com.example.activity_tiaozhuan.MainActivity" tools:ignore = "MergeRootFrame" > < TextView android:id = "@+id/tv_activity" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_alignParentLeft = "true" android:layout_alignParentTop = "true" android:text = "第二个activity" android:textColor = "#ff0000" /> </ RelativeLayout > |
3,新建一个SecondActivity.java,把MainActivity里的内容复制过去,改下这行内容:
setContentView(R.layout.activity_second);
内容:
1 2 3 4 5 6 7 8 9 10 | package com.example.activity_tiaozhuan; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; public class SecondActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_second); } } |
4,在MainActivity里实现页面跳转
内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.example.activity_tiaozhuan; //包名视具体情况自行改动 import android.support.v7.app.ActionBarActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends ActionBarActivity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt_activity); //查找到按钮bt_activity bt.setOnClickListener( this ); //给按钮注册点击事件 } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_activity: method(); //按钮是R.id.bt_activity时,调用method()方法来实现点击的具体意图 break ; } } private void method() { Intent intent = new Intent( this ,SecondActivity. class ); //定义一个跳转到SecondActivity的意图 startActivity(intent); //启动意图intent } } |
5,在AndroidMainfest.xml中注册SecondActivity,添加:
<activity
android:name="com.example.activity_tiaozhuan.SecondActivity" ></activity>
内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.activity_tiaozhuan" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "8" android:targetSdkVersion = "19" /> < application android:allowBackup = "true" android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" android:theme = "@style/AppTheme" > < activity android:name = "com.example.activity_tiaozhuan.MainActivity" android:label = "@string/app_name" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > < activity android:name = "com.example.activity_tiaozhuan.SecondActivity" ></ activity > </ application > </ manifest > |

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
当setWidth()和setHeight()方法不起作用时
当在Android开发中用方法setWidth()和setHeight()动态设置控件的宽高时,当被改后的宽高小雨原来的宽高时,这两个方法将不会生效。 解决办法: 1 2 3 4 LayoutParamslp=(LayoutParams)imageView.getLayoutParams(); lp.height= 200 ; lp.width= 100 ; imageView.setLayoutParams(lp); 本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1552598,如需转载请自行联系原作者
- 下一篇
"activity_main cannot be resolved or is not a field"的解决办法
出现这种报错的原因可能是在添加资源文件时,系统自动添加了“import android.R;”,而android.R是系统提供的资源,因此该程序的资源就无法检索到,导致报错。 解决办法:删掉:import android.R; 本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1552479,如需转载请自行联系原作者
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS关闭SELinux安全模块
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- CentOS6,CentOS7官方镜像安装Oracle11G
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Hadoop3单机部署,实现最简伪集群