如何让OnClickListener使用RelativeLayout处理自定义视图?

时间:2013-07-31 18:12:42

标签: android google-tv

问题

您好,

我使用RelativeLayout构建了自定义视图,以将其用作自定义图像按钮。 到目前为止,选择它工作(PIC2),甚至当我点击它(使用GoogleTV Remote)时,视图成功地将其状态更改为PIC3(感谢android:duplicateParentState="true"

但不幸的是onClickListener没有触发(如果我点击带有遥控器的视图“确定”按钮或者我使用触摸板,则无关紧要。)

我真的需要像普通按钮一样的行为。 怎么做到这一点?我已经花了几个小时来搜索Google和StackOverflow ...(顺便说一下。当为android:clickable="false"设置RelativeLayout时,OnClickListener正在运行,但只有当我使用鼠标指针时(触摸板)然后焦点丢失,状态(图3)不显示)

图片

PIC1

Custom View Normal

PIC2

Custom View Focused

PIC3

Custom View Clicked

代码

rounded_button.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="false">

<TextView
    android:id="@+id/caption"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="30dp"
    android:background="@drawable/btn_rounded_corners"
    android:paddingLeft="25dp"
    android:textSize="15sp" 
    android:duplicateParentState="true"/>

<ImageView
    android:id="@+id/icon"
    style="@style/Menu_Button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="-50dp"
    android:layout_toLeftOf="@id/caption"
    android:background="@drawable/btn_main_menu_back_shape"
    tools:ignore="ContentDescription"
    android:duplicateParentState="true" />

RoundedButton.java

public class RoundedButton extends RelativeLayout {
private String label;
private int icon;

/**
 * @param context
 */
public RoundedButton(Context context)
{
    super(context);
    initAttributes(context, null);
}

/**
 * @param context
 * @param attrs
 */
public RoundedButton(Context context, AttributeSet attrs)
{
    super(context, attrs);
    initAttributes(context, attrs);
}

/**
 * @param context
 * @param attrs
 * @param defStyle
 */
public RoundedButton(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    initAttributes(context, attrs);
}

private void initAttributes(Context context, AttributeSet attrs)
{
    LayoutInflater.from(context).inflate(R.layout.rounded_button, this, true);

    TypedArray a = 
        context.obtainStyledAttributes(attrs, R.styleable.RoundedButton);

    final int N = a.getIndexCount();
    for (int i = 0; i < N; ++i)
    {
        int attr = a.getIndex(i);
        switch (attr)
        {
            case R.styleable.RoundedButton_text:
                setLabel(a.getString(attr));
                break;
            case R.styleable.RoundedButton_icon:
                setIcon(a.getResourceId(attr, 0));
                break;
        }
    }
    a.recycle();
}

public String getLabel()
{
    return this.label;
}

public void setLabel(final String label)
{
    this.label = label;
    ((TextView)findViewById(R.id.caption)).setText(this.label);
}

/**
 * @return the icon
 */
public int getIcon()
{
    return icon;
}

/**
 * @param icon the icon to set
 */
public void setIcon(int icon)
{
    this.icon = icon;
    ((ImageView)findViewById(R.id.icon)).setImageResource(this.icon);
}
}

activity_main.xml的相关部分

<eu.test.custom_views.RoundedButton
    android:id="@+id/custombutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    custom:icon="@drawable/hand_icon_green_left"
    custom:text="Normal state" />

主要活动

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ((RoundedButton) findViewById(R.id.custombutton)).setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
    if(arg0.getId() == R.id.custombutton) { Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show(); }
}
}

3 个答案:

答案 0 :(得分:28)

我现在得到了......解决方案非常简单,需要一些时间; - )

解决方案

覆盖RoundedButton.java中的dispatchKeyEvent(KeyEvent event)并实现自己的OnClickListener。然后写一个公共setOnClickListener函数......

private OnClickListener listener;

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP) {
        if(listener != null) listener.onClick(this);
    }
    return super.dispatchTouchEvent(event);
}


@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
        if(listener != null) listener.onClick(this);
    }
    return super.dispatchKeyEvent(event);
}

public void setOnClickListener(OnClickListener listener) {
    this.listener = listener;
}

使用RoundedButton.java

public class RoundedButton extends RelativeLayout
{
private OnClickListener listener;
private String label;
private int icon;

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_UP) {
        if(listener != null) listener.onClick(this);
    }
    return super.dispatchTouchEvent(event);
}


@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_UP && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER || event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
        if(listener != null) listener.onClick(this);
    }
    return super.dispatchKeyEvent(event);
}

public void setOnClickListener(OnClickListener listener) {
    this.listener = listener;
}

/**
 * @param context
 */
public RoundedButton(Context context)
{
    super(context);
    initAttributes(context, null);
}

/**
 * @param context
 * @param attrs
 */
public RoundedButton(Context context, AttributeSet attrs)
{
    super(context, attrs);
    initAttributes(context, attrs);
}

/**
 * @param context
 * @param attrs
 * @param defStyle
 */
public RoundedButton(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    this.setClickable(true);
    this.setEnabled(true);
    this.setFocusable(true);
    this.setFocusableInTouchMode(true);

    initAttributes(context, attrs);
}

private void initAttributes(Context context, AttributeSet attrs)
{
    LayoutInflater.from(context).inflate(R.layout.rounded_button,  this, true);

    TypedArray a = 
        context.obtainStyledAttributes(attrs, R.styleable.RoundedButton);

    final int N = a.getIndexCount();
    for (int i = 0; i < N; ++i)
    {
        int attr = a.getIndex(i);
        switch (attr)
        {
            case R.styleable.RoundedButton_text:
                setLabel(a.getString(attr));
                break;
            case R.styleable.RoundedButton_icon:
                setIcon(a.getResourceId(attr, 0));
                break;
        }
    }
    a.recycle();
}

public String getLabel()
{
    return this.label;
}

public void setLabel(final String label)
{
    this.label = label;
    ((TextView)findViewById(R.id.caption)).setText(this.label);
}

/**
 * @return the icon
 */
public int getIcon()
{
    return icon;
}

/**
 * @param icon the icon to set
 */
public void setIcon(int icon)
{
    this.icon = icon;
    ((ImageView)findViewById(R.id.icon)).setImageResource(this.icon);
}

}

答案 1 :(得分:1)

我正在RelativeLayout中创建自定义视图的实例(继承自RecyclerView.Adapter)并将list_item.xml扩展到每个新实例中,然后将我的侦听器附加到{{1} }方法,但即使理论上我正在以正确的方式做事在我从onBindViewHolder()(我的自定义视图)&#39中删除android:clickable="true"属性之前它没有工作; s根布局

答案 2 :(得分:-1)

public class CustomView extends RelativeLayout implements OnClickListener {

    private OnClickListener clickListener;

    public CustomView(Context context) {
        super(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        super.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (clickListener != null) {
            clickListener.onClick(v);
        }
    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
        this.clickListener = l;
    }

}