android findViewById

通過ID找到Layout的 VIEW控件。,比如你的控件Button ID為"@+id/button01"   就可以通過這樣Button btn=(Button)findViewById(R.id.button01")找到控件。。這時這個btn就是代表這個按鈕。。你可以理解成JS的getElementById()。

    Android中FindViewById()是一个非常常用的函数,位于android.app.Activity包中。该函数利用我们在XML文件中定义的View的id属性来获取相应的View对象。findViewById()属于API Level 1, 对应的android版本是android1.0, 由此,可以看出,该函数是android早期版本中就有的。顺便说一下, android目前市场上已商用的版本及其对应的API Level如下:

   android 1.0             API Level 1

   android 1.1             API Level 2

   android 1.5             API Level 3

   android 1.6             API Level 4

   android 2.0             API Level 5

   android 2.0.1          API Level 6

   android 2.1             API Level 7

   android 2.2             API Level 8

1、参数错误:findViewById的参数是一个View的ID,如果在XML文件中没定义相应的ID,则程序会

             善意的告诉你:XXX cannot be resulved。此时,补充定义就可以了。

2、未指定调用布局:findViewById()的调用与具体的布局有关,默认的是main.xml中的布局,函数前

                 没有布局指示。不过,当我们在main.xml描述的布局中,添加其它布局时,利用该函数获

                 取所添加布局中的View,则需在调用时,添加布局名,形式如下:

                 addLayout.findViewById(), 如果不这么做,程序编译时有时不会报错,但运行时会

                 提示遇到异常,并强制关闭应用。

3、命名冲突:这个错误可能不是很常见,不过,要是没有遇到过,猛的来这么一下,还真让人 有点懵。

                 呵呵,本人就犯过这样的错误。解释下,这里的命名冲突是指当前工程中定义的类与

                 android在Framework中提供的名字相同,这样的话,当前工程文件中,会优先使用本工

                 程中的定义。当然,使用findViewById()函数时,发生这种错误必须满足以下几个条件:

                 一是:需要在当前工程中利用ID(xml中定义)来查找对应的View对象;

                 二是:查找的View类名恰好与本工程中已有的类定义相同

                 三是:同名的两个类实例化后产生的对象类型不同,如:一个是View, 一个是Activity。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、实验名称:实验5 事件处理 二、实验日期: 三、实验目的: 基于监听的事件处理 基于回调的事件处理 四、实验用的仪器和材料:Windows+Eclipse+jdk+sdk+adt 五、实验的步骤和方法: 实验一:基于监听的事件处理机制 Activity.java package com.my; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Week05Activity extends Activity implements OnClickListener{ EditText txt; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取button对象 Button btn1 = (Button)findViewById(R.id.button1); Button btn2 = (Button)findViewById(R.id.button2); Button btn3 = (Button)findViewById(R.id.button3); Button btn4 = (Button)findViewById(R.id.button4); Button btn5 = (Button)findViewById(R.id.button5); //获取edittext对象 txt = (EditText)findViewById(R.id.edittext); //定义一个单击事件的监听器(内部类) class MyClickListener implements OnClickListener{ public void onClick(View v) { EditText txt = (EditText)findViewById(R.id.edittext); txt.setText("内部类"); } } //定义一个单击事件的监听器(外部类) class ExtentEvent implements OnClickListener{ private Activity act; private EditText txt; public ExtentEvent(Activity act,EditText txt){ this.act=act; this.txt=txt; } public void onClick(View v) { txt.setText("外部类"); } } //为按钮绑定事件监听(内部类) btn1.setOnClickListener(new MyClickListener()); //为按钮绑定事件监听(外部类) btn2.setOnClickListener(new ExtentEvent(this,txt)); //直接使用Activity作为事件监听器 btn4.setOnClickListener(this); //匿名内部类 btn3.setOnClickListener(new OnClickListener(){ public void onClick(View v) { txt.setText("匿名内部类"); } }); } //直接绑定到标签 public void clickHandler(View source){ EditText et = (EditText)findViewById(R.id.edittext); et.setText("绑定到标签"); } //Activity本身作为事件监听起器类,实现的事件处理方法 public void onClick(View v) { txt.setText("Activity"); } } Main.xml <?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:o

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值