第一个安卓程序的解说
凡是程序员第一个安卓程序永远是Helloworld。
接下的步骤不具体说
建好一个JAVA项目:
运行:
Success 出现说明运行成功
这里的文件夹很多:
(1)src 文件夹
是你写的程序要实现的功能的JAVA文件。你所有的JAVA代码都放到这个文件里。
(2)gen文件夹:
这里必须要说一下 R文件是系统自动生成的文件,是存放所有的资源ID的地方(自动生成)不需要程序员手动维护的文件。程序员也不能这样做。
(3)
res是存放图片资源的文件,也级是说项目里所有的图片都会存放到这个文件夹里。当你放一张图片到这个文件夹下 就会在R文件里生成一个图片的ID.比如我的图片名为qian.png 在R文件里:
当我们使用这张图片的时候 就会在R文件里调用这张图片的ID 也就是 图片名字。
(4)
在AndroidManifest.xml文件里是实现我们的应用程序界面的的文件。代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="csdn.hr"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloWordsActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
android:icon="@drawable/ic_launcher" 中的@符号 是引用的意思 就是说 引用drawable文件夹下的ic_launcher这张图片如果我们把刚才插入的图片放到我们的应用程序里则把android:icon="@drawable/ic_launcher"改为android:icon="@drawable/qian"就行了
运行一下:
O了!
(5)修改图标下的文字:
把HelloWorlds改为我的第一个应用程序。这就需要定义的一个新的字符串 如下图:
在values文件夹下的String.xml文件就是记录项目资源字符的文件<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloWordsActivity!</string>
<string name="app_name">HelloWords</string>
</resources>
在xml文件里添加字符串:
<string name="name">我的第一个应用程序</string>
同时要在HelloworldsMianfest.xml文件引用此字符串。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="csdn.hr"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<-- 代表手机的一个活动,手机一屏-->
<application
android:icon="@drawable/qian"
android:label="@string/name" >
<activity
android:name=".HelloWordsActivity"
android:label="@string/name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如下图:
(6)
接下来 需要添加一个Button按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qq" />
</LinearLayout>
这样我们这个下程序的 界面就做好了:
(7)现在为这个按钮注册监听器:
打开这个文件 开始写JAVA代码:
package csdn.hr;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class HelloWordsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);设置该现实的视图
Button bnt=(Button) findViewById(R.id.button1);通过R文件的ID找到这个视图的按钮 因为Viwe类 是Button的父类 所以要用到父类的方法 需要一个造型也就是(Button);
bnt.setOnClickListener(new OnClickListener() {注册监听器 实现吐司方法。
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText( HelloWordsActivity.this//这个类的对象, "今天真热"//要显示的文字,Toast.LENGTH_SHORT//显示的时间).show();
}
});
}
}
效果图如下: