首页 新闻 会员 周边

android自定义view如何实现交互

0
悬赏园豆:80 [待解决问题]

我的一个布局是这样的(手机app上的):


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/radar_serch_bg"
android:orientation="vertical" >

<com.zmodo.fdq.widget.SearchDevicesView
android:id="@+id/search_device_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.zmodo.fdq.widget.SearchDevicesView>

<Button
android:id="@+id/radar_serch_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:background="@drawable/back_white" />

。。。。。。

</RelativeLayout>

其中SearchDevicesView是一个自定义view,里面主要是实现了onDraw来模拟了一个雷达动画,雷达搜索的时候会搜索到一个蓝牙设备(会把它显示在这个view上),当点击这个设备的时候我希望进行的操作是连接蓝牙设备到手机上,而且我希望这个连接的动作是在上面RelativeLayout所对应的 activity里面进行处理,我的问题是,当这个view组件被点击,我在activity里面能够知道它其中的自定义view被点击了吗,如何做到?

菜鸟的梦醒的主页 菜鸟的梦醒 | 初学一级 | 园豆:71
提问于:2015-03-12 13:48
< >
分享
所有回答(1)
0

在你自定义的SearchDevicesView里面,继承View.onClickListener()(名字记不住了,大概是这个),然后当SearchDevicesView被点击后,会被回调~

JoyPaPa | 园豆:209 (菜鸟二级) | 2015-03-12 13:53

我在自定义view里面有一个检测是否点击的事件处理:

public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            handleActionDownEvenet(event);
            return true;
        case MotionEvent.ACTION_MOVE:
            return true;
        case MotionEvent.ACTION_UP:
            return true;
        }
        return super.onTouchEvent(event);
    }

如果自定义view被点击,肯定会检测到,我的问题是,那个还有自定义view的activity怎么知道,我要在那个里面处理 

支持(0) 反对(0) 菜鸟的梦醒 | 园豆:71 (初学一级) | 2015-03-12 13:58

@菜鸟的梦醒: 这就要用到Android的Handler了,View侦测到事件,然后通过Handler发消息出去,然后Activity里面,Handler来接收并处理消息~

支持(0) 反对(0) JoyPaPa | 园豆:209 (菜鸟二级) | 2015-03-12 14:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册