Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中,如何从数据库中动态地为按钮分配名称?_Android_Database - Fatal编程技术网

在android中,如何从数据库中动态地为按钮分配名称?

在android中,如何从数据库中动态地为按钮分配名称?,android,database,Android,Database,这是我的主xml文件…我需要动态地为4个按钮分配名称…我怎么做?? 需要动态命名的四个按钮是 左上方 右上方 洛维夫特 右下角 </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp"> <Button an

这是我的主xml文件…我需要动态地为4个按钮分配名称…我怎么做?? 需要动态命名的四个按钮是 左上方 右上方 洛维夫特 右下角

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp">

    <Button
        android:id="@+id/UpperLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


    <Button
        android:id="@+id/UpperRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/LowerLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
    <Button
        android:id="@+id/LowerRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textAlignment="center"
        android:text="@string/answer"
        />
    </LinearLayout>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp">

    <Button
        android:id="@+id/UpperLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


    <Button
        android:id="@+id/UpperRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/LowerLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
    <Button
        android:id="@+id/LowerRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textAlignment="center"
        android:text="@string/answer"
        />
    </LinearLayout>


这是我的主xml文件…我需要从数据库中为4个按钮动态分配名称…我如何才能做到???

您只需在初始化
按钮
视图后使用它即可

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp">

    <Button
        android:id="@+id/UpperLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


    <Button
        android:id="@+id/UpperRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/LowerLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
    <Button
        android:id="@+id/LowerRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textAlignment="center"
        android:text="@string/answer"
        />
    </LinearLayout>
yourbutton1.setText("Button1");
yourbutton2.setText("Button2");
yourbutton3.setText("Button3");
yourbutton4.setText("Button4");

您可以通过Java来实现

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp">

    <Button
        android:id="@+id/UpperLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


    <Button
        android:id="@+id/UpperRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"/>


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/LowerLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
    <Button
        android:id="@+id/LowerRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:gravity="center"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textAlignment="center"
        android:text="@string/answer"
        />
    </LinearLayout>
button.setText("button name");

只需从数据库中获取名称并将其设置为按钮即可。是关于Android SQLite数据库的教程。