gpt4 book ai didi

java - Android,按钮根本不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:35 26 4
gpt4 key购买 nike

这段代码代表了我真正的问题......

我正在研究一个 UI。更复杂。

我有一个自定义 View ,需要在左侧添加一个按钮。为什么?因为我有四个这个自定义 View 放在一边。那些按钮的 View 创建了一些闰 View !

我有一个包含按钮的简单布局。

我必须让他脱离他的布局父级,属性为 clipChildren="false"

但是按钮不响应onClickListener

我当然错过了什么,但是什么?

根本没有播放动画点击...连安卓的click歌曲都不播放...

java代码在那里没有效果..按钮的 id button2 工作。按钮的 id 按钮不起作用..

这是我的 xml 代码。

<LinearLayout
android:orientation="vertical"
android:background="#0000FF"
android:clipChildren="false"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#FF0"
android:layout_marginLeft="-20dp"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_marginLeft="-80dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="80dp" />
</LinearLayout>

和 onCreate 方法:

Button buton1 = (Button) rootView.findViewById(R.id.button);
buton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.wtf("-----", "Click");
Toast.makeText(getActivity(), "button1", Toast.LENGTH_LONG).show();
}
});

Button buton2 = (Button) rootView.findViewById(R.id.button2);
buton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "button2", Toast.LENGTH_LONG).show();
}
});

最佳答案

尝试将按钮放回其父项中。

如果它在那里工作,请非常认真思考一下为什么您不希望此按钮存在于其父项中。这几乎总是一个坏主意,因为它会破坏事物(无论是在实践上还是在概念上)。

我怀疑问题在于按钮占用空间的其他东西正在消耗触摸事件。

关于java - Android,按钮根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25094824/

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