17

问题

你好,

我已经建立了一个自定义视图,RelativeLayout将其用作自定义图像按钮。到目前为止,选择它有效(PIC2),即使我单击它(使用 GoogleTV Remote),视图也成功将其状态更改为 PIC3(感谢android:duplicateParentState="true"

但不幸的是,onClickListener它不会触发(如果我使用远程“确定”按钮单击视图或使用触摸板都没有关系..)

我真的需要像普通按钮一样的行为。如何做到这一点?我已经花了几个小时搜索 Google 和 StackOverflow...(顺便说一句。设置android:clickable="false"为不显示)RelativeLayoutOnClickListener

图片

PIC1

自定义视图正常

PIC2

以自定义视图为中心

PIC3

点击自定义视图

代码

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(); }
}
}
4

2 回答 2

32

我现在明白了.. 解决方案非常简单,花了一些时间;-)

解决方案

dispatchKeyEvent(KeyEvent event)在 RoundedButton.java 中重写并实现您自己的 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);
}

}

于 2013-07-31T19:11:02.587 回答
7

我在 a 中创建了我的自定义视图(继承自RelativeLayout)的实例RecyclerView.Adapter并膨胀list_item.xml到每个新实例中,然后将我的侦听器附加到onBindViewHolder()方法中,但即使理论上我以正确的方式做事,直到我删除它才起作用(我的自定义视图)的根布局中的android:clickable="true"属性list_item.xml

于 2016-08-11T09:11:41.560 回答