Android学习第三天--事件监听器
生活随笔
收集整理的這篇文章主要介紹了
Android学习第三天--事件监听器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
事件監(jiān)聽(tīng)器方式一:
匿名內(nèi)部類,直接在組件上添加監(jiān)聽(tīng)器;例子如下
button.setOnclickListener(new OnClickListener() {System.out.println("hahahh"); });監(jiān)聽(tīng)方式二:
實(shí)現(xiàn)某個(gè)監(jiān)聽(tīng)器的接口,然后在添加該接口的實(shí)現(xiàn)類實(shí)現(xiàn)監(jiān)聽(tīng)機(jī)制;
public class MainActivity extends Activity implements OnClickListener {protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic void onClick(View arg0){Log.i("tag","hello");}}其中的Log.i("tag","hello")是用來(lái)解決程序錯(cuò)誤的時(shí)候用的,用法相當(dāng)于System.out.println();
在android中使用最廣泛的就是它了。前面是標(biāo)簽,在程序發(fā)生錯(cuò)誤的時(shí)候,通過(guò)過(guò)濾器就可以直接找到我們?cè)诔绦虍?dāng)中設(shè)置的標(biāo)簽,然后選擇我們想要的信息,查看是否是異常之類的問(wèn)題。
?
事件監(jiān)聽(tīng)的第三種
在xml中添加
android:onClick="click" <Button android:id="@+id/ss"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="測(cè)試"android:onClick="click"/>在主java文件中
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); }public void click(View w){Log.i("a", "hello");}這樣就可以實(shí)現(xiàn)事件監(jiān)聽(tīng)的第三種方式
轉(zhuǎn)載于:https://www.cnblogs.com/will-peng/archive/2013/03/09/2951730.html
總結(jié)
以上是生活随笔為你收集整理的Android学习第三天--事件监听器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IOS项目中加入Google Admob
- 下一篇: 安装设置Android Studio W