пятница, 27 января 2012 г.

Отношение композиции в xml файлах Android-а. Способ повесить слушателя на контрол в xml-е.

1. Отношение композиции в xml-файлах пример:

Код файла test.xml :

 <EditText
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="16dp" />


Код файла test2.xml:

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" >

    <include layout="@layout/test.xml" />;

 </LinearLayout>

2. Способ повесить слушателя на контрол в xml-е:
Пример кнопки в xml-файле:

  <ImageButton
   android:id="@+id/minus_image_button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@android:color/transparent"
   android:onClick="onClickRemoveRoutePlace"
   android:src="@drawable/minus" />

Пример функции в Activity, которая будет вызываться при нажатии кнопки:
      public void onClickRemoveRoutePlace(View v) {
      _numberLastRoutePoint--;
      int amountElements = _routePlacesLinearLayout.getChildCount();
      if (amountElements > 2) {
         ImageButton imageButton = (ImageButton)     _routePlacesLinearLayout.getChildAt(amountElements -  2).findViewById(R.id.minus_image_button);
      imageButton.setVisibility(View.VISIBLE);
    }
    _routePlacesLinearLayout.removeViewAt(amountElements - 1);
  }