Android 通常将onClickListener设置为膨胀的xml按钮

Android 通常将onClickListener设置为膨胀的xml按钮,android,forms,onclick,android-custom-view,Android,Forms,Onclick,Android Custom View,我有一个自定义的edittext,它的行为类似于表单。它有一个标签、编辑文本和一个按钮。我正在使用标签将其包含在布局中,以便可以重复使用表单。我希望按钮具有的功能是在单击表单时复制表单。这对我来说是可能的第一次点击第一个表单。但是,当我单击新表单的按钮时,没有任何操作。但是,如果单击第一个表单按钮,将创建一个新表单。这是我的xml:我包括了它 <?xml version="1.0" encoding="utf-8"?> 我尝试了各种方法来实现这一点,但我没有运气。任何帮助或建议都将

我有一个自定义的edittext,它的行为类似于表单。它有一个标签、编辑文本和一个按钮。我正在使用标签将其包含在布局中,以便可以重复使用表单。我希望按钮具有的功能是在单击表单时复制表单。这对我来说是可能的第一次点击第一个表单。但是,当我单击新表单的按钮时,没有任何操作。但是,如果单击第一个表单按钮,将创建一个新表单。这是我的xml:我包括了它

<?xml version="1.0" encoding="utf-8"?>

我尝试了各种方法来实现这一点,但我没有运气。任何帮助或建议都将非常感谢。

如果您单击新创建的按钮,这一行将不正确

    if (view == findViewById(R.id.form_button))

您可以为新创建的按钮分配一个固定id,或者尝试使用偏移量

我想我必须创建一个类级按钮和视图,以便从新表单更新按钮的功能

        try{
        LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
        LinearLayout layout2 = (LinearLayout) layout.findViewById(R.id.numbersll);

        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        tempView = vi.inflate(R.layout.form, null);
        numberEntryViews.add(tempView);

        layout2.addView(tempView);
        btn = (ImageButton) tempView.findViewById(R.id.form_button);
        //TODO: Work from here. Ask on Stack Overflow.
        btn.setOnClickListener(this);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Log.d(TAG, "Failed to inflate");
    }
btn是类级别,每次单击都会更新。删除最后一个按钮的功能,这在我的情况下是有意义的

    if (view == findViewById(R.id.form_button))
        try{
        LinearLayout layout = (LinearLayout) findViewById(R.id.contacteditll);
        LinearLayout layout2 = (LinearLayout) layout.findViewById(R.id.numbersll);

        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        tempView = vi.inflate(R.layout.form, null);
        numberEntryViews.add(tempView);

        layout2.addView(tempView);
        btn = (ImageButton) tempView.findViewById(R.id.form_button);
        //TODO: Work from here. Ask on Stack Overflow.
        btn.setOnClickListener(this);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Log.d(TAG, "Failed to inflate");
    }