Android控件监听方面,用接口实现监听是最好的,在Android 本身就提供了各种控件监听接口,我们只要按照这样实现,看起来代码会很整洁。实现的效果也是很好的,下面我列举了常用控件的接口监听,layout ,checkbox,RadioGroup,以及listview的单击或者长按监听。下面请看代码,有注释。 本文项目源码地址: 点击此处下载 转载请注明出处:http://blog.csdn.net/qq_16064871
package com.example.impletedemo;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.ListView;import android.widget.RadioGroup;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements         OnClickListener,CompoundButton.OnCheckedChangeListener,RadioGroup.OnCheckedChangeListener,OnItemLongClickListener,OnItemClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InitUI();}private void InitUI() {View Layout1 = findViewById(R.id.Layout1);CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);//接口实现监听,都是需要进行设置监听if (Layout1 != null)Layout1.setOnClickListener(this);if (checkBox1 != null)checkBox1.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener)this);if (checkBox2 != null)checkBox2.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener)this);if (radioGroup1 != null)radioGroup1.setOnCheckedChangeListener((RadioGroup.OnCheckedChangeListener)this);ListView myListView = (ListView) findViewById(R.id.listView1);myListView.setOnItemLongClickListener(this); // 设置ListView长按myListView.setOnItemClickListener(this);    // 设置ListView单击SimpleAdapter simpleAdapter = new SimpleAdapter(this, getDataSource(), R.layout.layout_list_item, new String[]{"title"}, new int[]{R.id.textView1});myListView.setAdapter(simpleAdapter);}public List<Map<String, Object>> getDataSource() {List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();  Map<String, Object> map = new HashMap<String, Object>();  map.put("title", "北京");listems.add(map);map = new HashMap<String, Object>();map.put("title", "上海");listems.add(map);map = new HashMap<String, Object>();map.put("title", "广州");listems.add(map);map = new HashMap<String, Object>();map.put("title", "南京");listems.add(map);map = new HashMap<String, Object>();map.put("title", "苏州");listems.add(map);return listems;}@Override//一般layout button监听实现方法public void onClick(View arg0) {if (arg0.getId() == R.id.Layout1) {TextView textView = (TextView) findViewById(R.id.textView1);textView.setText("layout监听");Toast.makeText(this, "layout监听", Toast.LENGTH_SHORT).show();}}@Override//checkbox监听实现方法public void onCheckedChanged(CompoundButton arg0, boolean arg1) {if (arg0.getId() == R.id.checkBox1) {if (arg1) {Toast.makeText(this, "选中单项1", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "取消单项1", Toast.LENGTH_SHORT).show();}} else if (arg0.getId() == R.id.checkBox2) {if (arg1) {Toast.makeText(this, "选中单项2", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "取消单项2", Toast.LENGTH_SHORT).show();}}}@Override//RadioGroup监听实现方法public void onCheckedChanged(RadioGroup arg0, int arg1) {if (arg0.getId() == R.id.radioGroup1) {if (arg1 == R.id.radio1) {           Toast.makeText(this, "选中男", Toast.LENGTH_SHORT).show();}else if (arg1 == R.id.radio2) {   Toast.makeText(this, "选中女", Toast.LENGTH_SHORT).show();}}}@Override//listview长按监听实现方法public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {Toast.makeText(this, "listview 长按" + getDataSource().get(arg2), Toast.LENGTH_SHORT).show();//这里返回一定是true , 不然会同时触发单击return true;   }@Override//listview单击监听实现方法public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {Toast.makeText(this, "listview 单击" + getDataSource().get(arg2), Toast.LENGTH_SHORT).show();}}

xml如下
<LinearLayout xmlns:tools="http://schemas.android.com/tools"    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/Layout1"        android:layout_width="match_parent"        android:layout_height="60dp"        android:gravity="center_vertical"        android:orientation="vertical" >        <TextView            android:id="@+id/textView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="TextView" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="60dp"        android:gravity="center_vertical"        android:orientation="vertical" >        <CheckBox            android:id="@+id/checkBox1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="单项1" />        <CheckBox            android:id="@+id/checkBox2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="单项2" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="90dp"        android:gravity="center_vertical"        android:orientation="vertical" >        <RadioGroup            android:id="@+id/radioGroup1"            android:layout_width="wrap_content"            android:layout_height="wrap_content" >            <RadioButton                android:id="@+id/radio1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="男" />            <RadioButton                android:id="@+id/radio2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="女" />        </RadioGroup>    </LinearLayout>    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </ListView></LinearLayout>

界面效果图

下面看一下常用监听接口,作为程序员,都会使用代码自动提示以及填充。接下来看看下图:可以找到你需要的接口。


好的到这里就结束了,我每次写博客,都是自已先实践,写代码。所以都有一个demo。欢迎交流。 本文项目源码地址:点击此处下载 转载请注明出处:http://blog.csdn.net/qq_16064871

更多相关文章

  1. 【QQ登录】Android_SDK使用说明
  2. 解析Android重要包功能描述
  3. Android进阶之AIDL的使用详解
  4. Android上HDMI介绍(基于高通平台)
  5. Flutter在Android(安卓)Studio上的初启动
  6. 深度详解Retrofit2使用(二)实践
  7. 我的第一个Android程序helloword及个人理解
  8. 理解Android系统的进程间通信原理(二)----RPC机制
  9. Android(安卓)Treble 简介

随机推荐

  1. 实力认证|Authing 入选 2021 《中国网络安
  2. redis与lua
  3. nginx 负载均衡
  4. 进程终止,环境表以及在内存中布局和非局
  5. linux学习基本
  6. Spark 3.0 中七个必须知道的 SQL 性能优
  7. Js学习笔记一(鼠标事件.....)
  8. php7新特性一览
  9. 程序员专享绿色独角兽Gunicorn,了解下
  10. redis事务,分布式锁