欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android的activity跳转到另一个activity

程序员文章站 2022-10-09 13:56:29
开发环境:android4.1.1实验功能:在第一个hello world!为标签的activity中显示good,该界面中有一个名为next的按钮。点击next按钮进入到...

开发环境:android4.1.1

实验功能:
在第一个hello world!为标签的activity中显示good,该界面中有一个名为next的按钮。点击next按钮进入到第二个activity中去,第二个界面中只有1个close按钮。当然,据网上有人将要比较安全的实现关闭程序的功能也不是挺简单的,因为android有专门的退出键返回键等。所以该close按钮暂时没去实现它。
我的第1个activity为helloworldactivity,第2个activity为nextactivity.

实验说明:
1. 要实现从1个activity跳到另一个activity,这需要通过intent来实现。当然我们需要在next按钮上绑定一个按钮按下的监听器(这些好像是java中的知识,可我从没学过java,只能用到哪个地方再去学了),一旦该按钮监听到有按键按下,则通过intent将指定的第2个activity触发,这样就完成了本次试验的功能。
2.在工程中,每一个activity都对应一个xml文件,xml文件主要是控制各控件的位置和属性的.
3. asserts目录下可以存放任何文件,res目录下也可以存放任意文件,且res下的文件会在gen目录下的r.java文件中自动生成一个全局id。
4. res目录下的values目下的strings.xml中的控件也是每个控件都在r.jar中对应一个id号。当然layout下的main.xml文件也是一样的。
5. androidmanifest.xml是整个应用程序的配置文件。
6. android.jar是该程序应用的所有android类的来源。
7. view是android中所有控件的父类。
8. activity可以理解为人机交互的界面,也可以理解为一个控件的容器。
9. eclipse中用crtl+shift+c注释选中区域,同时也是用ctrl+shift+c取消选中区域,这里的注释为双斜杆//.
  如果用/**/来注释的话,就是用ctrl+shift+/来注释选中区域,用ctrl+shift+\来取消选中区域的注释。
10. 用alt+/是增加单词函数等补全功能的提示。
11. ctrl+shift+o可以自动添加eclipse中检测到需要导入的包文件。
12. settext里面不能采用资源引用,资源引用显示文本应该是在xml中的。
13. xml的注释不能出现在属性值代码中,不能出现在标记中。且注释格式为<!--注释内容-->
14. xml语句结束后并不需要结束符号,比如说分号。

试验结果(在模拟器中运行的):
启动程序后:

android的activity跳转到另一个activity


实验主要部分代码及注释:

复制代码 代码如下:

helloworldactivity.java:
package com.example.helloworld;
import android.app.activity;
import android.content.intent;
import android.view.view;//注意view的大小写
import android.view.view.onclicklistener;
import android.os.bundle;
import android.widget.button;
public class helloworldactivity extends activity {
    private button my_button = null;
    @override
    public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_helloworld);
 my_button = (button)findviewbyid(r.id.my_button);
 my_button.settext( "next" );   
 my_button.setonclicklistener(new mybuttonlistener());
    }
    class mybuttonlistener implements onclicklistener{
 public void onclick(view v) {
     // todo auto-generated method stub
     intent intent = new intent();
     intent.setclass(helloworldactivity.this, nextactivity.class);
     helloworldactivity.this.startactivity(intent);
 } 
    }

    /**
     * 如果下面的语句不要,那么系统运行的时候会直接进入本程序中,而不是先进入主菜单
     * 再进入选择应用程序界面进入本程序
     * 为了方便调试,这里就不进入主菜单界面了*/
    /*@override
    public boolean oncreateoptionsmenu(menu menu) {
 getmenuinflater().inflate(r.menu.activity_helloworld, menu);
 return true;
    }*/
}

nextactivity.java:
package com.example.helloworld;
import android.app.activity;
import android.os.bundle;
import android.widget.button;
public class nextactivity extends activity{
    private button my_button2 = null;
    @override
    protected void oncreate(bundle savedinstancestate) {
 // todo auto-generated method stub
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_next);
 my_button2 = (button)findviewbyid(r.id.my_button2);
    //    my_button2.settext("@string/close");    //settext里面不能采用资源引用
      //资源引用显示文本应该是在xml中的
 my_button2.settext("close");
    }
}


activity_helloworld.xml:
复制代码 代码如下:

<!-- android:text="@string/wuwei" -->
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <textview
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignparentleft="true"
 android:layout_alignparentright="false"
 android:layout_alignparenttop="true"
 android:layout_centerhorizontal="true"
 android:text="@string/wuwei"
 tools:context=".helloworldactivity" />

    <button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"    
 android:id="@+id/my_button"  
 android:layout_centerhorizontal="true"
 android:layout_centervertical="true"
  />
</relativelayout>

activity_next.xml:
<?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" >

    <button
 android:id="@+id/my_button2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
    />
    </linearlayout>

androidmanifest.xml:

复制代码 代码如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    android:versioncode="1"
    android:versionname="1.0" >
    <uses-sdk
 android:minsdkversion="16"
 android:targetsdkversion="15" />
    <application
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/apptheme" >
 <activity
     android:name=".helloworldactivity"
     android:label="@string/hello_world" >
     <intent-filter>
  <action android:name="android.intent.action.main" />
  <category android:name="android.intent.category.launcher" />
     </intent-filter>
 </activity>
 <activity android:name=".nextactivity"  android:label="@string/close">
 </activity>
    </application>
</manifest>

实验总结:
对android开发工程下的几个目录的主要功能和任务有了个大致的了解,对android的开发流程大概熟悉了一遍,由于不懂java和xml语法,所以未来的学习进度可能会稍慢,还好,我主要不是弄这方向的,只是实验室有这方面的项目,不得不弄一下。
作者:tornadomeet