【问题标题】:Button not responding to first click按钮没有响应第一次点击
【发布时间】:2014-08-28 10:55:22
【问题描述】:

我正在开发一个 android 应用程序,并有一个启动另一个活动的按钮。按钮不响应第一次点击,但响应后续触摸。相关按钮的 xml

<Button
    android:id="@+id/bonePArenaplay"
    android:layout_width="160dp"
    android:layout_height="90dp"
    android:layout_alignLeft="@+id/bmainleaderboard"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="200dp"
    android:background="@drawable/arenabtnback"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center|bottom"
    android:padding="10dp"
    android:shadowColor="@android:color/white"
    android:shadowRadius="25"
    android:text="Play"
    android:textColor="@android:color/white"
    android:textSize="20sp" />

Java 代码:-

public class MainActivity extends FragmentActivity implements OnClickListener
     {

    protected void onCreate(Bundle savedInstanceState) {
    ....

    bonePArenaplay = (Button) findViewById(R.id.bonePArenaplay);

    bonePArenaplay.setOnClickListener(this);
      .....
     }      

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Animation anim2 = AnimationUtils.loadAnimation(this, R.anim.clickanim);
    Log.d("clicked", "called");
    switch (arg0.getId()) {

    case R.id.bonePArenaplay:

        bonePArenaplay.setAnimation(anim2);
        bonePArenaplay.startAnimation(anim2);
        Intent i = new Intent(this, GenreActivity.class);
        i.putExtra("Mode", 1);
        startActivity(i);
        break;
     ....

我正在尝试一个多星期的答案,并查看了 stackoverflow 上的每个答案,但都是徒劳的

这个问题在我的应用程序中的每个按钮中都存在。每个 Activity 都不会响应第一次触摸,但会响应后续的触摸。任何帮助表示赞赏。

我尝试过的一些解决方案:-

1 setonclicklistener 在 oncreate 中

2 我试过设置ontouchlistener调用onclicklistener还是没有效果

【问题讨论】:

  • 您是在模拟器还是物理硬件设备上进行测试?这可能是其中之一的错误。
  • 我在真实设备上进行测试,但在我尝试过的其他设备上问题仍然存在
  • 你能把OnClickListener的代码贴出来吗?
  • 已经更新了同样的问题
  • 第一次触摸后 LogCat 中的任何内容?我会注释掉所有与动画相关的代码,然后再试一次看看是否是这里的问题。

标签: android button onclicklistener


【解决方案1】:

删除

android:focusable="false"

第一次单击使按钮获得焦点,因为它目前无法获得焦点。

【讨论】:

  • 你的代码sn-p在什么方法中?在哪个班?
  • 您是否也尝试过删除 android:focusableInTouchMode="false" 或将其变为真实?
猜你喜欢
  • 2016-09-26
  • 2019-09-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多