У меня проблема с регистрацией вызова при нажатии кнопки.
У меня есть 2 EditTexts, одна кнопка и один CheckBox. Я хотел бы, чтобы функция onClick () вызывалась при нажатии кнопки. К сожалению, это происходит только при нажатии CheckBox, но это неверный идентификатор, поэтому я запутался.
Вот код Java:
Button connect = (Button) findViewById(R.id.connectToServer);
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
....
}
Как видите, я ищу идентификатор "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>
но код вызывается только тогда, когда кнопка CheckBox отмечена или не отмечена. Я не уверен, почему это так.
Большое спасибо!