在多个xml文件中管理onClickListeners

时间:2018-01-15 12:20:58

标签: java android onclick

我创建了一个应用,可以在用户点击添加按钮时动态添加表单,并在用户点击取消按钮时将其删除。我有两个取消按钮,一个在Field.xml文件中,另一个在activity_school_search_setup文件中。

它们都具有相同的ID,但Field.xml文件中的ID不会删除该字段。似乎onclicklistener不能用于删除按钮。

主Java类文件

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;


public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {

    private EditText searchSchoolID;
    private Button searchSchoolButtonID;
    private ListView listOfSchoolsID;
    private Button  openNewschoolID;
    private LinearLayout schoolSetupLayout;
    private Button addNewClass;
    private EditText classNameEditText;
    private Button deleteButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_search_setup);

        findAllViewsID();
        initializeListenners();
    }

    public void findAllViewsID(){

        classNameEditText = findViewById(R.id.classNameText);
        addNewClass = findViewById(R.id.addNewClassButton);
        schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
        searchSchoolID = findViewById(R.id.searchSchoolID);
        searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
        listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
        openNewschoolID = findViewById(R.id.openNewschoolID);
        deleteButton = findViewById(R.id.delete_button);




    }

    public void initializeListenners(){
        openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
        addNewClass.setOnClickListener(SchoolSearchSetup.this);
        deleteButton.setOnClickListener(SchoolSearchSetup.this);


    }




    public void addNewClass(){

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.field, null);

        schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);


    }




    @Override
    public void onClick(View view) {

        switch(view.getId()){

            case R.id.openNewschoolID:

              displaySchoolSetUpForms();

              break;

           case R.id.addNewClassButton:

               addNewClass();

               break;

            case R.id.delete_button:

                schoolSetupLayout.removeView((View) view.getParent());


        }

    }


    private void displaySchoolSetUpForms() {

        schoolSetupLayout.setVisibility(View.VISIBLE);

    }



}

这是主要的XML活动文件

activity.school_search_setup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.demeainc.demea.MainActivity"
    android:orientation="vertical"
    android:background="@color/backgroundColor">

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

        <ImageView
            android:id="@+id/backArrowClassView"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/ic_arrow_class"
            android:layout_marginLeft="5dp"/>


    </LinearLayout>

    <ScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="50dp"
                    android:text="Search your school."
                    android:gravity="center"
                    android:textSize="25dp" />

                <android.support.v7.widget.CardView
                    android:foreground="?attr/selectableItemBackground"
                    android:clickable="true"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="5dp">
                <EditText
                    android:id="@+id/searchSchoolID"
                    android:layout_width="270dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:drawableLeft="@drawable/ic_search_black_24dp"
                    android:hint="Search"/>

                </android.support.v7.widget.CardView>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="20dp">

                <Button
                    android:id="@+id/searchSchoolButtonID"
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:layout_marginLeft="60dp"
                    android:text="Search"
                    android:textColor="#ffff"
                    android:background="@color/colorPrimary"
                    android:textStyle="bold"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="10dp">

                <android.support.v7.widget.CardView
                    android:layout_width="300dp"
                    android:layout_height="wrap_content">

                    <ListView
                        android:id="@+id/listOfSchoolsID"
                        android:layout_width="match_parent"
                        android:layout_height="80dp"
                        android:background="@android:color/transparent"
                        android:cacheColorHint="@color/ligtherDarkGrey"
                        android:divider="#CCCCCC"
                        android:dividerHeight="2dp"
                        android:paddingLeft="2dp" >
                    </ListView>


                </android.support.v7.widget.CardView>



            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical">

                <Button
                    android:id="@+id/openNewschoolID"
                    android:layout_width="200dp"
                    android:layout_height="60dp"
                    android:layout_marginLeft="60dp"
                    android:background="@color/colorPrimary"
                    android:text="Open New School"
                    android:textColor="#ffff"
                    android:textStyle="bold" />

            </LinearLayout>


            <LinearLayout
                android:id="@+id/schoolSetupLayout"

                android:visibility="invisible"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/schoolSetupText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="School Setup"
                    android:textSize="20dp"
                    android:layout_marginLeft="50dp"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <EditText
                        android:id="@+id/classNameText"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="5"
                        android:hint="Class name, e.g Grade one" />

                    <Button
                        android:id="@+id/delete_button"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_weight="1"
                        android:background="@android:drawable/ic_delete"/>


                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_gravity="center">

                    <Button
                        android:id="@+id/addNewClassButton"
                        android:layout_marginTop="15dp"
                        android:layout_marginLeft="50dp"
                        android:layout_width="100dp"
                        android:layout_height="50dp"
                        android:text="Add new class"
                        android:textColor="#ffff"
                        android:background="@color/colorPrimary"/>

                    <Button
                        android:id="@+id/nextButton"
                        android:layout_marginTop="25dp"
                        android:layout_marginLeft="8dp"
                        android:layout_width="100dp"
                        android:layout_height="50dp"
                        android:text="Next"
                        android:textColor="#ffff"
                        android:background="@color/green"/>

                </LinearLayout>


            </LinearLayout>

        </LinearLayout>



    </ScrollView>


</LinearLayout>

这是field.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/schoolSetupLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/classNameText"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:hint="Class name, e.g Grade one" />

    <Button
        android:id="@+id/delete_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@android:drawable/ic_delete"/>


</LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您应该在

上添加clickListener
View rowView = inflater.inflate(R.layout.field, null);

如果您希望在其上发生点击事件,请在android:onClick的元素中添加field.xml

initializeListenners();仅在初始布局期间调用一次,而不是在动态视图添加中调用。

答案 1 :(得分:0)

在夸大该布局后,为OnClickListener按钮实施Field.xml

以下是示例:

public void addNewClass(){

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.field, null);
    final Button btnDelete = rowView.findViewById(R.id.delete_button);
    btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Your stuff
        }
    });

    schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}

希望有所帮助:)