gpt4 book ai didi

android - 如何让 OnClickListener 在具有 RelativeLayout 的自定义 View 上工作?

转载 作者:IT老高 更新时间:2023-10-28 23:40:38 26 4
gpt4 key购买 nike

问题

你好,

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

但不幸的是,onClickListener 没有触发(无论我是使用远程“确定”按钮单击 View 还是使用触摸板..)

我真的需要像普通按钮一样的行为。如何做到这一点?我已经花了几个小时搜索 Google 和 StackOverflow...(顺便说一句。在为 RelativeLayout 设置 android:clickable="false" 时,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" />

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

最佳答案

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

解决方案

重写 RoundedButton.java 中的 dispatchKeyEvent(KeyEvent event) 并实现您自己的 OnClickListener。然后写一个公共(public)的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);
}

}

关于android - 如何让 OnClickListener 在具有 RelativeLayout 的自定义 View 上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17977839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com