




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
android如何创建自己的监听接口第一步:创建接口和方法public interface OnUsernameAvailableListener public abstract void onAvailableChecked(String username, boolean available);第二步:定义变量保存listenerOnUsernameAvailableListener onUsernameAvailableListener = null;第三步:提供方法允许用户设置Listener/ Allows the user to set an Listener and react to the eventpublic void setOnUsernameAvailableListener(OnUsernameAvailableListener listener) onUsernameAvailableListener = listener;第四步:提供方法允许用户获得Listenerpublic final OnUsernameAvailableListener getOnUsernameAvailableListener() return onUsernameAvailableListener;第五步:添加方法,此方法每次在监听事件被触发的时候调用/ This function is called after the check was completeprivate void OnUserChecked(String username, boolean available)/ Check if the Listener was set, otherwise well get an Exception when we try to call itif(onUsernameAvailableListener!=null) / Only trigger the event, when we have a usernameif(!TextUtils.isEmpty(username)onUsernameAvailableListener.onAvailableChecked(username, available);完成。附完整代码:package com.tseng.examples;import com.tseng.examples.CheckUsernameEditText.OnUsernameAvailableListener;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class RegisterExample extends Activity / Declare our Views, so we can access them laterprivate CheckUsernameEditText etUsername;private EditText etPassword;private EditText etPassword2;private Button btnRegister;private Button btnCancel;private TextView lblUserStatus; /* Called when the activity is first created. */ Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); / Set Activity Layout setContentView(R.layout.main); / Get the EditText and Button References etUsername = (CheckUsernameEditText)findViewById(R.id.username); etPassword = (EditText)findViewById(R.id.password); etPassword2 = (EditText)findViewById(R.id.password2); btnRegister = (Button)findViewById(R.id.register_button); btnCancel = (Button)findViewById(R.id.cancel_button); lblUserStatus = (TextView)findViewById(R.id.userstatus); / Set our new Listener to the Username EditText view etUsername.setOnUsernameAvailableListener(new OnUsernameAvailableListener()Overridepublic void onAvailableChecked(String username, boolean available) / Handle the event hereif(!available)etUsername.setTextColor(Color.RED);lblUserStatus.setText(username + is already taken. Please choose another login name.); else etUsername.setTextColor(Color.GREEN);lblUserStatus.setText(username + is available.); ); / Set Click Listener btnRegister.setOnClickListener(new OnClickListener() Overridepublic void onClick(View v) / create Account); btnCancel.setOnClickListener(new OnClickListener() Overridepublic void onClick(View v) / Close the applicationfinish();); package com.tseng.examples;import java.lang.reflect.Array;import android.content.Context;import android.text.TextUtils;import android.util.AttributeSet;import android.view.KeyEvent;import android.view.View;import android.view.View.OnKeyListener;import android.widget.EditText;import android.widget.TextView;public class CheckUsernameEditText extends EditText implements OnKeyListener OnUsernameAvailableListener onUsernameAvailableListener = null;final private static String registeredUsers = new String / This is just a fixed List for tutorial purposes/ in a real application youd check this server sided or inside the databasetseng,admin,root,joedoe,john;public CheckUsernameEditText(Context context) super(context);/ Set KeyListener to ourselfthis.setOnKeyListener(this);public CheckUsernameEditText(Context context, AttributeSet attrs, int defStyle) super(context, attrs, defStyle);/ Set KeyListener to ourselfthis.setOnKeyListener(this);public CheckUsernameEditText(Context context, AttributeSet attrs) super(context, attrs);/ Set KeyListener to ourselfthis.setOnKeyListener(this);/ Allows the user to set an Listener and react to the eventpublic void setOnUsernameAvailableListener(OnUsernameAvailableListener listener) onUsernameAvailableListener = listener;/ This function is called after the check was completeprivate void OnUserChecked(String username, boolean available)/ Check if the Listener was set, otherwise well get an Exception when we try to call itif(onUsernameAvailableListener!=null) / Only trigger the event, when we have a usernameif(!TextUtils.isEmpty(username)onUsernameAvailableListener.onAvailableChecked(username, available);Overridepublic boolean onKey(View v, int keycode, KeyEvent keyevent) / We only want to handle ACTION_UP events, when user releases a keyif(keyevent.getAction()=KeyEvent.ACTION_DOWN)return false;boolean available = true;/ Whenever a user press a key, check if the username is availableString username = getText().toString().toLowerCase();if(!TextUtils.isEmpty(username)/ Only perform check, if we have anything inside the EditText boxfor(int i=0; iregisteredUsers.length; i+) if(registeredUsersi.equals(username)available = false;/ Finish the loop, as the name is already takenbreak;/ Tr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 彭阳消防考试题库及答案
- 2025年贵州公务员考试行测真题及答案
- 2025年广西壮族自治区中央遴选真题及参考答案(b类)
- 淮安清中开学考试卷及答案
- 母婴护理师考试试卷题库及答案
- 信息技术考试真题分类及答案
- 医学生化考试试题及答案
- 广东春季高考考试卷子及答案
- 九江编制考试题库及答案
- 2025年医疗器械法规与管理考试试题及答案
- 2025年注册会计师题库带答案分析
- 呼吸科出科考试题临床及答案2025版
- 设计管控管理办法
- 仓储能力及管理办法
- ROCK1蛋白:解锁食管鳞癌奥秘的关键密码
- 物流月结合同协议书范本
- 过敏性皮炎的治疗及护理
- 心理健康教育:男生女生
- 房颤内科护理学
- 《大中型企业安全生产标准化管理体系要求》
- 政策变迁课件
评论
0/150
提交评论