Aufgabe:
Wie findet man in Android Java ein View Control Element durch
eine Textbezeichnung?
Die Suche nach View Controls mit einer bestimmten Text in
der ID findet dadurch statt, dass man ein einzelnes Element mit
getIdentifier(text..) suchen kann.
Und mehrere Elemente kann man mit einem Loop nach Nummern
durchlaufen.
for (int iControl=0; iControl < 7; iControl++) {
int id
= getResources().getIdentifier("text" + iControl, "id", getPackageName());
TextView textView = (TextView) findViewById(id); // get the element
if(textView != null)
..->todo things
|
Beispiel Java Code in der Activity
//----< Find Views with Text
>----
for (int iControl=0; iControl < 7; iControl++) {
int id
= getResources().getIdentifier("textView_Wakeup"
+ iControl, "id", getPackageName());
TextView textView = (TextView) findViewById(id); // get the element
if(textView != null)
{
//< change
Background Colour >
if(textView.getBackground() !=null) {
textView.setBackgroundColor(Color.argb(55, 0, 255, 0));
}
//</ change
Background Colour >
}
}
//----</ Find Views with Text >----
|
Folgende TextView Elemente warden gefunden und mit einem
farbigen Hintergrund markiert
Xml Code
der activity_main.xml
Die TextView Elemente sind mit einer ID benannt, welche
sich nach einem Textmuster mit Zahl wieder finden lรคsst
<TextView
android:id="@+id/textView_Wakeup0"
|
Suche die TextView Elemente mit der Bezeichnung
Beispiel XML Code der Activity fรผr diesen Bereich
<TextView
android:id="@+id/textView_Wakeup0"
android:text="Set Wackup
Time"
android:layout_column="2"
android:layout_row="0"
android:textSize="30sp"
android:enabled="true"
android:clickable="true"
android:onClick="DayTimeViewOnClick"
/>
<TextView
android:id="@+id/textView_Wakeup1"
android:text="11:22"
android:layout_column="2"
android:layout_row="1"
android:textSize="30sp"
android:onClick="DayTimeViewOnClick"
/>
<TextView
android:id="@+id/textView_Wakeup2"
android:text="--:--"
android:layout_column="2"
android:layout_row="2"
android:textSize="30sp"
android:onClick="DayTimeViewOnClick"
/>
<TextView
android:id="@+id/textView_Wakeup3"
android:text="--:--"
android:layout_column="2"
android:layout_row="3"
android:textSize="30sp"
android:onClick="DayTimeViewOnClick"
/>
|