按钮OnClickListener不起作用

按钮OnClickListener不起作用

本文介绍了按钮OnClickListener不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a button, when I click a dialog box appears. The problem is, the setOnClickListener is not responding to my button.

这是我的Java代码:

Here is my Java code:

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:

The problem is with button 1 (b1). The rest of the buttons (b2, b3) work perfectly fine. Here is my 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" />

推荐答案

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

You don't need to set the click listener manually in the code. The easiest way, to do this is to add the callback in the 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属性命名的函数,该函数将被自动调用.

If you use eclipse with the android tools, you can set this property directly in the XML editor. Then you define a function in your code named after the Click property and it will be called automatically.

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

您还可以动态设置点击侦听器,但这仅在您像这样动态创建按钮时才需要:

You can also set a click listener dynamically, but this you need only if you create buttons on the fly like this:

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

这篇关于按钮OnClickListener不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:49