欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 实现监听的四种方法详解实例代码

程序员文章站 2024-03-31 18:41:34
直接上代码,大家可以参考下 (1)自身类作为事件监听器 package cn.edu.gdmec.s07150745.work5; import andr...

直接上代码,大家可以参考下

(1)自身类作为事件监听器

package cn.edu.gdmec.s07150745.work5;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.toast;

public class mainactivity extends appcompatactivity implements view.onclicklistener{

@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
button btn1 = (button) findviewbyid(r.id.btn1);
btn1.settext("单击");
btn1.setonclicklistener(this);
}

@override
public void onclick(view v) {
toast.maketext(this,"已单击我...",toast.length_short).show();
system.out.println("已单击我...");
}
}

 

(2)外部类作为事件监听器:

package cn.edu.gdmec.s07150745.work5;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;

public class act2 extends appcompatactivity {

@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_act2);
button btn2 = (button) findviewbyid(r.id.btn2);
btn2.settext("单击");
btn2.setonclicklistener(new outerclass("已单击我..."));
}
}
class outerclass implements view.onclicklistener{
private string str="已单击我....";
public outerclass(string str){
super();
this.str=str;
}
@override
public void onclick(view v) {
system.out.println(str);
}
}

(3)内部类作为事件监听器:

package cn.edu.gdmec.s07150745.work5;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.toast;

public class act3 extends appcompatactivity {

@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_act3);
button btn3 = (button) findviewbyid(r.id.btn3);
btn3.settext("单击");
btn3.setonclicklistener(new outerclass());
}
class outerclass implements view.onclicklistener{

@override
public void onclick(view v) {
toast.maketext(act3.this,"已单击我...",toast.length_short).show();
system.out.println("已单击我...");
}
}
}

 (4)匿名类作为事件监听器:

package cn.edu.gdmec.s07150745.work5;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.toast;

public class act4 extends appcompatactivity {

@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_act4);
button btn4 = (button) findviewbyid(r.id.btn4);
btn4.settext("单击");
btn4.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
toast.maketext(act4.this,"已单击我...",toast.length_short).show();
system.out.println("已单击我...");
}
});
}
}

 

以上四种方法之后,在xml文件中给每一个事件即activity里面定义多一个android label即可。不忘在每个activity里面添加如下蓝色的一段代码哦。

<activity android:name=".mainactivity" android:label="1">
<intent-filter>
<action android:name="android.intent.action.main" />

<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!