2011-10-20 30 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:我根本項目 - >清除...

的事件監聽器開始正常工作。