我如何获得OnClickListener正与一个RelativeLayout的自定义视图?自定义、视图、如何获得、RelativeLayout

2023-09-04 23:59:16 作者:古风名字

问题

您好,

我已经建有一个RelativeLayout的使用它作为自定义图像按钮,自定义视图。 到目前为止,选择它的工作原理(PIC2),甚至当我点击它(使用GoogleTV的遥控器),认为成功地改变了它的状态PIC3(感谢安卓duplicateParentState =真)

但不幸的是,onClickListener不火(不,如果我点击查看与远程确定按钮,或我使用触摸板关系。)

我真的需要像一个正常的按钮相同的行为。 如何实现这一目标?我已经花了几个小时的搜索谷歌和计算器...(BTW设置机器人时:点击=假为RelativeLayout的,在OnClickListener的工作,但只有当我使用鼠标指针(触控板),之后的焦点丢失并且不显示状态(如图3))

图片

PIC1

PIC2

PIC3

code

rounded_button.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:可点击=真
机器人:可聚焦=真
机器人:focusableInTouchMode =假>

<的TextView
    机器人:ID =@ + ID /标题
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerVertical =真
    机器人:layout_marginLeft =30dp
    机器人:背景=@可绘制/ btn_rounded_corners
    机器人:以下属性来=25dp
    机器人:TEXTSIZE =15sp
    机器人:duplicateParentState =真/>

< ImageView的
    机器人:ID =@ + ID /图标
    风格=@风格/ Menu_Button
    机器人:layout_width =50dp
    机器人:layout_height =50dp
    机器人:layout_marginRight = -  50dp
    机器人:layout_toLeftOf =@ ID /标题
    机器人:背景=@可绘制/ btn_main_menu_back_shape
    工具:忽略=ContentDescription
    机器人:duplicateParentState =真/>
 

RoundedButton.java

 公共类RoundedButton扩展RelativeLayout的{
私人字符串标签;
私人诠释图标;

/ **
 * @参数方面
 * /
公共RoundedButton(上下文的背景下)
{
    超(上下文);
    initAttributes(背景下,NULL);
}

/ **
 * @参数方面
 * @参数ATTRS
 * /
公共RoundedButton(上下文的背景下,ATTRS的AttributeSet)
{
    超(背景下,ATTRS);
    initAttributes(背景下,ATTRS);
}

/ **
 * @参数方面
 * @参数ATTRS
 * @参数defStyle
 * /
公共RoundedButton(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
{
    超(背景下,ATTRS,defStyle);
    initAttributes(背景下,ATTRS);
}

私人无效initAttributes(上下文的背景下,ATTRS的AttributeSet)
{
    LayoutInflater.from(上下文).inflate(R.layout.rounded_button,这一点,真正的);

    TypedArray A =
        context.obtainStyledAttributes(ATTRS,R.styleable.RoundedButton);

    最终诠释N = a.getIndexCount();
    的for(int i = 0; I< N ++ I)
    {
        INT ATTR = a.getIndex(ⅰ);
        开关(ATTR)
        {
            案例R.styleable.RoundedButton_text:
                setLabel(a.getString(attr)使用);
                打破;
            案例R.styleable.RoundedButton_icon:
                的setIcon(a.getResourceId(ATTR,0));
                打破;
        }
    }
    a.recycle();
}

公共字符串getLabel()
{
    返回this.label;
}

公共无效setLabel(最后的字符串标签)
{
    this.label =标签;
    ((TextView中)findViewById(R.id.caption))的setText(this.label)。
}

/ **
 返回:图标
 * /
公众诠释调用getIcon()
{
    返回图标;
}

/ **
 *参数图标图标设置
 * /
公共无效的setIcon(INT图标)
{
    this.icon =图标;
    ((ImageView的)findViewById(R.id.icon))setImageResource(this.icon)。
}
}
 
JS中onclick与addEventListener的区别详解

activity_main.xml

的有关部分

 < eu.test.custom_views.RoundedButton
    机器人:ID =@ + ID /的CustomButton
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    自定义:图标=@可绘制/ hand_icon_green_left
    自定义:文本=正常状态/>
 

主要活动

 公共类MainActivity扩展活动实现OnClickListener {

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    ((RoundedButton)findViewById(R.id.custombutton))setOnClickListener(本);
}

@覆盖
公共无效的onClick(查看为arg0){
    如果(arg0.getId()== R.id.custombutton){Toast.makeText(这一点,点击,Toast.LENGTH_SHORT).show(); }
}
}
 

解决方案

我得到了它..现在的解决方案是如此简单,它花了一些时间; - )

解决方案

覆盖 dispatchKeyEvent(KeyEvent的事件)在RoundedButton.java和实现自己的OnClickListener。然后写一个公共 setOnClickListener 函数...

 私人OnClickListener监听器;

@覆盖
公共布尔dispatchTouchEvent(MotionEvent事件){
    如果(event.getAction()== MotionEvent.ACTION_UP){
        如果(!监听= NULL)listener.onClick(本);
    }
    返回super.dispatchTouchEvent(事件);
}


@覆盖
公共布尔dispatchKeyEvent(KeyEvent的事件){
    如果(event.getAction()== KeyEvent.ACTION_UP和放大器;及(event.getKey code()== KeyEvent.KEY code_DPAD_CENTER || event.getKey code()== KeyEvent的。 KEY code_ENTER)){
        如果(!监听= NULL)listener.onClick(本);
    }
    返回super.dispatchKeyEvent(事件);
}

公共无效setOnClickListener(OnClickListener监听器){
    this.listener =侦听器;
}
 

工作RoundedButton.java

 公共类RoundedButton扩展RelativeLayout的
{
私人OnClickListener监听器;
私人字符串标签;
私人诠释图标;

@覆盖
公共布尔dispatchTouchEvent(MotionEvent事件){
    如果(event.getAction()== MotionEvent.ACTION_UP){
        如果(!监听= NULL)listener.onClick(本);
    }
    返回super.dispatchTouchEvent(事件);
}


@覆盖
公共布尔dispatchKeyEvent(KeyEvent的事件){
    如果(event.getAction()== KeyEvent.ACTION_UP和放大器;及(event.getKey code()== KeyEvent.KEY code_DPAD_CENTER || event.getKey code()== KeyEvent的。 KEY code_ENTER)){
        如果(!监听= NULL)listener.onClick(本);
    }
    返回super.dispatchKeyEvent(事件);
}

公共无效setOnClickListener(OnClickListener监听器){
    this.listener =侦听器;
}

/ **
 * @参数方面
 * /
公共RoundedButton(上下文的背景下)
{
    超(上下文);
    initAttributes(背景下,NULL);
}

/ **
 * @参数方面
 * @参数ATTRS
 * /
公共RoundedButton(上下文的背景下,ATTRS的AttributeSet)
{
    超(背景下,ATTRS);
    initAttributes(背景下,ATTRS);
}

/ **
 * @参数方面
 * @参数ATTRS
 * @参数defStyle
 * /
公共RoundedButton(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
{
    超(背景下,ATTRS,defStyle);
    this.setClickable(真正的);
    this.setEnabled(真正的);
    this.setFocusable(真正的);
    this.setFocusableInTouchMode(真正的);

    initAttributes(背景下,ATTRS);
}

私人无效initAttributes(上下文的背景下,ATTRS的AttributeSet)
{
    LayoutInflater.from(上下文).inflate(R.layout.rounded_button,这一点,真正的);

    TypedArray A =
        context.obtainStyledAttributes(ATTRS,R.styleable.RoundedButton);

    最终诠释N = a.getIndexCount();
    的for(int i = 0; I< N ++ I)
    {
        INT ATTR = a.getIndex(ⅰ);
        开关(ATTR)
        {
            案例R.styleable.RoundedButton_text:
                setLabel(a.getString(attr)使用);
                打破;
            案例R.styleable.RoundedButton_icon:
                的setIcon(a.getResourceId(ATTR,0));
                打破;
        }
    }
    a.recycle();
}

公共字符串getLabel()
{
    返回this.label;
}

公共无效setLabel(最后的字符串标签)
{
    this.label =标签;
    ((TextView中)findViewById(R.id.caption))的setText(this.label)。
}

/ **
 返回:图标
 * /
公众诠释调用getIcon()
{
    返回图标;
}

/ **
 *参数图标图标设置
 * /
公共无效的setIcon(INT图标)
{
    this.icon =图标;
    ((ImageView的)findViewById(R.id.icon))setImageResource(this.icon)。
}
 

}

Question

Hello,

i have built a custom view with a RelativeLayout to use it as custom image button. So far, selecting it works (PIC2) and even when I click it (using GoogleTV Remote), the view successfully changes it's state to PIC3 (thanks to android:duplicateParentState="true")

But unfortunately the onClickListener does not fire (doesn't matter if I click the View with the Remote "OK" Button or I use the touchpad..)

I really need the same behavior like a normal button. How to accomplish that? I already spent a few hours on searching Google and StackOverflow... (BTW. when setting android:clickable="false" for the RelativeLayout, the OnClickListener is working, but only when I use the mouse pointer (Touchpad) and afterwards the focus is lost and the state (pic 3) is not displayed)

Pictures

PIC1

PIC2

PIC3

Code

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

Relevant part of 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" />

Main Activity

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

解决方案

I got it now.. the solution is so simple, that it took some time ;-)

Solution

Override dispatchKeyEvent(KeyEvent event) in RoundedButton.java and implement your own OnClickListener. Then write a public setOnClickListener function...

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

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

}

 
精彩推荐
图片推荐