gpt4 book ai didi

android - 你如何使整个控件在 android 中可点击?

转载 作者:行者123 更新时间:2023-11-29 18:22:49 24 4
gpt4 key购买 nike

我在 android 中有一个自定义控件,它有一个 TextView 、一些图像等。

我想让整个控件都可以点击。

我声明了布局:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_gravity="center_vertical">

即使它的 android:clickable 属性设置为 true,点击事件也不会触发。

我做错了什么吗?

最佳答案

在您的 Main Activity 代码中,执行类似这样的操作

public class TestActivity extends Activity implements
OnTouchListener, OnLongClickListener, OnKeyListener,
OnClickListener, OnFocusChangeListener{

CustomUI = (CustomView) findViewById(R.id.custom_ui);

CustomUI.addListener(this);
CustomUI.setOnClickListener(this);
CustomUI.setOnFocusChangeListener(this);
CustomUI.setOnLongClickListener(this);
CustomUI.setOnTouchListener(this);
CustomUI.setOnKeyListener(this);

}

你也不能把属性放在你的相对布局中,你必须在这个布局中包含你的自定义用户界面......就像

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<com.company.some.CustomView

android:id="@+id/custom_ui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
/>

</RelativeLayout>

关于android - 你如何使整个控件在 android 中可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4362816/

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