【问题标题】:Adding unique buttons dynmaically动态添加唯一按钮
【发布时间】:2026-01-05 09:00:01
【问题描述】:

这就是我现在拥有的:

当我单击添加类时,我希望另一个按钮直接显示在“添加类”按钮的上方。当点击这个新创建的按钮时,将切换到一个显示该班级所有作业的活动(新按钮)。我想我知道如何做到这一点,但仅限于最基本的形式。

我想有 3 件事我不确定:

1) 如何通过java在xml文件中的特定位置添加按钮?

2) 如何添加具有唯一标识符的按钮?

3) 我如何单击新创建的按钮,以便打开一个仅显示该按钮分配的新活动?

这是我的 xml 布局当前的样子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7BEDFC" >

<ScrollView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/mLlayoutBottomButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="#FFFFFF"
            android:text="Classes"
            android:textStyle="bold"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/roundedcorners"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:textStyle="bold"
            android:drawableRight="@drawable/rightarrow"
            android:gravity="left|center_vertical"
            android:text=" Math" />
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/roundedcorners"
            android:textStyle="bold"
            android:padding="10dp"
            android:text="Add a Class" />                               
    </LinearLayout>

 </ScrollView>

</RelativeLayout>

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    您想将按钮添加到我猜的线性布局(id mLlayoutBottomButtons)。在你的活动中,你这样做:

    LinearLayout buttonsLayout = (LinearLayout) findViewById(R.id.mLlayoutBottomButtons);
    

    您将动态创建按钮,您可以给它一个 id (button.setId(int id)),但这不是必需的。像这样创建并添加它

    Button newButton = new Button(this);
    newButton.setText("New button");
    buttonsLayout.addView(newButton, parentView.getChildCount() - 2); //see Edit at end of post
    

    现在我们创建了按钮并将其添加到布局中。作为最后一步,您只需为此按钮设置一个 OnClickListener。在 onClick 方法中,您可以使用 Intent 启动 Activity 并添加 Extras 以确定应在下一个 Acitivty 中显示哪个分配。

    Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
    intent.putExtra("assignmentType", "theNewButtonsAssignments");
    startActivity(intent);
    

    在新 Activity 中调用 getIntent().getStringExtra("assignmentType") 以检索应显示的分配。

    编辑: 您可以使用 addView(View child, int index) 将其插入到 linearLayout 内的某个位置。

    【讨论】:

    • 谢谢,这真的很有帮助!我不确定的一件事是putExtra。假设我在活动 A 中有按钮 A,在按钮 B 中有活动 B。因此,当我单击将我带到 ActivtyB 的按钮 A 时,然后当我单击在当前活动(ActivtyB)上添加按钮 C 的按钮 B 时。因此,当我返回到 activtyA 然后单击按钮A 返回到 ActivtyB 时,它会显示按钮 C, buttonB 和单击 buttonB 时创建的任何其他按钮。但是,如果我使用 activtyA 中的另一个按钮导航到 ActivityB,它将有一组全新的按钮。ButtonB 可能相同,但其余按钮不同。
    • 您应该查看活动的目的。活动没有您可以更改的状态并保存以供下次使用。当您导航回 ActivityA 时,Activity B 可能被完全销毁并在再次出现时重新创建。如果你需要保存一个状态,我建议在一个独立的类中这样做。如果你想永久保存它,你可以使用 SharedPreference 或文件系统/数据库。
    • 我读过一些关于使 ActivityB 的视图由数据模型支持的内容,您能否详细说明这种方法?
    • 这通常是关于您的应用程序的架构。您可以浏览一些关于多层架构和模型-视图-控制器的 Wikipedia 文章。您不必尝试应用它们并深入理解它们,但您会理解“数据模型”的含义。这对您来说意味着:将分配对象与 Activity 分开,并将它们的状态(如 isActivated)与 Activity 分开存储。然后 Activity 只是读出状态(actived = button is there)并在 UI 中显示。