广告
返回顶部
首页 > 资讯 > 移动开发 >android的activity跳转到另一个activity
  • 545
分享到

android的activity跳转到另一个activity

activityAndroid 2022-06-06 10:06:27 545人浏览 八月长安
摘要

开发环境: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语句结束后并不需要结束符号,比如说分号。
试验结果(在模拟器中运行的):
启动程序后:


实验主要部分代码及注释:
代码如下:
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);
 } 
    }
   
   
}
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

您可能感兴趣的文章:Android使用Activity实现从底部弹出菜单或窗口的方法Android中为activity创建菜单android游戏载入的activity跳转到游戏主菜单的activity具体实现Android使用xml文件资源定义菜单实现方法示例Android自定义实现侧滑菜单效果Android基础之Fragment与Activity交互详解android获取当前运行Activity名字的方法Android实现Activity界面切换添加动画特效的方法Android 多个Activity之间的传值Android实现菜单关联activity的方法示例


--结束END--

本文标题: android的activity跳转到另一个activity

本文链接: https://www.lsjlt.com/news/27290.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作