2011-10-20 29 views
1

我有一个问题,当单击一个按钮时注册一个调用。View.OnClickListener()在Android响应错误的按钮

我有2个EditText,一个Button和一个CheckBox。我想在按下按钮时调用onClick()函数。不幸的是,它只发生在CheckBox被按下时,但它不是正确的ID,所以我很困惑。

下面是Java代码:

Button connect = (Button) findViewById(R.id.connectToServer); 
connect.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
    .... 
} 

正如你所看到的,我找的ID “connectToServer”。

这是我的.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:background="#ffb6b6b6" 
     android:textColor="#ffffffff" android:padding="2px" android:text="Settings" /> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:padding="13px"> 
     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:textSize="25px" 
      android:textColor="#ffffffff" android:text="Set server settings" /> 

     <TableLayout android:layout_marginTop="10px" 
      android:stretchColumns="1" android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TableRow> 
       <TextView android:text="IP address:" /> 

       <EditText android:id="@+id/serverIP" android:maxLength="15" /> 
      </TableRow> 

      <TableRow> 
       <TextView android:layout_marginTop="4px" android:text="Port:" /> 
       <EditText android:id="@+id/serverPort" android:maxLength="15" /> 
      </TableRow> 

     </TableLayout> 

     <LinearLayout android:layout_width="fill_parent" 
      android:layout_marginTop="10px" android:orientation="horizontal" 
      android:layout_height="fill_parent" android:gravity="center"> 
      <Button android:text="Connect" android:id="@+id/connectToServer" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     </LinearLayout> 
    </LinearLayout> 

    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:background="#ffb6b6b6" 
     android:textColor="#ffffffff" android:padding="2px" 
     android:layout_marginTop="10px" android:text="Availability" /> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:padding="13px"> 
     <TextView android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:textSize="25px" 
      android:textColor="#ffffffff" android:text="Set device available" /> 

     <TableLayout android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_weight="1"> 
      <TableRow> 
       <TextView android:layout_marginTop="10px" 
        android:layout_width="wrap_content" android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:text="Uncheck to disallow anyone from\nremotely connecting to the device." /> 
       <CheckBox android:id="@+id/checkbox" android:gravity="right" 
        android:checked="true" android:layout_weight="1" 
        android:layout_width="wrap_content" android:layout_height="fill_parent" /> 
      </TableRow> 
     </TableLayout> 
    </LinearLayout> 
</LinearLayout> 

我认为最重要的行会是这样:

<Button android:text="Connect" android:id="@+id/connectToServer" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

但代码只有当复选框按钮被选中或取消选中被调用。我不确定这是为什么。

非常感谢!

+0

复选框没有事件,这是唯一的事件。该复选框在按钮的onClick()内进行检查。 – Jary

+0

然后请显示您的所有来源正在尝试.. –

回答

4

试试这个,

Button connect = (Button) findViewById(R.id.connectToServer); 


connect.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 

    } 

}); 
+0

真棒!非常感谢你,那确实解决了它!是否使用View.OnClickListener()问题?我应该从现在开始使用OnClickListener()吗?非常感谢! – Jary

+0

我不确定,但我认为View.OnClickListener()为所有当前视图提供了单击侦听器,因此请使用我建议的简单方法,并且如果您发现这对您很有用,那么请注意并将其标记为正确它帮助你和其他用户也,谢谢。 – user370305

+0

当然,非常感谢! (我必须等待几分钟,直到我可以将其标记为正确)。非常感谢你,你帮了我很多! – Jary

3

有更简单的处理单击事件

1)添加Android的方式:的onClick分配的功能一样,

<Button android:id="@+id/connectToServer" 
android:text="Connect" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:onClick="onClick_connectToServer" 
/> 

2)实现的onClick在你的活动内

public void onClick_connectToServer(View v) { 

     String tag = "" + v.getTag(); 
// here you can handle the action 
} 

在这种情况下,您不需要明确添加onClickListener。


社会编码@AspiroTV

1

我遇到了同样的问题。我改变了XML中的按钮顺序,onClickListener开始调用错误的事件。该视图在设备上看起来是正确的,但R.java中的视图ID不正确。

使用Eclipse:我根本项目 - >清除...

的事件监听器开始正常工作。