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{ @Override protected 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
时间: 2024-08-11 09:53:16