Android: eventListener

本文详细介绍了Android开发中常见的六种事件监听器,包括单击事件、焦点事件、按键事件、触碰事件、创建上下文菜单事件及选中事件,并提供了具体的实现代码示例。

常见事件监听器

1.单击事件:当用户触碰某一个组件或者方向键被按下时产生的事件

view.OnClickListener (), 处理方法为 onClick()

        loginButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), loginButton.getText(), Toast.LENGTH_LONG);
				
			}
		});
        
        cancelButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), cancelButton.getText(), Toast.LENGTH_LONG);
				
			}
		});


2. 焦点事件:组件得到或者失去焦点时产生该事件

view.OnFocusChangeListener, 处理方法为onFocusChange()

        usernameEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), passwordEditText.getText(), Toast.LENGTH_LONG);
				
			}
		});
        
        passwordEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), passwordEditText.getText(), Toast.LENGTH_LONG);
				
			}
		});


3.按键事件:用户按下或者释放设备上的某个按键时产生,

view.OnKeyListener, 处理方法为onKey()

        usernameEditText.setOnKeyListener(new OnKeyListener() {
        	//按键方法
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				usernameEditText.setText("");
				return false;
			}
		});
        
        passwordEditText.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				passwordEditText.setText("");
				return false;
			}
		});


4.触碰事件:设备具有触摸屏功能时,触摸屏幕产生该事件

view.OnToughListener, 处理方法为onTough()

5.创建上下文菜单事件: 创建上下文菜单时产生该事件

view.OnCreateContextMenuListener, 处理方法为onCreateContextMenu()

6.选中事件:当用户选择一个选项时,触发该事件

view.OnCheckChangeListener,处理方法为onCheckChange()

               autoCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), autoCheckBox.isChecked()+"", Toast.LENGTH_LONG);
			}
		});




<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.shehuan.statusview.StatusView android:id="@+id/statusView" android:layout_width="match_parent" android:layout_height="match_parent"> <com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/smartRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" app:srlFooterTranslationViewId="@id/video_rv"> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/appBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:outlineProvider="paddedBounds" android:background="@color/transparent" app:elevation="0dp"> <com.google.android.material.appbar.CollapsingToolbarLayout android:id="@+id/toolbarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/home_rv" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" /> </com.google.android.material.appbar.CollapsingToolbarLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/type_rv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:layout_marginBottom="8dp" android:layout_marginRight="12dp" android:layout_marginLeft="12dp" app:layout_collapseMode="pin"/> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.shehuan.statusview.StatusView android:id="@+id/statusView1" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/video_rv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp"/> </com.shehuan.statusview.StatusView> <View android:layout_width="match_parent" android:layout_height="61dp" /> </LinearLayout> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </com.shehuan.statusview.StatusView> </LinearLayout> home_rv的滑动监听不起作用怎么解决
最新发布
12-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值