您的位置:首页 > 移动开发 > Android开发

Android开发历程_1(从1个activity跳转到另一个activity)

2016-03-13 22:37 591 查看
    Android开发历程_1(从1个activity跳转到另一个activity)

今天第一次学习android开发,配置好了环境后就打算来个hello world程序,且实现从一个activity跳转到另一个activity中去。

   开发环境: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语句结束后并不需要结束符号,比如说分号。

  试验结果(在模拟器中运行的):

  启动程序后:

  


  按下Next按钮后:

  


实验主要部分代码及注释(附件有工程code下载):

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语法,所以未来的学习进度可能会稍慢,还好,我主要不是弄这方向的,只是实验室有这方面的项目,不得不弄一下。

  附:工程code下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: