话说做事能让你充实,但我怎么感觉自己像一只苍蝇到处乱穿,到最后没多大收获呢。也许现在不是扩充知识面的时候,而是应该静下心来深入的学习一项东西,所谓术业有专攻,我想就是这个道理。泛而不精,貌似不是很好。
Android说白了就是Java的变种,只是加入了许多类库的支持而已,没什么大不了的,貌似在这吹牛,万一被那个大牛看见,那岂不是。。。。。。
Java的AWT是通过代码敲出来的,Android还可以通过R类找到相应的资源(XML),我对布局向来不行,从前非常专注与美工,甚至学习了ps,flash等等各种,后来发现我还是适合敲代码。
下面的例子是点击一个Button后,将Button的内容赋给TextView,效果图
看图可知,main.xml中肯定有一个TextView和一个Button
- <?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:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="forward another activity"
- />
- </LinearLayout>
@+id/button这个东西貌似没见过,其实是一个唯一标识,理解万岁!它会在R类中生成一个static id类方法里面有一个static 属性button,这样我们在HelloAdnroid.java中就可以同过R.id.button找到这个button。R中的内容好像在以前版本中不可以修改,但自从某某版本之后就可以了,我还没碰到要自己去改,因为它会自动为我们添加,比如上面的。还有为什么要这样写,我只能告诉你规定!我的一个C++群中,以高手如是解释规定。“我的钱放在右边的口袋里。我很明确的说了我的钱的位置,以后要拿钱的话,就不必问我钱放在那个口袋里了,这样省了很多麻烦”
- /* AUTO-GENERATED FILE. DO NOT MODIFY.
- *
- * This class was automatically generated by the
- * aapt tool from the resource data it found. It
- * should not be modified by hand.
- */
- package org.kevin.android;
- public final class R {
- public static final class attr {
- }
- public static final class drawable {
- public static final int icon=0x7f020000;
- }
- public static final class id {
- public static final int button=0x7f050001;
- public static final int tv=0x7f050000;
- }
- public static final class layout {
- public static final int main=0x7f030000;
- }
- public static final class string {
- public static final int app_name=0x7f040001;
- public static final int hello=0x7f040000;
- }
- }
我们点击button之后会有改变,说明这个button肯定有一个监听事件,看看HelloAndroid.java 是怎么实现的
- package org.kevin.android;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- //HelloAndroid.java
- public class HelloAndroid extends Activity {
- /** Called when the activity is first created. */
- Button button;
- TextView tv;
- OnClickListener listener=null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //listener
- listener=new OnClickListener(){
- //impl the function onClick
- public void onClick(View v){
- //get the button-text content
- String s=button.getText().toString();
- //set the tv cintent
- tv.setText(s);
- }
- };
- setContentView(R.layout.main);
- //find the res from main.xml
- button=(Button)findViewById(R.id.button);
- tv=(TextView)findViewById(R.id.tv);
- //set a listener for the button
- button.setOnClickListener(listener);
- }
- }
值得注意的是,监听事件要写在前面,刚才试了一下写在后面貌似不成功,也不知道是不是机器的问题,按理说写在哪不一样,求解释。懂得告诉小弟一下