Страница 1 из 1

Выделение TextView прямоугольником выделения

Добавлено: 07 фев 2014, 12:35
Leon2k
Здравствуйте!

Помогите начинающему девелоперу ;)

Пишу будильник (интерфейс "копирую" со стандартного).

В настройках будильника внутри LinearLayout вставил по 2 TextView (описание и значение).

Событие onClick обрабатываю от Layout-а.

Как сделать так, чтобы при нажатии на Layout он выделялся синим прямоугольником выделения (как элемент ListView) ???

Или, может есть другой (стандартный?) способ отображения этого прямоугольника?

Re: Выделение TextView прямоугольником выделения

Добавлено: 07 фев 2014, 12:48
anber
Ставишь на Layout background такую xml:

Код: Выделить всё

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false"
        android:drawable="@drawable/normal">
  </item>

  <item android:state_pressed="true"
        android:drawable="@drawable/pressed">
  </item></selector>
где normal и pressed соответствующие хмл или всемто них можно просто цвета

Re: Выделение TextView прямоугольником выделения

Добавлено: 07 фев 2014, 14:15
Leon2k
anber писал(а):Ставишь на Layout background такую xml:

Код: Выделить всё

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="false"
        android:drawable="@drawable/normal">
  </item>

  <item android:state_pressed="true"
        android:drawable="@drawable/pressed">
  </item></selector>
где normal и pressed соответствующие хмл или всемто них можно просто цвета
Спасибо!

А в каких ресурсах хранятся стандартные цвета и прямоугольники нажатого/отжатого состояния?

Re: Выделение TextView прямоугольником выделения

Добавлено: 07 фев 2014, 15:47
anber
цвета - http://stackoverflow.com/a/7323234/1159507
До стандартных прямоугольников можно достучаться например для кнопки - android.R.drawable.btn_default, но они зависят от версии андроида, так что лучше их скопировать из какого-либо проекта типа HoloEverywhere: https://github.com/Prototik/HoloEverywh ... s/drawable
см. также http://stackoverflow.com/questions/1382 ... dittext-fr

Re: Выделение TextView прямоугольником выделения

Добавлено: 08 фев 2014, 11:57
Leon2k
anber писал(а):цвета - http://stackoverflow.com/a/7323234/1159507
До стандартных прямоугольников можно достучаться например для кнопки - android.R.drawable.btn_default, но они зависят от версии андроида, так что лучше их скопировать из какого-либо проекта типа HoloEverywhere: https://github.com/Prototik/HoloEverywh ... s/drawable
см. также http://stackoverflow.com/questions/1382 ... dittext-fr
Спасибо! с этим вроде разобрался.
Еще вопрос возник. при открытии активити редактирования будильника фокус переходил на EditText. я его убрал:

Код: Выделить всё

.setSelected(false);
Теперь новая проблема: onClick на LinearLayout-е срабатывает со 2-го раза (как будто сначала принимает фокус, а потом кликается)

Вот мой активити:

[syntax=xml]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="@android:color/background_dark"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="top|center_horizontal"
android:orientation="vertical"
android:padding="2dp" >

<CheckBox
android:id="@+id/checkBoxEnabled"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/AddEditchkEnabled"
android:textSize="20sp"
android:textStyle="bold" />

<LinearLayout
android:id="@+id/llTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@layout/custom_background"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >

<TextView
android:id="@+id/TextTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/StringTime"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvTimeValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="14sp" />

</LinearLayout>

<LinearLayout
android:id="@+id/llDaysOfWeek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginBottom="1dp"
android:background="@layout/custom_background"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >

<TextView
android:id="@+id/TextDaysOfWeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/StringDaysOfWeek"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvDaysOfWeekValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="14sp" />

</LinearLayout>

<LinearLayout
android:id="@+id/llRintone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@layout/custom_background"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >

<TextView
android:id="@+id/TextRingtone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/StringRingtone"
android:textSize="20sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvRingtoneValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="14sp" />

</LinearLayout>

<CheckBox
android:id="@+id/checkBoxVibrate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_vertical"
android:text="@string/chkVibrate"
android:textSize="20sp"
android:textStyle="bold" />

<EditText
android:id="@+id/etDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="@string/StringDescriptionHint"
android:textStyle="italic" >

</EditText>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >

<Button
android:id="@+id/btSave"
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/AddEditSave" />

<Button
android:id="@+id/btCancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/AddEditCancel" />

</LinearLayout>

</LinearLayout>[/syntax]

Обработчики назначены программно

Re: Выделение TextView прямоугольником выделения

Добавлено: 09 фев 2014, 09:18
Leon2k
Всё, сам разобрался. Изменил XML вот так:

[syntax=xml]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="@android:color/background_dark"
android:clickable="false"
android:focusable="false"
android:gravity="top|center_horizontal"
android:orientation="vertical"
android:padding="2dp" >

<CheckBox
android:id="@+id/checkBoxEnabled"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/AddEditchkEnabled"
android:textSize="18sp"
android:textStyle="bold" />

<LinearLayout
android:id="@+id/llTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@layout/custom_background"
android:clickable="true"
android:focusable="true"
android:orientation="vertical" >

<TextView
android:id="@+id/TextTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:clickable="false"
android:focusable="false"
android:text="@string/StringTime"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvTimeValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="12sp" />

</LinearLayout>

<LinearLayout
android:id="@+id/llDaysOfWeek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginBottom="1dp"
android:background="@layout/custom_background"
android:clickable="true"
android:focusable="true"
android:orientation="vertical" >

<TextView
android:id="@+id/TextDaysOfWeek"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:focusable="false"
android:text="@string/StringDaysOfWeek"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvDaysOfWeekValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textSize="12sp" />

</LinearLayout>

<LinearLayout
android:id="@+id/llRintone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@layout/custom_background"
android:clickable="true"
android:focusable="true"
android:orientation="vertical" >

<TextView
android:id="@+id/TextRingtone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:focusable="false"
android:text="@string/StringRingtone"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tvRingtoneValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_weight="1"
android:focusable="false"
android:textSize="12sp" />

</LinearLayout>

<CheckBox
android:id="@+id/checkBoxVibrate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_vertical"
android:text="@string/chkVibrate"
android:textSize="18sp"
android:textStyle="bold" />

<EditText
android:id="@+id/etDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="@string/StringDescriptionHint"
android:textStyle="italic" >

</EditText>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" >

<Button
android:id="@+id/btSave"
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/AddEditSave" />

<Button
android:id="@+id/btCancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/AddEditCancel" />

</LinearLayout>

</LinearLayout>[/syntax]

Убрал Clickable и Focusable у ненужных элементов.