我有一個問題,當單擊一個按鈕時註冊一個調用。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>
但代碼只有當複選框按鈕被選中或取消選中被調用。我不確定這是爲什麼。
非常感謝!
複選框沒有事件,這是唯一的事件。該複選框在按鈕的onClick()內進行檢查。 – Jary
然後請顯示您的所有來源正在嘗試.. –