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

简介: 1、在xml文件中配置android:onClick=“方法名” 2、在所在activity中定义onClick方法 看下具体的实现:1.

1、

 

 

看下具体的实现:

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;
  }
  
 }

 

 

相关文章
|
7月前
|
Android开发 开发者
Android基础知识:什么是Intent?有哪些类型的Intent?
Android基础知识:什么是Intent?有哪些类型的Intent?
424 0
|
7月前
|
编解码 Android开发
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)1
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)
365 1
|
2月前
|
Android开发
Android面试高频知识点(1) 图解Android事件分发机制
Android面试高频知识点(1) 图解Android事件分发机制
|
2月前
|
Android开发
Android面试高频知识点(1) 图解 Android 事件分发机制
Android面试高频知识点(1) 图解 Android 事件分发机制
39 1
|
2月前
|
XML 前端开发 Android开发
Android面试高频知识点(1) 图解Android事件分发机制
Android面试高频知识点(1) 图解Android事件分发机制
Android面试高频知识点(1) 图解Android事件分发机制
|
2月前
|
Android开发
Android 事件分发机制详细解读
Android 事件分发机制详细解读
40 4
|
7月前
|
编解码 开发工具 Android开发
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)2
Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)2
422 2
|
4月前
|
图形学 Android开发
小功能⭐️Unity调用Android常用事件
小功能⭐️Unity调用Android常用事件
|
4月前
|
Android开发
Android面试高频知识点(1) 图解 Android 事件分发机制
在Android开发中,事件分发机制是一块Android比较重要的知识体系,了解并熟悉整套的分发机制有助于更好的分析各种点击滑动失效问题,更好去扩展控件的事件功能和开发自定义控件,同时事件分发机制也是Android面试必问考点之一,如果你能把下面的一些事件分发图当场画出来肯定加分不少。废话不多说,总结一句:事件分发机制很重要。
200 9
|
4月前
|
开发工具 Android开发
Android项目架构设计问题之组件A通知组件B某个事件的发生如何解决
Android项目架构设计问题之组件A通知组件B某个事件的发生如何解决
41 0