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
原创粉丝点击