Android OnFocuChangeListener焦点事件详解
程序员文章站
2023-12-16 15:38:34
本文实例为大家分享了android onfocuchangelistener焦点事件的具体代码,供大家参考,具体内容如下
界面
打开“res/layout/acti...
本文实例为大家分享了android onfocuchangelistener焦点事件的具体代码,供大家参考,具体内容如下
界面
打开“res/layout/activity_main.xml”文件。
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <edittext android:id="@+id/mobile" android:layout_width="190dp" android:layout_height="wrap_content" android:text="手机号码" /> <edittext android:id="@+id/address" android:layout_width="190dp" android:layout_height="wrap_content" android:text="地址" /> </linearlayout>
mainactivity.java
package com.example.whaletosea.application04; import android.os.bundle; import android.app.activity; import android.view.view; import android.widget.edittext; import android.view.view.onclicklistener; import android.view.view.onfocuschangelistener; import android.widget.toast; public class mainactivity extends activity { //声明 edittext private edittext etmobile=null; private edittext etaddress=null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //获取edittext etmobile = (edittext) super.findviewbyid(r.id.mobile); etaddress = (edittext) super.findviewbyid(r.id.address); //注册onclick onfocuschange监听器 etmobile.setonclicklistener(new mobileonclicklistener()); etmobile.setonfocuschangelistener(new mobileonfocuschanagelistener()); etaddress.setonclicklistener(new addressonclicklistener()); etaddress.setonfocuschangelistener(new addressonfocuschanagelistener()); } //mobileonclicklistener单击监听器 private class mobileonclicklistener implements onclicklistener{ @override public void onclick(view view ){ etmobile.settext(""); } } //mobileonfocuschanagelistener焦点监听器 private class mobileonfocuschanagelistener implements onfocuschangelistener{ @override public void onfocuschange(view view ,boolean hasfocus){ if(view.getid()==etmobile.getid()) toast.maketext(getapplicationcontext(),"手机文本框获得焦点!",toast.length_long).show(); } } //addressonclicklistener单击监听器 private class addressonclicklistener implements onclicklistener{ @override public void onclick(view view){ etaddress.settext(""); } } //mobileonfocuschanagelistener焦点监听器 private class addressonfocuschanagelistener implements onfocuschangelistener{ @override public void onfocuschange(view view,boolean hasfocus){ if(view.getid()==etaddress.getid()) toast.maketext(getapplicationcontext(), "地址文本框获得焦点!",toast.length_long).show(); } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。