?
?今天是礼拜一,第一天学android。昨天放假的时候在家整开发环境,整了几个小时。今天去学校,轻松了许多。不过,还是有些小小的心得和体会。好吧,既然叫了android日记,那就开始吧。
从开始搭建android的开发环境开始吧,其实搞清楚了很简单,没有那么的复杂。
所需文件是:android-sdk-2.3
eclipse-3.5-正式版
jdk-1.6-win32
?
装好sdk,进入eclipse里面,点击help,install new software安装android插件。
然后就可以新建android工程了。
?
src-------用户的源代码
?
?
gen------插件自动维护的,用于存放资源索引的。
?
assets--------插件自动生成,用户把程序打包成apk的文件,并交个android虚拟机安装运行。
?
res-------包含需要的字符串文件和前台布局文件----------本文件夹一定要注意,里面的文件名称绝对不能用大写,如果用了大写就会报莫名奇妙的错误。
?
/HelloAndroid/AndroidManifest.xml --------------- 配置程序入口的xml文件
?
ok,明白了android的文件夹类型及意义,
我就写了个android程序吧。。。。。。。。。
?
Hello_World
?
import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class Hello_World extends Activity { /** Called when the activity is first created. */ private EditText usernameText; private Button loginBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//设定main.xml为本activity类的前台类,暂且这样称呼他.. usernameText=(EditText)findViewById(R.id.username_field);//根据id拿到EditText对象 loginBtn=(Button)findViewById(R.id.sureBtn);//根据id拿到Button对象 loginBtn.setOnClickListener(new View.OnClickListener() {//监听 @Override public void onClick(View v) {//方法 String username=usernameText.getText().toString().trim(); Intent it=new Intent(Hello_World.this,MyActivity.class);//intent对象使用来实现两个类之间通信的 it.putExtra("loginname", username);//传递username参数给MyActivity类 startActivity(it);//启动本intent对象 } }); }}
package com.tan.hello;
import android.app.Activity;import android.os.Bundle;import android.widget.TextView;
public class MyActivity extends Activity {
[email protected]?protected void onCreate(Bundle savedInstanceState) {??// TODO Auto-generated method stub??super.onCreate(savedInstanceState);??setContentView(R.layout.my_activity);//以my_activity.xml为本类的前台类??String loginname=this.getIntent().getStringExtra("loginname");//拿到传过来的username的值??TextView showname=(TextView)findViewById(R.id.showView);//将其显示??showname.setText(loginname);?}}
main.xml内容
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:orientation="vertical"??? android:layout_width="fill_parent"??? android:layout_height="fill_parent"??? ><TextView? ??? android:layout_width="fill_parent" ??? android:layout_height="wrap_content" ??? android:text="@string/username"//在string.xml中拿到username的值,以下同理??? />?? <EditText?? android:layout_width="fill_parent"?? android:layout_height="wrap_content"?? android:id="@+id/username_field"?? />?? <TextView? ??? android:layout_width="fill_parent" ??? android:layout_height="wrap_content" ??? android:text="@string/password"??? />??? ???? <EditText?? android:layout_width="fill_parent"?? android:layout_height="wrap_content"?? android:id="@+id/password_field"?? android:password="true"?? />?? <Button?? android:id="@+id/sureBtn"?? android:layout_width="fill_parent"?? android:layout_height="wrap_content"?? android:text="@string/sure"?? />????? <Button?? android:id="@+id/concelBtn"?? android:layout_width="fill_parent"?? android:layout_height="wrap_content"?? android:text="@string/concel"?? /></LinearLayout>
my_activity.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="wrap_content">? <TextView? android:id="@+id/showView"? android:layout_width="fill_parent"? android:layout_height="wrap_content"? /></LinearLayout>
String.xml内容
<?xml version="1.0" encoding="utf-8"?><resources>??? <string name="username">用户名:</string>??? <string name="app_name">腾讯QQ</string>??? <string name="password">密码:</string>??? <string name="sure">确定</string>??? <string name="concel">取消</string></resources>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"????? package="com.tan.hello"????? android:versionCode="1"????? android:versionName="1.0">
/HelloAndroid/AndroidManifest.xml内容
??? <application android:icon="@drawable/icon" android:label="@string/app_name">??????? <activity android:name=".Hello_World"????????????????? android:label="@string/app_name">//程序的名称??????????? <intent-filter>//在activity配置中出现这个表示程序从该类启动??????????????? <action android:name="android.intent.action.MAIN" />??????????????? <category android:name="android.intent.category.LAUNCHER" />??????????? </intent-filter>??????? </activity>??????? <activity android:name=".MyActivity"? android:label="@string/app_name">??????? </activity>
??? </application></manifest>
?
?
?
?
?