Android 常用控件接口监听

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

Android 常用控件接口监听的相关文章

Android常用控件:进度条

各种进度条属于 ProgressBar的子类 Sytle: 水平风格:Horizontal小风格:Small大风格:Large反向风格:Inverse小反向风格:Small.Inverse大反向风格:Large.Inverse 设置style:   style="?android:attr/progressBarStyle..." 主要属性:最大值:max当前进度:progress次要进度值:SecondaryProgress --效果类似于看电影那些缓冲 判断进度条是转圈还是水平的方

android常用控件

在 Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑文本控件 ProgressBar - 进度条控件 SeekBar - 可拖动的进度条控件 AutoCompleteTextView - 支持自动完成功能的可编辑文本控件 MultiAutoCompleteTextView - 支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的

android控件edittext-addTextChangedListener监听文本的变化

前言:edittext的addTextChangedListener监听事件用于监听edittext的输入文本的变化,他都用于密码框,或者那种检测用户输入过程中的变化. 1.使用方式   ①为edittext添加监听器 1 mEtPassword = (EditText) findViewById(R.id.id_et_password); 2 //添加监听器 3 mEtPassword.addTextChangedListener(new MyWatcher()); ②创建一个实现TextWa

非专业码农 JAVA学习笔记 用户图形界面设计与实现-所有控件的监听事件

用户图形界面设计与实现-监听事件 System.applet.Applet (一)用户自定义成分 1.绘制图形 Public voit piant(Ghraphics g){  g.drawLine等图形名称(坐标1234);g.file图形名(坐标123)} 2.设置字体-Font类 (1)定义font:Font myfont=new Font(“字体”,”样式”,字号); 例如:Font myfont=new Font(“宋体”,Font.BOLD,12); (2)引用定义的Font:类/容

Android常用控件及对应Robotium API

最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行?个动作 Button Text field 可编辑的文本区域,可以使用AutoCompleteTextView创建一个带有自动完成功能的编辑文本域 EditText,AutoCompleteTextView Checkbox 复选框,?个可以由用户切换的ON/OFF开关.当提供给用户?组不互斥的可选项时,你应该使用复选框 Che

Android常用控件之下拉刷新Wifi列表

有些列表信息需要手动去更新,此时比较常用的就是下拉刷新列表,在这里就使用下拉列表来刷新当前Wifi信息 目录结构 界面               关键代码 下拉列表类 package com.example.dropdownrefresh.ui; import java.text.SimpleDateFormat; import java.util.Date; import com.example.dropdownrefresh.R; import android.content.Contex

Android常用控件之AutoCompleteTextView、Spinner

概述 1.AutoCompleteTextView:相对于普通的TextView,AutoCompleteTextView的特点是可以自动提示文本,它可以通过SetAdapter()方法加载适配器. 2.Spinner:一种下拉列表. 知识内容 AutoCompletedTextView 布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="

Android常用控件之GridView与ExpandableListView的用法

概述 1.GridView:与ListView相比,可以显示多列,xml布局时其属性numColumns可以设置显示的列数. 2.ExpandableListView:与ListView相比,可以让每一列单元都拥有子列表. 内容 GridView 显示3列和多行的图片以及名称 布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sc

android常用控件纪录

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orien