找到资源加入监听

话说做事能让你充实,但我怎么感觉自己像一只苍蝇到处乱穿,到最后没多大收获呢。也许现在不是扩充知识面的时候,而是应该静下心来深入的学习一项东西,所谓术业有专攻,我想就是这个道理。泛而不精,貌似不是很好。

Android说白了就是Java的变种,只是加入了许多类库的支持而已,没什么大不了的,貌似在这吹牛,万一被那个大牛看见,那岂不是。。。。。。

Java的AWT是通过代码敲出来的,Android还可以通过R类找到相应的资源(XML),我对布局向来不行,从前非常专注与美工,甚至学习了ps,flash等等各种,后来发现我还是适合敲代码。

下面的例子是点击一个Button后,将Button的内容赋给TextView,效果图

看图可知,main.xml中肯定有一个TextView和一个Button

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView     
  8.     android:id="@+id/tv"  
  9.     android:layout_width="fill_parent"    
  10.     android:layout_height="wrap_content"    
  11.     android:text="@string/hello"  
  12.     />  
  13. <Button  
  14.     android:id="@+id/button"  
  15.     android:layout_width="wrap_content"  
  16.     android:layout_height="wrap_content"  
  17.     android:text="forward another activity"  
  18.     />  
  19. </LinearLayout>  

@+id/button这个东西貌似没见过,其实是一个唯一标识,理解万岁!它会在R类中生成一个static id类方法里面有一个static 属性button,这样我们在HelloAdnroid.java中就可以同过R.id.button找到这个button。R中的内容好像在以前版本中不可以修改,但自从某某版本之后就可以了,我还没碰到要自己去改,因为它会自动为我们添加,比如上面的。还有为什么要这样写,我只能告诉你规定!我的一个C++群中,以高手如是解释规定。“我的钱放在右边的口袋里。我很明确的说了我的钱的位置,以后要拿钱的话,就不必问我钱放在那个口袋里了,这样省了很多麻烦”

Code:
  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY.  
  2.  *  
  3.  * This class was automatically generated by the  
  4.  * aapt tool from the resource data it found.  It  
  5.  * should not be modified by hand.  
  6.  */  
  7.   
  8. package org.kevin.android;   
  9.   
  10. public final class R {   
  11.     public static final class attr {   
  12.     }   
  13.     public static final class drawable {   
  14.         public static final int icon=0x7f020000;   
  15.     }   
  16.     public static final class id {   
  17.         public static final int button=0x7f050001;   
  18.         public static final int tv=0x7f050000;   
  19.     }   
  20.     public static final class layout {   
  21.         public static final int main=0x7f030000;   
  22.     }   
  23.     public static final class string {   
  24.         public static final int app_name=0x7f040001;   
  25.         public static final int hello=0x7f040000;   
  26.     }   
  27. }   

 

我们点击button之后会有改变,说明这个button肯定有一个监听事件,看看HelloAndroid.java 是怎么实现的

Code:
  1. package org.kevin.android;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.View;   
  6. import android.view.View.OnClickListener;   
  7. import android.widget.Button;   
  8. import android.widget.TextView;   
  9. //HelloAndroid.java   
  10. public class HelloAndroid extends Activity {   
  11.     /** Called when the activity is first created. */  
  12.     Button button;   
  13.     TextView tv;   
  14.     OnClickListener listener=null;   
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {   
  17.         super.onCreate(savedInstanceState);   
  18.       //listener   
  19.         listener=new OnClickListener(){   
  20.             //impl the function onClick   
  21.             public void onClick(View v){   
  22.                 //get the button-text content   
  23.                 String s=button.getText().toString();   
  24.                 //set the tv cintent   
  25.                 tv.setText(s);   
  26.             }   
  27.         };   
  28.         setContentView(R.layout.main);   
  29.         //find the res from main.xml   
  30.         button=(Button)findViewById(R.id.button);   
  31.         tv=(TextView)findViewById(R.id.tv);   
  32.         //set a listener for the button   
  33.         button.setOnClickListener(listener);   
  34.            
  35.     }   
  36. }  

值得注意的是,监听事件要写在前面,刚才试了一下写在后面貌似不成功,也不知道是不是机器的问题,按理说写在哪不一样,求解释。懂得告诉小弟一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值