如何管理未列出的新创建的按钮,Android Studios

时间:2018-08-25 20:22:05

标签: java android

我已经设置了此活动,以便当您按下FloatingActionButton时,它将创建一个新的XML Button并将其添加到LinearLayout。我想管理我创建的这些新按钮,但是我不知道该怎么做。我需要给每个按钮一个单独的名字吗?还是将它们放在按钮阵列中?这是我的代码和xml。希望我对此解释足够好。

代码:

public class CataFragment extends Fragment {

private LinearLayout mLayout;
private FloatingActionButton mButton;
private String newCataLine;

//private ArrayList<Button> buttonsList = new ArrayList<>();

public static CataFragment newInstance() {
    CataFragment fragment = new CataFragment();
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_cata, container, 
false);

    mLayout = (LinearLayout) rootView.findViewById(R.id.LinearLineLayout);
    mButton = (FloatingActionButton) rootView.findViewById(R.id.plus);
    mButton.setOnClickListener(onClick());

    Button cataButton = new Button(getContext());

    return rootView;
}

private View.OnClickListener onClick() {
    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showChangeLangDialog();
        }
    };
}

private TextView createNewTextView(String text) {
    final LinearLayout.LayoutParams lparams =
            new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT);

    final Button cataButton = new Button(getContext());
    cataButton.setAllCaps(false);
    cataButton.setTextSize(40);
    cataButton.setLayoutParams(lparams);
    cataButton.setText(text);
    return cataButton;
}


public void showChangeLangDialog() {
    final AlertDialog.Builder dialogBuilder = new 
AlertDialog.Builder(getContext());
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
    dialogBuilder.setView(dialogView);

    final EditText edt = (EditText) dialogView.findViewById(R.id.edit1);

    dialogBuilder.setTitle("Create New Category");
    dialogBuilder.setPositiveButton("Add", new 
DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            newCataLine = edt.getText().toString();
            mLayout.addView(createNewTextView(newCataLine));
        }
    });
    dialogBuilder.setNegativeButton("Don't Add", new 
DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    AlertDialog b = dialogBuilder.create();
    b.show();
}
}

Xml:

<android.support.constraint.ConstraintLayout 
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="match_parent"
tools:context=".HomeFragment">

<FrameLayout
    android:id="@+id/frame_fragmentholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/constraintLayout">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <LinearLayout
                    android:id="@+id/LinearLineLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                </LinearLayout>
            </ScrollView>
        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="fitXY"
                android:src="@android:color/holo_blue_dark"
                android:visibility="visible"
                app:layout_constraintBottom_toBottomOf="@+id/InputText"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/InputText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:backgroundTint="@android:color/black"
                android:cursorVisible="false"
                android:inputType="textNoSuggestions"
                android:textCursorDrawable="@null"
                android:textSize="20sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/plus"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginBottom="24dp"
            android:layout_marginEnd="24dp"
            android:clickable="true"
            android:src="@drawable/plus"
            app:backgroundTint="@android:color/holo_blue_light"
            app:elevation="6dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

    </android.support.constraint.ConstraintLayout>

</FrameLayout>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

由于您正在createNewTextView中创建新的文本视图按钮,因此可以创建一个ArrayList<TextView>来存储对您创建的所有文本视图的引用,然后可以通过它们的索引对其进行访问。如果他们有特殊的ID,则可以改用地图进行轻松检索。