加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

关于自定义linearlayout点击不能触发事件问题的解决

(2017-05-26 06:21:20)
1.自定义linearlayout如下:
package com.balanceapp;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ImageButton_define extends LinearLayout {

    private  ImageButton   imagebutton;
    private  TextView   textView;
   
    public ImageButton_define(Context context,AttributeSet attrs) {
        super(context,attrs);
        // TODO Auto-generated constructor stub   
        imagebutton = new ImageButton(context, attrs);   
        imagebutton.setPadding(0, 0, 0, 0);   
        imagebutton.setScaleType(ImageButton.ScaleType.FIT_CENTER);
        imagebutton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0, 7));
      //关闭imagebutton点击事件,返回点击事件到ImageButton_define处理
        imagebutton.setClickable(false);
        textView =new TextView(context, attrs);
        //水平居中
        textView.setGravity(android.view.Gravity.CENTER);   
        textView.setPadding(0, 0, 0, 0);
        textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,0, 3));
        textView.setClickable(false);
        setFocusableInTouchMode(true);    //触摸获取焦点
        setClickable(true);   
        setFocusable(true);        //键盘按键获取焦点
        setOnTouchListener(null);
        setOnClickListener(null);
        setBackgroundColor(0x00000000);
        //setBackgroundResource(android.R.drawable.btn_default);   
        setOrientation(LinearLayout.VERTICAL);   
        addView(imagebutton);   
        addView(textView);       
    }
}
注意:红色字体部分,需要在自定义时,关闭内部控件点击响应。

2.布局引用
                        android:id="@+id/imageButton_open_bluetooth"
                android:layout_width="100dp"
                android:layout_height="120dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="#00000000"
                android:src="@drawable/button_bluetooth_style"
                android:text="@string/button_open_bluetooth" >
           
3.imagebutton风格


   
        android:state_pressed="true"
        android:drawable="@drawable/bluetooth_click" />
   
        android:state_focused="true"
        android:drawable="@drawable/bluetooth_click" />
   
        android:state_enabled="false"
        android:drawable="@drawable/bluetooth_disable" />
   
        android:drawable="@drawable/bluetooth" />

4、响应事件处理
ImageButton_open_bluetooth = (ImageButton_define)findViewById(R.id.imageButton_open_bluetooth);
        ImageButton_open_bluetooth.setOnTouchListener(myOnTouchListener);
        ImageButton_open_bluetooth.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                //如果ImageButton状态为onClick则响应点击事件
                String str = "请先连接称重蓝牙设备!";
                Toast.makeText(getApplicationContext(),str ,Toast.LENGTH_LONG).show();
                System.out.println("MyBalanceApp_show_init--------->setOnClickListener");
            }
        });
//触摸事件处理
public OnTouchListener myOnTouchListener = new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //TODO Auto-generated method stub
            if(event.getAction()==MotionEvent.ACTION_DOWN)
            {
                System.out.println("myOnTouchListener--------->down");
            }
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                System.out.println("myOnTouchListener--------->up");
            }
            return false;
        }
    };

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有