Android之路——第一步:Activity之间切换(Intent、点击事件)
来源:互联网 发布:海洋最深处 知乎 编辑:程序博客网 时间:2024/06/07 02:27
我的博客:http://blog.csdn.net/name_cjf
前言
在上一篇博客中,大家应该清楚了如何显示一个Activity,那么,如果要显示两个Activity,并且可以相互切换,这该怎么做呢?下文介绍实现该目的的基本方法。由于上节课已经将eclipse创建Android项目以及编写简单的代码做了详细介绍,所以以后关于讲过的操作不在赘述。
Activity之间切换
首先,我们创建两个布局(Layout)文件,命名为activity_main与activity_second。
activity_main代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你好,世界1" /> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="切换到另一个activity"/></LinearLayout>
activity_second代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你好,世界1" /> <Button android:id="@+id/btn1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="切换到另一个activity"/></LinearLayout>
两个布局文件的作用是区分两个Activity,我们希望点击第一个Activity的按钮后跳转到第二个Activity,然后点击第二个Acticity的按钮后跳转至第一个Acticity。布局中Button节点为按钮控件,其中点击按钮后要处理切换Activity的事件,所以需要给它一个唯一标识,也就是id。id的格式为“@+id/id名”。
接下来我们写两个继承Activity的类(MainActivity、SecondActivity)来连接布局文件。
MainActivity.java代码如下:
package com.cjf.change;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.cjf.helloworld.R;import com.cjf.helloworld.R.id;public class MainActivity extends Activity { Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1=(Button) findViewById(id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent); } }); }}
SecondActivity代码如下:
package com.cjf.change;import com.cjf.helloworld.R;import com.cjf.helloworld.R.id;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SecondActivity extends Activity { Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn2=(Button) findViewById(id.btn2); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); }}
接下来,我们根据上述代码讲讲Android中的点击事件。在Android中,控件几乎全部具有可点击的特性,上述代码中为按钮添加了点击事件。系统首先需要知道点击的是哪一个按钮,而findViewById方法实现了控件的关联。关联后就可以设置点击事件了,点击事件有好几种实现方法,我在此写出了其中的一种。接下来,讲讲如何进行Acticity之间的切换。要实现切换的功能,我们需要一个对象:Intent。关于intent,可以自己将鼠标悬停至Intent上查看说明。我们只需要new一个新的Intent对象,并且调用setClass设置需要由哪跳转至哪,然后调用startActivity方法即可。在SecondActivity中有个finish方法,该方法的作用是结束Acticity。由于第二个Acticity结束,被压在其下的第一个Acticity便重新显示了出来。
接下来在AcdroidManifest中进行相关配置即可。
配置如下:
<activity android:name="com.cjf.change.MainActivity"> <intent-filter > <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name="com.cjf.change.SecondActivity"></activity>
接下来,运行程序,得到如下结果:
0 0
- Android之路——第一步:Activity之间切换(Intent、点击事件)
- Android之路——第二步:Activity之间传值(Intent、onActivityResult)
- android开发学习笔记(9)Intent之在两个Activity之间切换
- 每天一点Android开发------Intent(Activity之间切换)
- Android学习之路(三)之 显式Intent对于Activity之间的多种跳转方式
- 【Android成长之路】Intent初体验—使用Intent 在活动之间穿梭(一)
- Android--Activity之间的跳转(Intent)
- android之点击事件ImageView切换
- mars老师的第五讲——多个Activity之间通过intent来切换。
- intent(activity之间的切换) 详解
- Android学习笔记——两个Activity之间的切换
- android在Activity之间传数据之Intent
- Android开发之使用Intent在Activity之间传递数据
- Android学习笔记之Intent穿梭于各个Activity之间
- Android之Activity之间的数据传递-intent-bundle
- Activity之间的跳转—Intent
- Activity的切换(一)---Intent与Activity之间的交互
- Android之Activity(二):使用Intent在活动之间穿梭
- HTML5-Canvas画布的使用
- 专治linux不服——vbox 安装增强工具 建立共享目录
- hdoj GCD 2588 (欧拉函数)
- win7 64位下安装delphi xe3及破解
- 数论之欧拉函数
- Android之路——第一步:Activity之间切换(Intent、点击事件)
- SDWebImage中的- (void)sd_setImageWithURL:(NSURL *)url方法分析
- 传智播客 Java基础 day07笔记
- 查看Android Developer文档
- C#判断操作系统类型总结
- Android在处理图片减少出现OOM的方式
- Unity3D ref的用法 out的介绍
- OPENCV学习杂录
- join解析和遍历