<第一行代码>学习笔记1.09

一、四大组件

Activity,Service,Broadcast Receiver,Content Provider.

二、搭建开发环境

三、自动创建android项目

1.创建项目 

file---new---android application project

application name:Hello World

project name:HelloWorld

package name:com.test.helloworld

2.创建活动

勾选create activity会创建模板activity,不勾选则自己设计。

activity name:HelloWorldActivity

layout name:hello_world_layout

3.运行HelloWorld项目

确认模拟器在线后,点击run as---android application

4.分析代码

(1)activity(由模板创建,模板为blank activity)

第5行是调用布局,第10行是调用菜单。

 1 public class HelloWorldActivity extends Activity {
 2    @Override
 3    protected void onCreate(Bundle savedInstanceState) {
 4        super.onCreate(savedInstanceState);
 5        setContentView(R.layout.hello_world_layout);
 6    }
7 @Override 8 public boolean onCreateOptionsMenu(Menu menu) { 9 // Inflate the menu; this adds items to the action bar if it is present. 10 getMenuInflater().inflate(R.menu.hello_world, menu); 11 return true; 12 } 13 }

(2)layout

打开res/layout目录下的hello_world_layout.xml 。

TestView是android提供的一个控件,用于在布局显示文字。

控件的宽和高都刚好涵盖字--"wrap_content"。控件的text为定义的一个string常量hello_world,但是hello_world的内容尚不知。

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".HelloWorldActivity" >
10     <TextView
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="@string/hello_world" />
14 </RelativeLayout>

(3)字符串的定义处

打开res/values目录下的strings.xml。

在这里定义了三个字符串常量,layout中调用的是第三个。

1 <resources>
2     <string name="app_name">Hello World</string>
3     <string name="action_settings">Settings</string>
4     <string name="hello_world">Hello world!</string>
5 </resources>

(4)在Androidmanifest.xml中注册activity

intent-filter中的两行代码表示这是主活动,点击应用图标运行的程序。

1 <activity
2     android:name="com.test.helloworld.HelloWorldActivity"
3     android:label="@string/app_name" >
4     <intent-filter>
5         <action android:name="android.intent.action.MAIN" />
6         <category android:name="android.intent.category.LAUNCHER" />
7     </intent-filter>
8 </activity>

(5)如何引用res文件夹中的资源。

在代码中通过 R.string.hello_world 可以获得该字符串的引用;
在 XML 中通过@string/hello_world 可以获得该字符串的引用。

string可以换成其他的,比如drawable,则引用的是图片资源。

四、手动创建android项目

1.创建项目

file---new---Android project 

project name:ActivityTest

src/file---new---package

package name:com.example.activitytest

2.创建活动

package/file---new---class---FirstActivity

勾选create activity会创建模板activity,不勾选则自己设计。

3.添加代码

(1)acitivity

1 public class FirstActivity extends Activity {
2     @Override
3     protected void onCreate(Bundle savedInstanceState) {
4         super.onCreate(savedInstanceState);
5         setContentView(R.layout.first_layout);
6     }
7 }

(2)layout

res/layout---new---android xml file---first_layout.xml

自己添加了一个button。并且为这个button定义了一个id。

xml文件中定义id的格式:@+id/id_name

xml文件中引用id的格式:@id/id_name

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical" >
 5     <Button
 6         android:id="@+id/button_1"
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:text="Button 1"
10     />
11 </LinearLayout>

(3)字符串的定义

res/values---new---android xml file---strings.xml

在这里定义了三个字符串常量,layout中调用的是第三个。

1  <resources>
2      <string name="app_name">Hello World</string>
3      <string name="action_settings">Settings</string>
4      <string name="button_1">Button 1</string>
5  </resources>

(4)注册活动

 1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 2     package="com.example.activitytest"
 3     android:versionCode="1"
 4     android:versionName="1.0" >
 5     <uses-sdk
 6         android:minSdkVersion="14"
 7         android:targetSdkVersion="19" />
 8    <application
 9         android:allowBackup="true"
10         android:icon="@drawable/ic_launcher"
11         android:label="@string/app_name"
12         android:theme="@style/AppTheme" >
13         <activity
14             android:name=".FirstActivity"
15             android:label="This is FirstActivity" >
16             <intent-filter>
17                 <action android:name="android.intent.action.MAIN" />
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21    </application>
22 </manifest>

4.运行HelloWorld项目

确认模拟器在线后,点击run as---android application

五、隐藏标题栏

只需要添加一句话,但是必须加在setContentView之前。

1 protected void onCreate(Bundle savedInstanceState) {
2     super.onCreate(savedInstanceState);
3     requestWindowFeature(Window.FEATURE_NO_TITLE);
4     setContentView(R.layout.first_layout);
5 }

六、使用Toast

toast的弹出必须要有触发点,干脆点击Button 1的时候触发toast。

(1)为button添加监听器,响应click操作。

首先找到为谁添加这个监听器。然后添加响应操作。

 1 protected void onCreate(Bundle savedInstanceState) {
 2     super.onCreate(savedInstanceState);
 3     requestWindowFeature(Window.FEATURE_NO_TITLE);
 4     setContentView(R.layout.first_layout);
 5     Button button1 = (Button) findViewById(R.id.button_1);
 6     
 7     button1.setOnClickListener(new OnClickListener() {
 8         @Override
 9         public void onClick(View v) {
10              Toast.makeText(FirstActivity.this, "You clicked Button 1",
11         Toast.LENGTH_SHORT).show();
12         }
13     });
14 }

(2)具体的响应操作

1 @Override
2 public void onClick(View v) {
3     Toast.makeText(FirstActivity.this, "You clicked Button 1",
4     Toast.LENGTH_SHORT).show();
5 }

七、使用菜单

(1)创建菜单

res---new---folder---menu

res/menu---new---android xml file---main.xml

(2)添加菜单项的代码

添加了两个菜单项,并定义了它们的id。item标签就是用来创建菜单项的。

1 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
2     <item
3         android:id="@+id/add_item"
4         android:title="Add"/>
5     <item
6         android:id="@+id/remove_item"
7         android:title="Remove"/>
8 </menu>

(3)字符串定义

(4)在活动中添加菜单,并且定义菜单响应事件

都是在activity中写的。

通过getItemId知道点击的是哪个菜单项,然后做出对应反应。

1 public boolean onCreateOptionsMenu(Menu menu) {
2     getMenuInflater().inflate(R.menu.main, menu);
3     return true;
4 }
 1 public boolean onOptionsItemSelected(MenuItem item) {
 2     switch (item.getItemId()) {
 3     case R.id.add_item:
 4         Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
 5        break;
 6     case R.id.remove_item:
 7         Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
 8         break;
 9     default:
10     }
11     return true;
12 }

八、logCat的使用

(1)添加logCat到eclipse中

window---show view---other---android---logcat---ok

(2)log的等级

Log.v(tag,message)  Log.d()   Log.i()   Log.w()   Log.e()

如果没有打印出来,进入到DDMS视图,点击当前设备。

(3)添加过滤器

如果添加过滤器时将by log tag设为data,那么只会打印tag为data的错误日志。

如果在日志级别控制中选中某等级,那么此等级以下的log就不会被打印。

 

1.9日学习总结:

开发环境的搭建。遇到了问题但是解决了。

如何创建一个project,activity---layout---menu--button、toast、item---注册

注意id很重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!每一个layout的元素都要有自己的id。

错误日志的用法。

posted @ 2015-01-10 20:08  behappylee  阅读(461)  评论(0编辑  收藏  举报