Java 按钮点击动作

Java 按钮点击动作,java,android,android-layout,Java,Android,Android Layout,我正在开发一个联系人列表android应用程序,每次按下“添加地址”按钮时,我都会尝试插入5个新的编辑文本字段。我试图将java文件中的文本字段放在onClick方法中,但没有成功,问题是我听说可以在另一个布局中添加一个布局,现在我尝试在包含按钮的布局中添加一个包含5个文本字段的布局。这是包含一些其他字段和addaddress按钮的.xml文件的代码 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android

我正在开发一个联系人列表android应用程序,每次按下“添加地址”按钮时,我都会尝试插入5个新的编辑文本字段。我试图将java文件中的文本字段放在onClick方法中,但没有成功,问题是我听说可以在另一个布局中添加一个布局,现在我尝试在包含按钮的布局中添加一个包含5个文本字段的布局。这是包含一些其他字段和addaddress按钮的.xml文件的代码

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

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:id="@+id/add_contact_layout"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/first_name_editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/first_name_hint" >
        </EditText>

        <EditText
            android:id="@+id/second_name_editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/second_name_hint" />

        <EditText
            android:id="@+id/mobile_phone_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/cell_phone_hint"
            android:inputType="phone" />

        <EditText
            android:id="@+id/work_phone_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/work_phone_hint"
            android:inputType="phone" />

        <EditText
            android:id="@+id/email_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/email_text_hint"
            android:inputType="textEmailAddress" />

        <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/add_address_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add_address_text" />

        <Spinner
            android:id="@+id/address_spinner"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </TableRow>

    </LinearLayout>

</ScrollView>

这是包含五个编辑文本字段的新.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:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
        android:id="@+id/typeOfAdresstextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/spinner_value"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/address_street_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"

            android:hint="@string/street_hint"
            android:inputType="textPostalAddress" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/address_number_editText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"

            android:hint="@string/address_number_hint"
            android:inputType="number" />

        <EditText
            android:id="@+id/address_city_editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"

            android:hint="@string/city_hint"
            android:inputType="textPostalAddress" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/address_state_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"

            android:hint="@string/state_hint"
            android:inputType="textPostalAddress" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/address_zipcode_editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"

            android:hint="@string/zipcode_hint"
            android:inputType="number" />

    </TableRow>


</LinearLayout>


我的目标是在按钮和微调器下方插入这5个文本字段,每次我按下按钮时,这些文本字段都会再次出现,以便用户能够添加他/她想要的所有地址(在先前填充的地址文本字段下方)。我不知道这是否是正确的方法,但如果有其他方法来实现这一点,我愿意接受建议。在这里,我留下了我想要完成的一部分

您可以在第一个XML布局文件中的
表格布局
之后创建
框架布局
,然后将第二个布局充气到
框架布局
。或者,只需将第二个布局嵌入包含布局的第一个布局中(如上文建议的
FrameLayout
),并将容器标记为
android:visibility=“gone”
。按下按钮后,更改可见性,其他5个字段将变为可见。

好的,您对代码做了什么,是否有任何错误。如果是这样,错误是什么?我试图用这段代码使java中的文本字段适应我想要的内容,首先我创建了一个线性布局final LinearLayout subParentView=new LinearLayout(这个);然后我添加了EditText=neweditText(getApplicationContext())的文本字段;然后我将其添加到subparent subParentView.addView(edittext);但是当我运行此操作时,活动全部为空,第一个文本字段和按钮不显示嘿,谢谢你的回答,但我已经尝试了可见性属性,关于这一点,它只是使第二个xml中已经创建的文本字段集可见,我希望能够创建更多的文本字段集,作为第二个xml中的文本字段集(例如为用户填充这些文本字段的空白副本)然后可以使用第一种方法,将新的子对象充气到第二种布局中。或者,可能更好的方法是使用
ListView
,您正在将新行插入适配器中,并且列表随着您添加新条目而增长。你只需要实现适当的支持类和适配器。我是所有android方面的新手,你知道有没有任何教程可以让我学习如何扩展布局?不,我不知道。但是,如果您查看
LayoutInflater
文档以及SDK中的一些示例,您应该能够找到您需要的内容。