Taschenrechner button autom. erzeugen?

  • 1 Antworten
  • Letztes Antwortdatum
F

Fred12

Gast
hi,
gibs ne Möglichkeit z.B in einer Taschenrechner app die buttons dynamisch zu erzeugen ? Bisher hab ich alles von Hand eingetippt :

Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/displayField"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:textSize="60sp"
        android:text="@string/Eingabe"
        android:inputType="number"
        android:textColor="@android:color/white"
        android:editable="false"
        android:background="@android:color/black" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_below="@id/displayField">

        <Button
            android:id="@+id/emptyButton"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:enabled="false"
            android:layout_height="60dp"
            android:layout_width="wrap_content" />

        <ImageButton
            android:id="@+id/backspace"
            android:layout_height="60dp"
            android:layout_width="wrap_content"
            android:layout_weight="0.37"
            android:src="@drawable/backspace" />

        </LinearLayout>

    <TableLayout
        android:stretchColumns="*"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_margin="0dp"
        android:layout_below="@id/linearLayout1">

        <TableRow
           android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            >

            <ImageButton
                android:id="@+id/btn7"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/seven"
                />


            <ImageButton
                android:id="@+id/btn8"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/eight"
                />

            <ImageButton
                android:id="@+id/btn9"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/nine"
                />

            <ImageButton
                android:id="@+id/btnDivide"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/division"
                />



        </TableRow>

        <TableRow
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="2">
            <ImageButton
                android:id="@+id/btn4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/four"
                />

            <ImageButton
                android:id="@+id/btn5"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/five"
                />
            <ImageButton
                android:id="@+id/btn6"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/six"
                />
            <ImageButton
                android:id="@+id/btnMultiply"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/multiply"
                />

        </TableRow>

        <TableRow
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="2">

        <ImageButton
            android:id="@+id/btn1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/btn2"
            android:src="@drawable/one"
            />

            <ImageButton
                android:id="@+id/btn2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/two"
                />
            <ImageButton
                android:id="@+id/btn3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/three"
                />
            <ImageButton
                android:id="@+id/btnMinus"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/btn2"
                android:src="@drawable/minus"
                />

        </TableRow>

        <TableRow
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_weight="2">
        <ImageButton
            android:id="@+id/btnComma"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/btn2"
            android:src="@drawable/comma2"
            />
            <ImageButton
                android:id="@+id/btn0"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/zero"
                />
            <ImageButton
                android:id="@+id/btnEqual"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@color/white"
                android:src="@drawable/equals"
                />
            <ImageButton
                android:id="@+id/btnPlus"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/plus"
                />
    </TableRow>


    </TableLayout>


</RelativeLayout>

bei soviele gleichen Buttons aber nicht so sinnvoll irgendwie... kann ich mir die
auch erzeugen lassen?

2.Frage: Ich nutze Android Studio, gibs da ne Möglichkeit sein Mobile Device zu konfigurieren und als Testgerät zu nutzen?
 
ja du kannst auch alles was du in xml machst, per java code erzeugen.

Es gibt die passenden Klassen natürlich im Framework. Beispiel

Button btn = new Button(activity);

und schon hast nen Button. aber ist auch sehr mühsam das alles anständig zu positionieren, vor allem im hinblick auf unterschiedliche devices
 
Zurück
Oben Unten