【android基础】之关于重复类型事件的处理方法

1、

在xml文件中配置android:onClick=“方法名”

 

2、

在所在activity中定义onClick方法

 

看下具体的实现:

1.main.xml文件

 

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:scrollbars="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- 横排开始 -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/mg1"
                    android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

            </LinearLayout>
        </LinearLayout>
  <!-- 横排结束 -->
               <!-- 横排开始 -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

           
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

              
            </LinearLayout>
        </LinearLayout>
  <!-- 横排结束 -->
  
          <!-- 横排开始 -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

              
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>
        </LinearLayout>
  <!-- 横排结束 -->
  
          <!-- 横排开始 -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

              
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>
        </LinearLayout>
  <!-- 横排结束 -->
  
          <!-- 横排开始 -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

               
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>
        </LinearLayout>
  <!-- 横排结束 -->
          <!-- 横排开始 -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

             
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/circle_corner1"
                android:orientation="vertical" >

                <ImageView
                     android:onClick="myClick"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_gravity="left"
                    android:padding="10dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/img01" />

               
            </LinearLayout>
        </LinearLayout>
  <!-- 横排结束 -->
  
    </LinearLayout>

</ScrollView>

 

 

2. MyOnClickListener.java文件

public void myClick(View target) {
  Intent intent;
  switch (target.getId()) {
  case R.id.mg1:
   intent  = new Intent(ImageListActivity.this, ImageSwitcher.class);
   startActivity(intent);
   break;
  case R.id.mg2:
    intent  = new Intent(ImageListActivity.this, ImageSwitcher.class);
   startActivity(intent);
   break;
  case R.id.mg3:
   intent  = new Intent(ImageListActivity.this, ImageSwitcher.class);
   startActivity(intent);
   break;
  case R.id.mg4:
   intent  = new Intent(ImageListActivity.this, ImageSwitcher.class);
   startActivity(intent);
   break;

.......
  default:
   break;
  }
  
 }

 

 

时间: 2024-10-26 20:38:41

【android基础】之关于重复类型事件的处理方法的相关文章

jQuery防止重复绑定事件的解决方法_jquery

本文实例分析了jQuery防止重复绑定事件的解决方法.分享给大家供大家参考,具体如下: 一.问题: 今天发现jQuery一个对象的事件可以重复绑定多次,当事件触发的时候会引起代码多遍执行. 下面是一个click事件被重复绑定的示例: function reg_button_click(){ $("#button).click(function(){ alert("button click"); }); } $(document).ready(function(){ #重复注册

Android给TextView添加点击事件的实现方法_Android

首先设定TextView的clickable属性为true. 可以在布局文件中进行设定,比如: <TextView android:id="@+id/phone" android:clickable="true" --------->设定此属性 android:layout_marginLeft="10dp" android:layout_below="@id/address" android:layout_toR

Android给TextView添加点击事件的实现方法

首先设定TextView的clickable属性为true. 可以在布局文件中进行设定,比如: <TextView android:id="@+id/phone" android:clickable="true" --------->设定此属性 android:layout_marginLeft="10dp" android:layout_below="@id/address" android:layout_toR

【Xamarin开发 Android 系列 4】 Android 基础知识

原文:[Xamarin开发 Android 系列 4] Android 基础知识 什么是Android?   Android一词的本义指"机器人",同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成,而且不存在任何以往阻碍移动产业创新的专有权障碍,号称是首个为移动终端打造的真正开放和完整的移动软件. Android是一种以Linux为基础的开放源代码操作系统,主要使用于便携设备.目前尚未有统一中文

Android基础控件(EditView、SeekBar等)的使用方法_Android

 android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView的使用方法.在介绍各种控件之前,先简单介绍android UI控件最基本的几种属性: id: id是控件唯一标识符,可通过**findViewById(R.id.*)**操作控件. layout_width:控件宽度,可设置为match_parent(充满父布局,即让父布局决定当前控件的宽度).wrap_

Android 基础课堂 - Lesson.3

Android 基础课堂 经过上一周的小课堂,我们已经成功创建一个Android工程啦,也知道了四大组件中使用最多的Activity,本周呢,按正常情况来看,我们是应该开始那个我们一直说的APP的开发了,好,那么就让我们正式进入开发吧. 既然要进入开发了,那么就让我们一步步来,首先先从搭建一个布局开始,于是乎,我们本周的小课堂,就得讲一讲Android里的五大布局了,这个嘛基础知识点,可能会比较乏味,不过还是很有必要理解的,把这些搞懂是今后做布局的第一步嘛,好了,让我们进入今天的Android基

Android防止按钮过快点击造成多次事件的解决方法_Android

问题 onClick事件是Android开发中最常见的事件.比如,一个submitButton,功能是点击之后会提交一个订单, 则一般代码如下,其中submitOrder()函数会跳转到下一页进行处理 : //代码0 submitButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { submitOrder(); } }); 正常情况下这段代码是没什么问题的,但是Andro

Android为按钮控件绑定事件的五种实现方式_Android

一.写在最前面 本次,来介绍一下安卓中为控件--Button绑定事件的五种方式. 二.具体的实现 第一种:直接绑定在Button控件上: 步骤1.在Button控件上设置android:onClick=",其中这个属性的属性值对应的是MainActivity类中的方法名字(自己创建的方法):  步骤2.在MainActivity类中创建相对应的方法: public void demo(View view){ Toast.makeText(MainActivity.this, "第二个按

Android基础知识之单点触摸_Android

相对于多点触摸,单点触摸还是很简单的. 新建一个工程,先看看布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="