【问题标题】:Button OnClickListener not working按钮 OnClickListener 不起作用
【发布时间】:2013-06-24 11:33:16
【问题描述】:

我有一个按钮,当我单击时会出现一个对话框。问题是,setOnClickListener 没有响应我的按钮。

这是我的 Java 代码:

Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(usage);

    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(price);

    b3 = (Button) findViewById(R.id.button3);
    b3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            System.out.println("Button3");
            mainDialog3 = new Dialog(Advice.this);
            mainDialog3.setContentView(R.layout.fuel);
            mainDialog3.setCancelable(true);

            mainDialog3.show();
            Window window = mainDialog3.getWindow();
            window.setLayout(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);

        }
    });


private View.OnClickListener usage = new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        System.out.println("Button1");
        Toast.makeText(getApplicationContext(), "Button 1",
                Toast.LENGTH_LONG).show();
        mainDialog1 = new Dialog(Advice.this);
        mainDialog1.setContentView(R.layout.usage);
        mainDialog1.setCancelable(true);

        mainDialog1.show();
        Window window = mainDialog1.getWindow();
        window.setLayout(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);

    }
};

private View.OnClickListener price = new View.OnClickListener() {
    public void onClick(View v) {
        System.out.println("Button2");
        mainDialog2 = new Dialog(Advice.this);
        mainDialog2.setContentView(R.layout.price);
        mainDialog2.setCancelable(true);

        mainDialog2.show();
        Window window = mainDialog2.getWindow();
        window.setLayout(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);

    }

};

问题在于按钮 1 (b1)。其余按钮(b2b3)工作得很好。这是我的 XML:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/button7"
    android:text="Edit" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button3"
    android:layout_alignLeft="@+id/button1"
    android:text="Edit" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView8"
    android:layout_alignLeft="@+id/button2"
    android:text="Edit" />

【问题讨论】:

  • 愚蠢的问题,但你确定你点击的按钮是button1吗?似乎您在 RelativeLayout 中有许多具有相同文本(“编辑”)的按钮。是否有可能另一个按钮位于您认为是 button1 的顶部,从而阻止它被点击?
  • 是的,我检查了没有其他按钮与按钮重叠。相反,我添加了一个新按钮并检查但仍然是同样的问题。
  • 嘿,非常感谢,我不知道问题出在哪里,但现在可以了。
  • @IrshadKhan:在编辑帖子时,请尝试编辑不仅仅是几个字——尝试修复尽可能多的问题,您可以在帖子中找到。如果建议的编辑仅对帖子进行了一点点改进,则可能会因为“太小”而被拒绝。谢谢!

标签: android-layout onclicklistener android-button buttonclick


【解决方案1】:

试试b1.setOnClickListener(usage); 而不是 b1.setOnClickListener(usage); 在您的代码中它将起作用..brother

【讨论】:

  • 有什么区别?
  • 尝试吃东西而不是把食物放进嘴里?
【解决方案2】:

您无需在代码中手动设置点击监听器。最简单的方法是在 XML 中添加回调。

像这样:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/button7"
    android:onClick="foo"
    android:text="Edit" />

如果你使用 eclipse 和 android 工具,你可以直接在 XML 编辑器中设置这个属性。然后在代码中定义一个以 Click 属性命名的函数,它将被自动调用。

public void foo(View oView)
{
    Button cklicked ...
}

您也可以动态设置点击侦听器,但只有当您像这样动态创建按钮时才需要这样做:

    ImageButton button = (ImageButton) findViewById(R.id.list_manager_add);
    button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            button pressed...
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2021-03-17
    相关资源
    最近更新 更多